运算符>>需要在命名空间std ?? [英] operator >> needs to be in namespace std??

查看:87
本文介绍了运算符>>需要在命名空间std ??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板< typename T>

std :: istream&运算符>(std :: istream& in,std :: pair< T,T& p)

{

in> p.first> p。第二个;

返回;

}

....

std :: istream_iterator< std :: pair< size_type,size_type

in_beg(std :: cin),in_end;

....

无法编译。包装运算符>为名称空间中的对

std {}有效。因为你不是允许的把东西插入

命名空间std为什么看似需要,怎么可以这样做呢?没有它的


template < typename T >
std::istream & operator >(std::istream & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
....
std::istream_iterator< std::pair<size_type, size_type
in_beg(std::cin), in_end;
....
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you''re not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?

推荐答案

Noah Roberts写道:
Noah Roberts wrote:

template< typename T>

std :: istream&运算符>(std :: istream& in,std :: pair< T,T& p)

{

in> p.first> p。第二个;

返回;

}


...

std :: istream_iterator< std :: pair< size_type,size_type

in_beg(std :: cin),in_end;

...


失败编译。包装运算符>为名称空间中的对

std {}有效。因为你不是允许的将东西插入

命名空间std为什么看似需要,怎么可以这样做

没有呢?
template < typename T >
std::istream & operator >(std::istream & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
...
std::istream_iterator< std::pair<size_type, size_type
in_beg(std::cin), in_end;
...
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you''re not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?



看起来像ADL的东西。小心发布一个完整的例子,这样我就可以尝试在这里编译它?
我想确定我正在做的事情和你一样。

我不确定std :: operator>>(...)的后果可能是什么。


-

NOUN:1。遗嘱遗留给他人的金钱或财产。 2.从祖先或前任或过去那里下来的东西:b
宗教自由的遗产。 ETYMOLOGY:MidE legacie,副手办公室,来自OF,来自ML legatia的
,来自L legare,以及deute,遗赠。 www.bartleby.com/61/


Noah Roberts写道:
Noah Roberts wrote:

template< typename T>

std :: istream&运算符>(std :: istream& in,std :: pair< T,T& p)

{

in> p.first> p。第二个;

返回;

}


...

std :: istream_iterator< std :: pair< size_type,size_type

in_beg(std :: cin),in_end;

...


失败编译。包装运算符>为名称空间中的对

std {}有效。因为你不是允许的把东西插入

命名空间std为什么看似需要,怎么可以这样做

没有呢?
template < typename T >
std::istream & operator >(std::istream & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
...
std::istream_iterator< std::pair<size_type, size_type
in_beg(std::cin), in_end;
...
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you''re not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?



我以为你被允许添加到std命名空间的东西(即

预先存在的运算符的重载 - 就像这样)。几年前有一个关于这个问题的讨论,我确实记得双方

的论点,但是我不记得讨论是多么明确。

I thought you were allowed to add things to the std namespace (namely
overloads of pre-existing operators - just like this). There was a
discussion about this a couple of years ago, and I do remember two sides
of the argument, however I can''t remember how definitive the discussion was.


Gianni Mariani写道:
Gianni Mariani wrote:

Noah Roberts写道:
Noah Roberts wrote:

>模板< typename T>
std :: istream&运算符>(std :: istream& in,std :: pair< T,T& p)
{
> p.first> p.second;
返回;
}

...
std :: istream_iterator< std :: pair< size_type,size_type
in_beg(std :: cin),in_end;
...

无法编译。包含运算符>为名称空间中的对
std {}有效。因为你不是允许的将东西插入
命名空间std为什么看似需要,怎么可以这样做呢
没有它?
>template < typename T >
std::istream & operator >(std::istream & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
...
std::istream_iterator< std::pair<size_type, size_type
in_beg(std::cin), in_end;
...
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you''re not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?



我以为你被允许将东西添加到std命名空间(即

预先存在的运算符的重载 - 就像这样)。


I thought you were allowed to add things to the std namespace (namely
overloads of pre-existing operators - just like this).



奇怪的是,这有效:


#include< iostream>


模板< typename T>

std :: ostream& operator<<(std :: ostream& out,std :: pair< T,T>& p){

return out<< p.first<< p.second;

}

int main(){

std :: pair< int,intp(1,4);

std :: cout<< p<< std :: endl;

}


实际上,即使添加了符号到std工作并且是可接受的,我想要知道为什么它是必要的。

Curiously, this works:

#include <iostream>

template < typename T >
std::ostream& operator<<(std::ostream& out, std::pair<T,T>& p) {
return out << p.first << p.second;
}
int main() {
std::pair<int,intp(1,4);
std::cout<<p<<std::endl;
}

In truth, even if adding the symbol to std works and is "acceptable", I
would like to know why it is necessary.


有一个

几年前对此进行了讨论,我确实记得双方

的论点,但是我不记得讨论是多么明确

是。
There was a
discussion about this a couple of years ago, and I do remember two sides
of the argument, however I can''t remember how definitive the discussion
was.



我遵从标准。

-

NOUN:1。遗嘱遗留给他人的金钱或财产。 2.从祖先或前任或过去那里下来的东西:b
宗教自由的遗产。 ETYMOLOGY:MidE legacie,副手办公室,来自OF,来自ML legatia的
,来自L legare,以及deute,遗赠。 www.bartleby.com/61/


这篇关于运算符&gt;&gt;需要在命名空间std ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