[namespace.udir] (original) (raw)
9 Declarations [dcl.dcl]
9.8 Namespaces [basic.namespace]
9.8.3 Using namespace directive [namespace.udir]
using-directive: attribute-specifier-seq using namespace nested-name-specifier namespace-name ;
A using-directive shall not appear in class scope, but may appear in namespace scope or in block scope.
A using-directive specifies that the names in the nominated namespace can be used in the scope in which theusing-directive appears after the using-directive.
During unqualified name lookup ([basic.lookup.unqual]), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace.
[ Note
:
In this context, “contains” means “contains directly or indirectly”.
— end note
]
A using-directive does not add any members to the declarative region in which it appears.
[ Example
:
namespace A {
int i;
namespace B {
namespace C {
int i;
}
using namespace A::B::C;
void f1() {
i = 5;
}
}
namespace D {
using namespace B;
using namespace C;
void f2() {
i = 5;
}
}
void f3() {
i = 5;
}
}
void f4() {
i = 5;
}
— end example
]
For unqualified lookup ([basic.lookup.unqual]), theusing-directive is transitive: if a scope contains ausing-directive that nominates a second namespace that itself contains using-directives, the effect is as if theusing-directives from the second namespace also appeared in the first.
[ Example
:
namespace M { int i; }
namespace N { int i; using namespace M; }
void f() {
using namespace N;
i = 7;
}
For another example,
namespace A {
int i;
}
namespace B {
int i;
int j;
namespace C {
namespace D {
using namespace A;
int j;
int k;
int a = i;
}
using namespace D;
int k = 89;
int l = k;
int m = i;
int n = j;
}
}
— end example
]
[ Note
:
If name lookup finds a declaration for a name in two different namespaces, and the declarations do not declare the same entity and do not declare functions or function templates, the use of the name is ill-formed ([basic.lookup]).
In particular, the name of a variable, function or enumerator does not hide the name of a class or enumeration declared in a different namespace.
For example,
namespace A { class X { }; extern "C" int g(); extern "C++" int h(); } namespace B { void X(int); extern "C" int g(); extern "C++" int h(int); } using namespace A; using namespace B;
void f() {
X(1);
g();
h();
}
— end note
]
During overload resolution, all functions from the transitive search are considered for argument matching.
The set of declarations found by the transitive search is unordered.
[ Note
:
In particular, the order in which namespaces were considered and the relationships among the namespaces implied by theusing-directives do not cause preference to be given to any of the declarations found by the search.
— end note
]
An ambiguity exists if the best match finds two functions with the same signature, even if one is in a namespace reachable throughusing-directives in the namespace of the other.96
[ Example
:
namespace D { int d1; void f(char); } using namespace D;
int d1;
namespace E { int e; void f(int); }
namespace D {
int d2;
using namespace E;
void f(int);
}
void f() {
d1++;
::d1++;
D::d1++;
d2++;
e++;
f(1);
f('a');
}
— end example
]