8

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 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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.