35
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.

anderas
  • 5,744
  • 30
  • 49
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 13
    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 Oct 13 '15 at 12:18
  • 3
    BTW a `namespace C14_compatibility { namespace std { template using decay_t = typename decay::type; }}` and `using namespace C14_compatibility;` seems a possible usage. – Jarod42 Oct 13 '15 at 12:31
  • 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 Oct 13 '15 at 15:07
  • 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 Oct 13 '15 at 16:03
  • 22 of you think this was well researched. The [censored] guys? – Alec Teal Oct 14 '15 at 16:36
  • Well I searched a lot the site @AlecTeal, but I failed to find the duplicate! – gsamaras Oct 14 '15 at 21:49
  • @gsamaras if you really couldn't find info on this it is because the question is so stupid no one thought to ask. It'd be like asking "what's the difference between some/file.thing and /some/file.thing" - quite literally. – Alec Teal Oct 14 '15 at 22:16
  • Thanks for calling me stupid. Well at least I am not the only one, as the duplicate marks. – gsamaras Oct 14 '15 at 23:20
  • 2
    @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 Oct 14 '15 at 23:25
  • I saw a storm of downvotes, so I thought it was a bad question @Makoto. Yes, you are right! – gsamaras Oct 14 '15 at 23:31
  • Two downvotes is hardly a storm... But okay then. – Makoto Oct 14 '15 at 23:31
  • @Makoto I got two downvotes after I posted it, I thought it was the beginning of one. :) – gsamaras Oct 15 '15 at 10:25

4 Answers4

42

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!)
}
anderas
  • 5,744
  • 30
  • 49
  • 4
    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 Oct 13 '15 at 18:32
13

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.

Petr
  • 9,812
  • 1
  • 28
  • 52
  • So `::std::` says to take the more global namespace of std? Oh, Andreas makes it clear in his answer, thanks though, +1. – gsamaras Oct 13 '15 at 12:14
  • 4
    @gsamaras, not only the more global, but the (only) one that is absolutely global, not nested anywhere. – Petr Oct 13 '15 at 12:15
9

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>
}
bku_drytt
  • 3,169
  • 17
  • 19
  • How would the using directive be used in this case to make it consistent with the question? – dspfnder Oct 13 '15 at 12:23
  • Generally this is true, but this question is about `using` types from a namespace, not the whole namespace! – anderas Oct 13 '15 at 12:23
  • @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 Oct 13 '15 at 12:43
5

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.

Rostislav
  • 3,857
  • 18
  • 30
  • 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 Oct 13 '15 at 12:15
  • @BoPersson Of course :) I was just illustrating the difference. It's probably just developer's preference to write `::std::...`. – Rostislav Oct 13 '15 at 12:20
  • @BoPersson: I added comment in OP's question which seems a viable reason to have `namespace std` inside user namespace. – Jarod42 Oct 13 '15 at 12:34