what is the difference between ::std::string
and std::string
The former is global? But global for what?Is not namespace std global? Thanks for helping me.
::std::string和std::string之间的区别是什么?但全球为了什么?名称空间std不是全局的吗?谢谢你帮助我。
1 个解决方案
#1
14
::std::string
means string
in namespace std
in the global namespace. The leading ::
forces the lookup to start in the global namespace. Therefore ::std::string
always means the string
type from the C++ standard library.
:::string表示全局名称空间中的名称空间std中的string。引导::强制查找在全局命名空间中启动。因此::std::string总是表示来自c++标准库的string类型。
std::string
means string
in namespace std
, where std
will be looked up in the current scope. Therefore, if there is a class, namespace, or enum named std
, name lookup might find that 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
}
}
}
It is not necessary to use the leading ::
as long as you and your collaborators agree to not name anything std
. It's just good style.
只要你和你的合作者同意不要说出任何std的名字,那就不是必要的了。
#1
14
::std::string
means string
in namespace std
in the global namespace. The leading ::
forces the lookup to start in the global namespace. Therefore ::std::string
always means the string
type from the C++ standard library.
:::string表示全局名称空间中的名称空间std中的string。引导::强制查找在全局命名空间中启动。因此::std::string总是表示来自c++标准库的string类型。
std::string
means string
in namespace std
, where std
will be looked up in the current scope. Therefore, if there is a class, namespace, or enum named std
, name lookup might find that 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
}
}
}
It is not necessary to use the leading ::
as long as you and your collaborators agree to not name anything std
. It's just good style.
只要你和你的合作者同意不要说出任何std的名字,那就不是必要的了。