From Jason Turner

[basic.lookup.qual]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp6n6_5ebr/{from.md → to.md} +103 -62
tmp/tmp6n6_5ebr/{from.md → to.md} RENAMED
@@ -8,10 +8,12 @@ enumeration. If a `::` scope resolution operator in a
8
  lookup of the name preceding that `::` considers only namespaces, types,
9
  and templates whose specializations are types. If the name found does
10
  not designate a namespace or a class, enumeration, or dependent type,
11
  the program is ill-formed.
12
 
 
 
13
  ``` cpp
14
  class A {
15
  public:
16
  static int n;
17
  };
@@ -20,31 +22,37 @@ int main() {
20
  A::n = 42; // OK
21
  A b; // ill-formed: A does not name a type
22
  }
23
  ```
24
 
25
- Multiply qualified names, such as `N1::N2::N3::n`, can be used to refer
26
- to members of nested classes ([[class.nest]]) or members of nested
27
- namespaces.
 
 
28
 
29
  In a declaration in which the *declarator-id* is a *qualified-id*, names
30
  used before the *qualified-id* being declared are looked up in the
31
  defining namespace scope; names following the *qualified-id* are looked
32
  up in the scope of the member’s class or namespace.
33
 
 
 
34
  ``` cpp
35
  class X { };
36
  class C {
37
  class X { };
38
  static const int number = 50;
39
  static X arr[number];
40
  };
41
  X C::arr[number]; // ill-formed:
42
- // equivalent to: ::X C::arr[C::number];
43
- // not to: C::X C::arr[C::number];
44
  ```
45
 
 
 
46
  A name prefixed by the unary scope operator `::` ([[expr.prim]]) is
47
  looked up in global scope, in the translation unit where it is used. The
48
  name shall be declared in global namespace scope or shall be a name
49
  whose declaration is visible in global scope because of a
50
  *using-directive* ([[namespace.qual]]). The use of `::` allows a global
@@ -63,20 +71,21 @@ scope designated by the *nested-name-specifier*. Similarly, in a
63
  nested-name-specifierₒₚₜ class-name '::' '~' class-name
64
  ```
65
 
66
  the second *class-name* is looked up in the same scope as the first.
67
 
 
 
68
  ``` cpp
69
  struct C {
70
  typedef int I;
71
  };
72
  typedef int I1, I2;
73
  extern int* p;
74
  extern int* q;
75
  p->C::I::~I(); // I is looked up in the scope of C
76
- q->I1::~I2(); // I2 is looked up in the scope of
77
- // the postfix-expression
78
 
79
  struct A {
80
  ~A();
81
  };
82
  typedef A AB;
@@ -84,25 +93,30 @@ int main() {
84
  AB* p;
85
  p->AB::~AB(); // explicitly calls the destructor for A
86
  }
87
  ```
88
 
89
- [[basic.lookup.classref]] describes how name lookup proceeds after the
90
- `.` and `->` operators.
 
 
91
 
92
  #### Class members <a id="class.qual">[[class.qual]]</a>
93
 
94
  If the *nested-name-specifier* of a *qualified-id* nominates a class,
95
  the name specified after the *nested-name-specifier* is looked up in the
96
  scope of the class ([[class.member.lookup]]), except for the cases
97
  listed below. The name shall represent one or more members of that class
98
- or of one of its base classes (Clause  [[class.derived]]). A class
99
- member can be referred to using a *qualified-id* at any point in its
100
- potential scope ([[basic.scope.class]]). The exceptions to the name
101
- lookup rule above are the following:
102
 
103
- - a destructor name is looked up as specified in  [[basic.lookup.qual]];
 
 
 
 
 
 
104
  - a *conversion-type-id* of a *conversion-function-id* is looked up in
105
  the same manner as a *conversion-type-id* in a class member access
106
  (see  [[basic.lookup.classref]]);
107
  - the names in a *template-argument* of a *template-id* are looked up in
108
  the context in which the entire *postfix-expression* occurs.
@@ -113,22 +127,26 @@ lookup rule above are the following:
113
  In a lookup in which function names are not ignored[^9] and the
114
  *nested-name-specifier* nominates a class `C`:
115
 
116
  - if the name specified after the *nested-name-specifier*, when looked
117
  up in `C`, is the injected-class-name of `C` (Clause  [[class]]), or
118
- - in a *using-declaration* ([[namespace.udecl]]) that is a
119
- *member-declaration*, if the name specified after the
120
- *nested-name-specifier* is the same as the *identifier* or the
121
- *simple-template-id*’s *template-name* in the last component of the
122
- *nested-name-specifier*,
123
-
124
- the name is instead considered to name the constructor of class `C`. For
125
- example, the constructor is not an acceptable lookup result in an
126
- *elaborated-type-specifier* so the constructor would not be used in
127
- place of the injected-class-name. Such a constructor name shall be used
128
- only in the *declarator-id* of a declaration that names a constructor or
129
- in a *using-declaration*.
 
 
 
 
130
 
131
  ``` cpp
