Search code examples
c++c++11scopenamespacesusing

Difference in using namespace (std:: vs ::std::)


using ::std::...;

VS

using std::...;

Is there a difference(s)? If so, which one(s)?

I saw this:

using ::std::nullptr_t;

which made me wonder.


Solution

  • In your case, there is most likely no difference. However, generally, the difference is as follows:

    using A::foo; resolves A from the current scope, while using ::A::foo searches for A from the root namespace. For example:

    namespace A
    {
        namespace B
        {
             class C;
        }
    }
    namespace B
    { 
        class C;
    }
    namespace A
    {
        using B::C; // resolves to A::B::C
        using ::B::C; // resolves to B::C
        // (note that one of those using declarations has to be
        // commented for making this valid code!)
    }