[英]What's the difference between using std::cout and using ::std::cout?
As we already know ::
is global namespace. 我们已经知道
::
是全局名称空间。 And we have already know that using namespace has some downsides. 而我们已经知道的是使用命名空间有一些缺点。
So we decide to type less and type in the beggining of our file using ::std::cout;
因此,我们决定减少键入,然后
using ::std::cout;
文件的开头using ::std::cout;
, but we can also type using std::cout;
,但我们也可以
using std::cout;
. 。 So is there any difference?
那有什么区别吗?
Some people write ::std::cout
in order to be absolutely sure that they're getting the cout
from the standard library, since the leading ::
roots the type at the global namespace. 有些人写
::std::cout
以绝对确保他们得到的cout
标准库中,由于领导::
根在全局命名空间的类型。
Otherwise it's conceivable that lookup could find some ::haha::trolling::std::cout
name. 否则,可以想到查找可以找到
::haha::trolling::std::cout
名称。 However, I'd say that this is unlikely. 但是,我想这不太可能。
I do sometimes write ::std
in short function templates that are designed to be highly reusable, just for maximum portability, but it's fairly uncommon. 我有时会在简短的函数模板中编写
::std
,这些模板被设计为具有高度可重用性,以实现最大的可移植性,但这并不常见。 I've seen one SO contributor who stuck to it religiously, but that's about it. 我见过一位SO贡献者虔诚地坚持下去,但仅此而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.