132
  struct A { A(); };
133
  struct B: public A { B(); };
134
 
@@ -138,39 +156,42 @@ B::B() { }
138
  B::A ba; // object of type A
139
  A::A a; // error, A::A is not a type name
140
  struct A::A a2; // object of type A
141
  ```
142
 
 
 
143
  A class member name hidden by a name in a nested declarative region or
144
  by the name of a derived class member can still be found if qualified by
145
  the name of its class followed by the `::` operator.
146
 
147
  #### Namespace members <a id="namespace.qual">[[namespace.qual]]</a>
148
 
149
- If the *nested-name-specifier* of a *qualified-id* nominates a
150
- namespace, the name specified after the *nested-name-specifier* is
151
- looked up in the scope of the namespace. If a *qualified-id* starts with
152
- `::`, the name after the `::` is looked up in the global namespace. In
153
- either case, the names in a *template-argument* of a *template-id* are
154
- looked up in the context in which the entire *postfix-expression*
155
- occurs.
156
 
157
  For a namespace `X` and name `m`, the namespace-qualified lookup set
158
  S(X, m) is defined as follows: Let S'(X, m) be the set of all
159
  declarations of `m` in `X` and the inline namespace set of `X` (
160
  [[namespace.def]]). If S'(X, m) is not empty, S(X, m) is S'(X, m);
161
  otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
162
- nominated by *using-directives* in `X` and its inline namespace set.
163
 
164
  Given `X::m` (where `X` is a user-declared namespace), or given `::m`
165
  (where X is the global namespace), if S(X, m) is the empty set, the
166
  program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
167
  if the context of the reference is a *using-declaration* (
168
  [[namespace.udecl]]), S(X, m) is the required set of declarations of
169
  `m`. Otherwise if the use of `m` is not one that allows a unique
170
  declaration to be chosen from S(X, m), the program is ill-formed.
171
 
 
 
172
  ``` cpp
173
  int x;
