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.
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.
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!)
}
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.
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>
}
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
.