[英]What is the difference between ::std::string and std::string?
::std::string
和std::string
什么区别前者是全局的? 但全局是什么?不是命名空间std全球? 谢谢你的帮助。
::std::string
表示全局命名空间中命名空间std
中的string
。 leading ::
强制查找在全局命名空间中开始。 因此::std::string
总是表示C ++标准库中的string
类型。
std::string
表示命名空间std
string
,其中std
将在当前范围中查找。 因此,如果存在名为std
的类,名称空间或枚举,则名称查找可能会找到该std
。
#include <string>
namespace foo {
namespace std {
class string { ... };
namespace bar {
std::string s; // means foo::std::string
::std::string s; // means string from standard library
}
}
}
只要您和您的合作者同意不指定任何std
,就没有必要使用前导::
这只是一种很好的风格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.