174
  namespace Y {
175
  void f(float);
176
  void h(int);
@@ -199,38 +220,38 @@ namespace AB {
199
  void g();
200
  }
201
 
202
  void h()
203
  {
204
- AB::g(); // g is declared directly in AB,
205
- // therefore S is { AB::g() } and AB::g() is chosen
206
- AB::f(1); // f is not declared directly in AB so the rules are
207
- // applied recursively to A and B;
208
- // namespace Y is not searched and Y::f(float)
209
- // is not considered;
210
- // S is { A::f(int), B::f(char) } and overload
211
- // resolution chooses A::f(int)
212
  AB::f('c'); // as above but resolution chooses B::f(char)
213
 
214
- AB::x++; // x is not declared directly in AB, and
215
- // is not declared in A or B , so the rules are
216
- // applied recursively to Y and Z,
217
- // S is { } so the program is ill-formed
218
- AB::i++; // i is not declared directly in AB so the rules are
219
- // applied recursively to A and B,
220
- // S is { A::i , B::i } so the use is ambiguous
221
- // and the program is ill-formed
222
- AB::h(16.8); // h is not declared directly in AB and
223
- // not declared directly in A or B so the rules are
224
- // applied recursively to Y and Z,
225
- // S is { Y::h(int), Z::h(double) } and overload
226
- // resolution chooses Z::h(double)
227
  }
228
  ```
229
 
 
 
 
 
230
  The same declaration found more than once is not an ambiguity (because
231
- it is still a unique declaration). For example:
 
 
232
 
233
  ``` cpp
234
  namespace A {
235
  int a;
236
  }
@@ -248,11 +269,11 @@ namespace BC {
248
  using namespace C;
249
  }
250
 
251
  void f()
252
  {
253
- BC::a++; // OK: S is { A::a, A::a }
254
  }
255
 
256
  namespace D {
257
  using A::a;
258
  }
@@ -262,14 +283,20 @@ namespace BD {
262
  using namespace D;
263
  }
264
 
265
  void g()
266
  {
267
- BD::a++; // OK: S is { A::a, A::a }
268
  }
269
  ```
270
 
 
 
 
 
 
 
271
  Because each referenced namespace is searched at most once, the
272
  following is well-defined:
273
 
274
  ``` cpp
275
  namespace B {
@@ -285,25 +312,29 @@ namespace B {
285
  using namespace A;
286
  }
287
 
288
  void f()
289
  {
290
- A::a++; // OK: a declared directly in A, S is {A::a}
291
- B::a++; // OK: both A and B searched (once), S is {A::a}
292
- A::b++; // OK: both A and B searched (once), S is {B::b}
293
- B::b++; // OK: b declared directly in B, S is {B::b}
294
  }
295
  ```
296
 
 
 
297
  During the lookup of a qualified namespace member name, if the lookup
298
  finds more than one declaration of the member, and if one declaration
299
  introduces a class name or enumeration name and the other declarations
300
  either introduce the same variable, the same enumerator or a set of
301
  functions, the non-type name hides the class or enumeration name if and
302
  only if the declarations are from the same namespace; otherwise (the
303
  declarations are from different namespaces), the program is ill-formed.
304
 
 
 
305
  ``` cpp
306
  namespace A {
307
  struct x { };
308
  int x;
309
  int y;
@@ -319,10 +350,12 @@ namespace C {
319
  int i = C::x; // OK, A::x (of type int)
320
  int j = C::y; // ambiguous, A::y or B::y
321
  }
322
  ```
323
 
 
 
324
  In a declaration for a namespace member in which the *declarator-id* is
325
  a *qualified-id*, given that the *qualified-id* for the namespace member
326
  has the form
327
 
328
  ``` bnf
@@ -331,24 +364,30 @@ nested-name-specifier unqualified-id
331
 
332
  the *unqualified-id* shall name a member of the namespace designated by
333
  the *nested-name-specifier* or of an element of the inline namespace
334
  set ([[namespace.def]]) of that namespace.
335
 
 
 
336
  ``` cpp
337
  namespace A {
338
  namespace B {
339
  void f1(int);
340
  }
341
  using namespace B;
342
  }
343
  void A::f1(int){ } // ill-formed, f1 is not a member of A
344
  ```
345
 
 
 
346
  However, in such namespace member declarations, the
347
  *nested-name-specifier* may rely on *using-directive*s to implicitly
348
  provide the initial part of the *nested-name-specifier*.
349
 
 
 
350
  ``` cpp
351
  namespace A {
352
  namespace B {
353
  void f1(int);
354
  }
@@ -363,5 +402,7 @@ namespace C {
363
  using namespace A;
364
  using namespace C::D;
365
  void B::f1(int){ } // OK, defines A::B::f1(int)
366
  ```
367
 
 
 
 
8
  lookup of the name preceding that `::` considers only namespaces, types,
9
  and templates whose specializations are types. If the name found does
10
  not designate a namespace or a class, enumeration, or dependent type,
11
  the program is ill-formed.
12
 
13
+ [*Example 1*:
14
+
15
  ``` cpp
16
  class A {
17
  public:
18
  static int n;
19
  };
 
22
  A::n = 42; // OK
23
  A b; // ill-formed: A does not name a type
24
  }
25
  ```
26
 
27
+ *end example*]
28
+
29
+ [*Note 1*: Multiply qualified names, such as `N1::N2::N3::n`, can be
30
+ used to refer to members of nested classes ([[class.nest]]) or members
31
+ of nested namespaces. — *end note*]
32
 
33
  In a declaration in which the *declarator-id* is a *qualified-id*, names
34
  used before the *qualified-id* being declared are looked up in the
35
  defining namespace scope; names following the *qualified-id* are looked
36
  up in the scope of the member’s class or namespace.
37
 
38
+ [*Example 2*:
39
+
40
  ``` cpp
41
  class X { };
42
  class C {
43
  class X { };
44
  static const int number = 50;
45
  static X arr[number];
46
  };
47
  X C::arr[number]; // ill-formed:
48
+ // equivalent to ::X C::arr[C::number];
49
+ // and not to C::X C::arr[C::number];
50
  ```
51
 
52
+ — *end example*]
53
+
54
  A name prefixed by the unary scope operator `::` ([[expr.prim]]) is
55
  looked up in global scope, in the translation unit where it is used. The
56
  name shall be declared in global namespace scope or shall be a name
57
  whose declaration is visible in global scope because of a
58
  *using-directive* ([[namespace.qual]]). The use of `::` allows a global
 
71
  nested-name-specifierₒₚₜ class-name '::' '~' class-name
72
  ```
73
 
74
  the second *class-name* is looked up in the same scope as the first.
75
 
76
+ [*Example 3*:
77
+
78
  ``` cpp
79
  struct C {
80
  typedef int I;
81
  };
82
  typedef int I1, I2;
83
  extern int* p;
84
  extern int* q;
85
  p->C::I::~I(); // I is looked up in the scope of C
86
+ q->I1::~I2(); // I2 is looked up in the scope of the postfix-expression
 
87
 
88
  struct A {
89
  ~A();
90
  };
91
  typedef A AB;
 
93
  AB* p;
94
  p->AB::~AB(); // explicitly calls the destructor for A
95
  }
96
  ```
97
 
98
+ *end example*]
99
+
100
+ [*Note 2*: [[basic.lookup.classref]] describes how name lookup
101
+ proceeds after the `.` and `->` operators. — *end note*]
102
 
103
  #### Class members <a id="class.qual">[[class.qual]]</a>
104
 
105
  If the *nested-name-specifier* of a *qualified-id* nominates a class,
106
  the name specified after the *nested-name-specifier* is looked up in the
107
  scope of the class ([[class.member.lookup]]), except for the cases
108
  listed below. The name shall represent one or more members of that class
109
+ or of one of its base classes (Clause  [[class.derived]]).
 
 
 
110
 
111
+ [*Note 1*: A class member can be referred to using a *qualified-id* at
112
+ any point in its potential scope (
113
+ [[basic.scope.class]]). — *end note*]
114
+
115
+ The exceptions to the name lookup rule above are the following:
116
+
117
+ - the lookup for a destructor is as specified in  [[basic.lookup.qual]];
118
  - a *conversion-type-id* of a *conversion-function-id* is looked up in
119
  the same manner as a *conversion-type-id* in a class member access
120
  (see  [[basic.lookup.classref]]);
121
  - the names in a *template-argument* of a *template-id* are looked up in
122
  the context in which the entire *postfix-expression* occurs.
 
127
  In a lookup in which function names are not ignored[^9] and the
128
  *nested-name-specifier* nominates a class `C`:
129
 
130
  - if the name specified after the *nested-name-specifier*, when looked
131
  up in `C`, is the injected-class-name of `C` (Clause  [[class]]), or
132
+ - in a *using-declarator* of a *using-declaration* (
133
+ [[namespace.udecl]]) that is a *member-declaration*, if the name
134
+ specified after the *nested-name-specifier* is the same as the
135
+ *identifier* or the *simple-template-id*’s *template-name* in the last
136
+ component of the *nested-name-specifier*,
137
+
138
+ the name is instead considered to name the constructor of class `C`.
139
+
140
+ [*Note 2*: For example, the constructor is not an acceptable lookup
141
+ result in an *elaborated-type-specifier* so the constructor would not be
142
+ used in place of the injected-class-name. *end note*]
143
+
144
+ Such a constructor name shall be used only in the *declarator-id* of a
145
+ declaration that names a constructor or in a *using-declaration*.
146
+
147
+ [*Example 1*:
148
 
149
  ``` cpp
150
  struct A { A(); };
151
  struct B: public A { B(); };
152
 
 
156
  B::A ba; // object of type A
157
  A::A a; // error, A::A is not a type name
158
  struct A::A a2; // object of type A
159
  ```
160
 
161
+ — *end example*]
162
+
163
  A class member name hidden by a name in a nested declarative region or
164
  by the name of a derived class member can still be found if qualified by
165
  the name of its class followed by the `::` operator.
166
 
167
  #### Namespace members <a id="namespace.qual">[[namespace.qual]]</a>
168
 
169
+ If the *nested-name-specifier* of a *qualified-id* nominates a namespace
170
+ (including the case where the *nested-name-specifier* is `::`, i.e.,
171
+ nominating the global namespace), the name specified after the
172
+ *nested-name-specifier* is looked up in the scope of the namespace. The
173
+ names in a *template-argument* of a *template-id* are looked up in the
174
+ context in which the entire *postfix-expression* occurs.
 
175
 
176
  For a namespace `X` and name `m`, the namespace-qualified lookup set
177
  S(X, m) is defined as follows: Let S'(X, m) be the set of all
178
  declarations of `m` in `X` and the inline namespace set of `X` (
179
  [[namespace.def]]). If S'(X, m) is not empty, S(X, m) is S'(X, m);
180
  otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
181
+ nominated by *using-directive*s in `X` and its inline namespace set.
182
 
183
  Given `X::m` (where `X` is a user-declared namespace), or given `::m`
184
  (where X is the global namespace), if S(X, m) is the empty set, the
185
  program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
186
  if the context of the reference is a *using-declaration* (
187
  [[namespace.udecl]]), S(X, m) is the required set of declarations of
188
  `m`. Otherwise if the use of `m` is not one that allows a unique
189
  declaration to be chosen from S(X, m), the program is ill-formed.
190
 
191
+ [*Example 1*:
192
+
193
  ``` cpp
194
  int x;
195
  namespace Y {
196
  void f(float);
197
  void h(int);
 
220
  void g();
221
  }
222
 
223
  void h()
224
  {
225
+ AB::g(); // g is declared directly in AB, therefore S is { `AB::g()` } and AB::g() is chosen
226
+
227
+ AB::f(1); // f is not declared directly in AB so the rules are applied recursively to A and B;
228
+ // namespace Y is not searched and Y::f(float) is not considered;
229
+ // S is { `A::f(int)`, `B::f(char)` } and overload resolution chooses A::f(int)
230
+
 
 
231
  AB::f('c'); // as above but resolution chooses B::f(char)
232
 
233
+ AB::x++; // x is not declared directly in AB, and is not declared in A or B, so the rules
234
+ // are applied recursively to Y and Z, S is { } so the program is ill-formed
235
+
236
+ AB::i++; // i is not declared directly in AB so the rules are applied recursively to A and B,
237
+ // S is { `A::i`, `B::i` } so the use is ambiguous and the program is ill-formed
238
+
239
+ AB::h(16.8); // h is not declared directly in AB and not declared directly in A or B so the rules
240
+ // are applied recursively to Y and Z, S is { `Y::h(int)`, `Z::h(double)` } and
241
+ // overload resolution chooses Z::h(double)
 
 
 
 
242
  }
243
  ```
244
 
245
+ — *end example*]
246
+
247
+ [*Note 1*:
248
+
249
  The same declaration found more than once is not an ambiguity (because
250
+ it is still a unique declaration).
251
+
252
+ [*Example 2*:
253
 
254
  ``` cpp
255
  namespace A {
256
  int a;
257
  }
 
269
  using namespace C;
270
  }
271
 
272
  void f()
273
  {
274
+ BC::a++; // OK: S is { `A::a`, `A::a` }
275
  }
276
 
277
  namespace D {
278
  using A::a;
279
  }
 
283
  using namespace D;
284
  }
