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

15 KiB
Raw Permalink Blame History

[basic.lookup.qual]

6 Basics [basic]

6.5 Name lookup [basic.lookup]

6.5.5 Qualified name lookup [basic.lookup.qual]

6.5.5.1 General [basic.lookup.qual.general]

1

#

Lookup of an identifier followed by a :: scope resolution operator considers only namespaces, types, and templates whose specializations are types.

If a name,template-id,splice-scope-specifier, orcomputed-type-specifier is followed by a ::, it shall either be a dependent splice-scope-specifier ([temp.dep.splice]) or it shall designate a namespace, class, enumeration, or dependent type, and the :: is never interpreted as a complete nested-name-specifier.

[Example 1: class A {public:static int n;};int main() {int A; A::n = 42; // OK A b; // error: A does not name a type}template struct B : A {};namespace N {template void B(); int f() {return B<0>::n; // error: N::B<0> is not a type}} — end example]

2

#

A member-qualified name is the (unique) component name ([expr.prim.id.unqual]), if any, of

an unqualified-id or

a nested-name-specifier of the formtype-name :: or namespace-name ::

in the id-expression of a class member access expression ([expr.ref]).

A qualified name is

a member-qualified name or

the terminal name of

a qualified-id,

a using-declarator,

a typename-specifier,

a qualified-namespace-specifier, or

a nested-name-specifier,reflection-name,elaborated-type-specifier, orclass-or-decltype that has a nested-name-specifier ([expr.prim.id.qual]).

The lookup context of a member-qualified name is the type of its associated object expression (considered dependent if the object expression is type-dependent).

The lookup context of any other qualified name is the type, template, or namespace nominated by the preceding nested-name-specifier.

[Note 1:

When parsing a class member access, the name following the -> or . is a qualified name even though it is not yet known of which kind.

— end note]

[Example 2:

In N::C::m.Base::f()Base is a member-qualified name; the other qualified names are C, m, and f.

— end example]

3

#

Qualified name lookup in a class, namespace, or enumeration performs a search of the scope associated with it ([class.member.lookup]) except as specified below.

Unless otherwise specified, a qualified name undergoes qualified name lookup in its lookup context from the point where it appears unless the lookup context either is dependent and is not the current instantiation ([temp.dep.type]) or is not a class or class template.

If nothing is found by qualified lookup for a member-qualified name that is the terminal name ([expr.prim.id.unqual]) of a nested-name-specifier and is not dependent, it undergoes unqualified lookup.

[Note 2:

During lookup for a template specialization, no names are dependent.

— end note]

[Example 3: int f();struct A {int B, C; template using D = void; using T = void; void f();};using B = A;template using C = A;template using D = A;template using X = A;

templatevoid g(T p) { // as instantiated for g: p->X<0>::f(); // error: A::X not found in ((p->X) < 0) > ::f() p->template X<0>::f(); // OK, ::X found in definition context p->B::f(); // OK, non-type A::B ignored p->template C<0>::f(); // error: A::C is not a template p->template D<0>::f(); // error: A::D<0> is not a class type p->T::f(); // error: A::T is not a class type}template void g(A); — end example]

4

#

If a qualified name Q follows a ~:

  • (4.1)

    If Q is a member-qualified name, it undergoes unqualified lookup as well as qualified lookup.

  • (4.2)

    Otherwise, its nested-name-specifier N shall nominate a type. If N has another nested-name-specifier S,Q is looked up as if its lookup context were that nominated by S.

  • (4.3)

    Otherwise, if the terminal name of N is a member-qualified name M,Q is looked up as if ~Q appeared in place of M (as above).

  • (4.4)

    Otherwise, Q undergoes unqualified lookup.

  • (4.5)

    Each lookup for Q considers only types (if Q is not followed by a <) and templates whose specializations are types. If it finds nothing or is ambiguous, it is discarded.

  • (4.6)

    The type-name that is or contains Q shall refer to its (original) lookup context (ignoring cv-qualification) under the interpretation established by at least one (successful) lookup performed.

[Example 4: struct C {typedef int I;};typedef int I1, I2;extern int* p;extern int* q;void f() { p->C::I::~I(); // I is looked up in the scope of C q->I1::~I2(); // I2 is found by unqualified lookup}struct A {~A();};typedef A AB;int main() { AB* p; p->AB::~AB(); // explicitly calls the destructor for A} — end example]

6.5.5.2 Class members [class.qual]

1

#

In a lookup for a qualified name N whose lookup context is a class C in which function names are not ignored,18

if the search finds the injected-class-name of C ([class.pre]), or

if N is dependent and is the terminal name of a using-declarator ([namespace.udecl]) that names a constructor,

N is instead considered to name the constructor of class C.

Such a constructor name shall be used only in the declarator-id of a (friend) declaration of a constructor or in a using-declaration.

[Example 1: struct A { A(); };struct B: public A { B(); };

A::A() { } B::B() { } B::A ba; // object of type A A::A a; // error: A::A is not a type namestruct A::A a2; // object of type A — end example]

18)18)

Lookups in which function names are ignored include names appearing in anested-name-specifier, anelaborated-type-specifier, or a base-specifier.

6.5.5.3 Namespace members [namespace.qual]

1

#

Qualified name lookup in a namespace N additionally searches every element of the inline namespace set of N ([namespace.def]).

If nothing is found, the results of the lookup are the results of qualified name lookup in each namespace nominated by a using-directive that precedes the point of the lookup and inhabits N or an element of N's inline namespace set.

[Note 1:

If a using-directive refers to a namespace that has already been considered, it does not affect the result.

— end note]

[Example 1: int x;namespace Y {void f(float); void h(int);}namespace Z {void h(double);}namespace A {using namespace Y; void f(int); void g(int); int i;}namespace B {using namespace Z; void f(char); int i;}namespace AB {using namespace A; using namespace B; void g();}void h(){ AB::g(); // g is declared directly in AB, therefore S is { AB::g() } and AB::g() is chosen AB::f(1); // f is not declared directly in AB so the rules are applied recursively to A and B;// namespace Y is not searched and Y::f(float) is not considered;// S is { A::f(int), B::f(char) } and overload resolution chooses A::f(int) AB::f('c'); // as above but resolution chooses B::f(char) AB::x++; // x is not declared directly in AB, and is not declared in A or B, so the rules// are applied recursively to Y and Z, S is { } so the program is ill-formed AB::i++; // i is not declared directly in AB so the rules are applied recursively to A and B,// S is { A::i, B::i } so the use is ambiguous and the program is ill-formed AB::h(16.8); // h is not declared directly in AB and not declared directly in A or B so the rules// are applied recursively to Y and Z, S is { Y::h(int), Z::h(double) } and// overload resolution chooses Z::h(double)} — end example]

2

#

[Note 2:

The same declaration found more than once is not an ambiguity (because it is still a unique declaration).

[Example 2: namespace A {int a;}namespace B {using namespace A;}namespace C {using namespace A;}namespace BC {using namespace B; using namespace C;}void f(){ BC::a++; // OK, S is { A::a, A::a }}namespace D {using A::a;}namespace BD {using namespace B; using namespace D;}void g(){ BD::a++; // OK, S is { A::a, A::a }} — end example]

— end note]

3

#

[Example 3:

Because each referenced namespace is searched at most once, the following is well-defined:namespace B {int b;}namespace A {using namespace B; int a;}namespace B {using namespace A;}void f(){ A::a++; // OK, a declared directly in A, S is { A::a } B::a++; // OK, both A and B searched (once), S is { A::a } A::b++; // OK, both A and B searched (once), S is { B::b } B::b++; // OK, b declared directly in B, S is { B::b }}

— end example]

4

#

[Note 3:

Class and enumeration declarations are not discarded because of other declarations found in other searches.

— end note]

[Example 4: namespace A {struct x { }; int x; int y;}namespace B {struct y { };}namespace C {using namespace A; using namespace B; int i = C::x; // OK, A::x (of type int)int j = C::y; // ambiguous, A::y or B::y} — end example]