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.
1 Answer
::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.
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.
#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.