Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in using namespace (std:: vs ::std::) [duplicate]

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.

like image 674
gsamaras Avatar asked Oct 13 '15 12:10

gsamaras


People also ask

Why is using namespace std not recommended?

While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.

What is the difference between use namespace std in C++?

The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.

Why should we use using namespace std in C++?

using namespace std; are used. It is because computer needs to know the code for the cout, cin functionalities and it needs to know which namespace they are defined.


3 Answers

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!)
}
like image 77
anderas Avatar answered Oct 20 '22 09:10

anderas


If you are inside another namespace that has its own nested std namespace, then ::std and std are different. A simple example:

#include <iostream>

namespace A {
    namespace std {
        void foo() { ::std::cout << "foo" << ::std::endl;}
    }
    //using std::cout; // compile error
    using ::std::cout; //ok

    using std::foo; // ok
    //using ::std::foo; // compile error
}

Though definitely not a good practice to ever have a nested std namespace.

like image 13
Petr Avatar answered Oct 20 '22 11:10

Petr


From: http://en.cppreference.com/w/cpp/language/using_declaration

Using-declaration introduces a member of another namespace into current namespace or block scope

Therefore, if your current scope already has a class with the same name, there will be an ambiguity between the one you introduced and the one in your current namespace/block.

A using declaration is just a subset of a using directive. The using directives is defined as follows (http://en.cppreference.com/w/cpp/language/namespace):

From the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from namespace-name is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and namespace-name.

Thus, you can consider these two examples that display the issues that can arise.

It prevents ambiguity between namespaces that share the same name (example 1) as well as ambiguity between class names in different namespaces (example 2).

namespace A
{
    namespace B
    {
        struct my_struct {};
    }
}

namespace B
{
    struct my_struct {};
}

using namespace A; // removing this line makes B:: resolve to the global B::

int main()
{
    ::B::my_struct; // from global, will not pick A::B::

    B::my_struct; // error: 'B' is ambiguous, there is A::B:: and B::
}

Consider this example which showcases why people shun the use of using namespace std;

using namespace std;

template <typename T>
class vector
{ };

int main()
{
    vector<int> v; // which one did you want? ambiguous
    ::vector<int> v_global;     // global one
    ::std::vector<int> v_std;   // std::vector<T>
}
like image 9
bku_drytt Avatar answered Oct 20 '22 10:10

bku_drytt