c++namespacesstdnamespace-organisation

On namespace 'names': ::std:: vs std::


I have been looking over some posts here on Stackoverflow, and I have noticed that most people use std:: but some people uses ::std::

I think i have read something about a global scope or something like that in namespaces as a reason to use ::std:: (but i can't find it now, because it was in a comment to an unrelated question)

Is there any reason to prefer one way versus the other?


Solution

  • It's a bad idea to write code like this, but you could:

    namespace foo {
        namespace std {
            int bar;
        }
        std::string s;
    }
    

    In that case, the std::string refers to the ::foo::std namespace, not the ::std namespace. So, using ::std::string is just being a little bit more unambiguously careful.