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

149 views Asked by At

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?

1

There are 1 answers

2
Greg Hewgill On BEST ANSWER

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.