Files
2025-10-25 03:02:53 +03:00

8.8 KiB
Raw Permalink Blame History

[namespace.udir]

9 Declarations [dcl]

9.9 Namespaces [basic.namespace]

9.9.4 Using namespace directive [namespace.udir]

using-directive:
attribute-specifier-seqopt using namespace nested-name-specifieropt namespace-name ;
attribute-specifier-seqopt using namespace splice-specifier ;

1

#

The splice-specifier (if any) shall designate a namespace that is not the global namespace.

The nested-name-specifier,namespace-name, andsplice-specifier shall not be dependent.

2

#

A using-directive shall not appear in class scope, but may appear in namespace scope or in block scope.

[Note 1:

When looking up a namespace-name in ausing-directive, only namespace names are considered, see [basic.lookup.udir].

— end note]

The optional attribute-specifier-seq appertains to the using-directive.

3

#

[Note 2:

A using-directive makes the names in the designated namespace usable in the scope in which theusing-directive appears after the using-directive ([basic.lookup.unqual], [namespace.qual]).

During unqualified name lookup, the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the designated namespace.

— end note]

4

#

[Note 3:

A using-directive does not introduce any names.

— end note]

[Example 1: namespace A {int i; namespace B {namespace C {int i; }using namespace A::B::C; void f1() { i = 5; // OK, C::i visible in B and hides A::i}}namespace D {using namespace B; using namespace C; void f2() { i = 5; // ambiguous, B::C::i or A::i?}}void f3() { i = 5; // uses A::i}}void f4() { i = 5; // error: neither i is visible} — end example]

5

#

[Note 4:

A using-directive is transitive: if a scope contains ausing-directive that designates a namespace that itself contains using-directives, the namespaces designated by thoseusing-directives are also eligible to be considered.

— end note]

[Example 2: namespace M {int i;}namespace N {int i; using namespace M;}void f() {using namespace N; i = 7; // error: both M::i and N::i are visible}

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; // B::i hides A::i}using namespace D; int k = 89; // no problem yetint l = k; // ambiguous: C::k or D::kint m = i; // B::i hides A::iint n = j; // D::j hides B::j}}

— end example]

6

#

[Note 5:

Declarations in a namespace that appear after a using-directive for that namespace can be found through that using-directive after they appear.

— end note]

7

#

[Note 6:

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); // error: name X found in two namespaces g(); // OK, name g refers to the same entity h(); // OK, overload resolution selects A::h}

— end note]

8

#

[Note 7:

The order in which namespaces are considered and the relationships among the namespaces implied by theusing-directives do not affect overload resolution.

Neither is any function excluded because another has the same signature, even if one is in a namespace reachable throughusing-directives in the namespace of the other.83

— end note]

[Example 3: namespace D {int d1; void f(char);}using namespace D;

int d1; // OK, no conflict with D::d1namespace E {int e; void f(int);}namespace D { // namespace extensionint d2; using namespace E; void f(int);}void f() { d1++; // error: ambiguous ::d1 or D::d1?::d1++; // OK D::d1++; // OK d2++; // OK, D::d2 e++; // OK, E::e f(1); // error: ambiguous: D::f(int) or E::f(int)? f('a'); // OK, D::f(char)} — end example]

83)83)

During name lookup in a class hierarchy, some ambiguities can be resolved by considering whether one member hides the other along some paths ([class.member.lookup]).

There is no such disambiguation when considering the set of names found as a result of followingusing-directives.