Search code examples
c++namespacesstdusing

C++ using namespace declaration


So for a while I've been using...

using namespace::std;

and recently I realized that's supposed to be

using namespace std;

Can somebody explain to me why what I was doing worked, and how it differs from the correct way of declaring the usage of a certain namespace?


Solution

  • The first version works because the compiler sees it as

    using namespace ::std;
    // Notice space^
    

    That is simply telling the compiler to look for the name-space std in the global scope.

    The scope operator :: without anything on the left-hand side is the same as the global scope.