285
 
286
  void g()
287
  {
288
+ BD::a++; // OK: S is { `A::a`, `A::a` }
289
  }
290
  ```
291
 
292
+ — *end example*]
293
+
294
+ — *end note*]
295
+
296
+ [*Example 3*:
297
+
298
  Because each referenced namespace is searched at most once, the
299
  following is well-defined:
300
 
301
  ``` cpp
302
  namespace B {
 
312
  using namespace A;
313
  }
314
 
315
  void f()
316
  {
317
+ A::a++; // OK: a declared directly in A, S is { `A::a` }
318
+ B::a++; // OK: both A and B searched (once), S is { `A::a` }
319
+ A::b++; // OK: both A and B searched (once), S is { `B::b` }
320
+ B::b++; // OK: b declared directly in B, S is { `B::b` }
321
  }
322
  ```
323
 
324
+ — *end example*]
325
+
326
  During the lookup of a qualified namespace member name, if the lookup
327
  finds more than one declaration of the member, and if one declaration
328
  introduces a class name or enumeration name and the other declarations
329
  either introduce the same variable, the same enumerator or a set of
330
  functions, the non-type name hides the class or enumeration name if and
331
  only if the declarations are from the same namespace; otherwise (the
332
  declarations are from different namespaces), the program is ill-formed.
333
 
334
+ [*Example 4*:
335
+
336
  ``` cpp
