Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by chue x, Roddy of the Frozen Peas, Deduplicator c++ yesterday

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

11  
You need to write ::std::nullptr_t only if someone else adds another std::nullptr_t to a nested namespace inside your project. Or you could let your manager have a serious talk to that guy. – Bo Persson yesterday
3  
BTW a namespace C14_compatibility { namespace std { template <typename T> using decay_t = typename decay<T>::type; }} and using namespace C14_compatibility; seems a possible usage. – Jarod42 yesterday
    
Or indeed, when your compiler doesn't support the C++ standard you want to use, or it does but you need your own implementation in some places. – OrangeDog yesterday
1  
@chuex agree, but the question itself and the answers seem a little bit better here. (I probably would have written the same even if I hadn't written the accepted answer. At least I hope so...) – anderas yesterday
1  
@gsamaras: Kind of a shame you deleted your Meta question...but he's not calling you stupid. I wouldn't bother with this personally; don't take offense to it. – Makoto 25 mins ago

4 Answers 4

up vote 24 down vote accepted

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!)
}
share|improve this answer
2  
This difference is very important when writing macros. A macro is expanded wherever the developer uses it. The macro cannot assume std goes exactly where it wants, so macros will often use ::std to ensure that the macro does not have any unexpected surprises. – Cort Ammon yesterday

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.

share|improve this answer
    
So ::std:: says to take the more global namespace of std? Oh, Andreas makes it clear in his answer, thanks though, +1. – gsamaras yesterday
3  
@gsamaras, not only the more global, but the (only) one that is absolutely global, not nested anywhere. – Petr yesterday

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>
}
share|improve this answer
    
How would the using directive be used in this case to make it consistent with the question? – dspfnder yesterday
    
Generally this is true, but this question is about using types from a namespace, not the whole namespace! – anderas yesterday
    
@anderas The difference between a using declaration and a using directive isn't that big. You can simply see it as one introduces a single type into the current scope and the other introduces all types into the current scope. – bku_drytt yesterday

It depends on where you use the using declaration. On a global namespace scope there will be no difference. However, if you have code like

#include <iostream>
#include <vector>

namespace my_namespace {
    namespace std {
        class vector {
        };
    }

    using std::vector;
}

int main()
{
    my_namespace::vector<int> v;
}

it will not compile unless you inform the compiler to search the global namespace -> std namespace -> vector in your declaration by stating using ::std::vector.

share|improve this answer
2  
On the other hand, people adding an extra namespace std to the source deserve to get fired. That's much easier than having everyone else write ::std:: all over the place. – Bo Persson yesterday
    
@BoPersson Of course :) I was just illustrating the difference. It's probably just developer's preference to write ::std::.... – Rostislav yesterday
    
@BoPersson: I added comment in OP's question which seems a viable reason to have namespace std inside user namespace. – Jarod42 yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.