337
  namespace A {
338
  struct x { };
339
  int x;
340
  int y;
 
350
  int i = C::x; // OK, A::x (of type int)
351
  int j = C::y; // ambiguous, A::y or B::y
352
  }
353
  ```
354
 
355
+ — *end example*]
356
+
357
  In a declaration for a namespace member in which the *declarator-id* is
358
  a *qualified-id*, given that the *qualified-id* for the namespace member
359
  has the form
360
 
361
  ``` bnf
 
364
 
365
  the *unqualified-id* shall name a member of the namespace designated by
366
  the *nested-name-specifier* or of an element of the inline namespace
367
  set ([[namespace.def]]) of that namespace.
368
 
369
+ [*Example 5*:
370
+
371
  ``` cpp
372
  namespace A {
373
  namespace B {
374
  void f1(int);
375
  }
376
  using namespace B;
377
  }
378
  void A::f1(int){ } // ill-formed, f1 is not a member of A
379
  ```
380
 
381
+ — *end example*]
382
+
383
  However, in such namespace member declarations, the
384
  *nested-name-specifier* may rely on *using-directive*s to implicitly
385
  provide the initial part of the *nested-name-specifier*.
386
 
387
+ [*Example 6*:
388
+
389
  ``` cpp
390
  namespace A {
391
  namespace B {
392
  void f1(int);
393
  }
 
402
  using namespace A;
403
  using namespace C::D;
404
  void B::f1(int){ } // OK, defines A::B::f1(int)
405
  ```
406
 
407
+ — *end example*]
408
+