From Jason Turner

[temp.res]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp1huivh24/{from.md → to.md} +272 -489
tmp/tmp1huivh24/{from.md → to.md} RENAMED
@@ -1,60 +1,109 @@
1
  ## Name resolution <a id="temp.res">[[temp.res]]</a>
2
 
3
- Three kinds of names can be used within a template definition:
4
 
5
- - The name of the template itself, and names declared within the
6
- template itself.
7
- - Names dependent on a *template-parameter* [[temp.dep]].
8
- - Names from scopes which are visible within the template definition.
 
 
 
9
 
10
- A name used in a template declaration or definition and that is
11
- dependent on a *template-parameter* is assumed not to name a type unless
12
- the applicable name lookup finds a type name or the name is qualified by
13
- the keyword `typename`.
 
 
14
 
15
  [*Example 1*:
16
 
17
  ``` cpp
18
- // no B declared here
19
-
20
- class X;
21
-
22
- template<class T> class Y {
23
- class Z; // forward declaration of member class
24
-
25
- void f() {
26
- X* a1; // declare pointer to X
27
- T* a2; // declare pointer to T
28
- Y* a3; // declare pointer to Y<T>
29
- Z* a4; // declare pointer to Z
30
- typedef typename T::A TA;
31
- TA* a5; // declare pointer to T's A
32
- typename T::A* a6; // declare pointer to T's A
33
- T::A* a7; // error: no visible declaration of a7
34
- // T::A is not a type name; multiplication of T::A by a7
35
- B* a8; // error: no visible declarations of B and a8
36
- // B is not a type name; multiplication of B by a8
 
37
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  };
 
 
39
  ```
40
 
 
 
 
41
  — *end example*]
42
 
 
 
 
 
 
 
43
  ``` bnf
44
  typename-specifier:
45
  typename nested-name-specifier identifier
46
  typename nested-name-specifier 'templateₒₚₜ ' simple-template-id
47
  ```
48
 
49
- A *typename-specifier* denotes the type or class template denoted by the
50
- *simple-type-specifier* [[dcl.type.simple]] formed by omitting the
51
- keyword `typename`. The usual qualified name lookup
52
- [[basic.lookup.qual]] is used to find the *qualified-id* even in the
53
- presence of `typename`.
54
 
55
- [*Example 2*:
 
 
 
56
 
57
  ``` cpp
58
  struct A {
59
  struct X { };
60
  int X;
@@ -66,52 +115,44 @@ template<class T> void f(T t) {
66
  typename T::X x;
67
  }
68
  void foo() {
69
  A a;
70
  B b;
71
- f(b); // OK: T::X refers to B::X
72
  f(a); // error: T::X refers to the data member A::X not the struct A::X
73
  }
74
  ```
75
 
76
  — *end example*]
77
 
78
- A qualified name used as the name in a *class-or-decltype*
79
- [[class.derived]] or an *elaborated-type-specifier* is implicitly
80
- assumed to name a type, without the use of the `typename` keyword. In a
81
- *nested-name-specifier* that immediately contains a
82
- *nested-name-specifier* that depends on a template parameter, the
83
- *identifier* or *simple-template-id* is implicitly assumed to name a
84
- type, without the use of the `typename` keyword.
85
 
86
- [*Note 1*: The `typename` keyword is not permitted by the syntax of
87
- these constructs. — *end note*]
88
-
89
- A *qualified-id* is assumed to name a type if
90
-
91
- - it is a qualified name in a type-id-only context (see below), or
92
- - it is a *decl-specifier* of the *decl-specifier-seq* of a
 
 
 
 
93
  - *simple-declaration* or a *function-definition* in namespace scope,
94
  - *member-declaration*,
95
- - *parameter-declaration* in a *member-declaration* [^10], unless that
96
  *parameter-declaration* appears in a default argument,
97
  - *parameter-declaration* in a *declarator* of a function or function
98
  template declaration whose *declarator-id* is qualified, unless that
99
  *parameter-declaration* appears in a default argument,
100
  - *parameter-declaration* in a *lambda-declarator* or
101
  *requirement-parameter-list*, unless that *parameter-declaration*
102
  appears in a default argument, or
103
  - *parameter-declaration* of a (non-type) *template-parameter*.
104
 
105
- A qualified name is said to be in a *type-id-only context* if it appears
106
- in a *type-id*, *new-type-id*, or *defining-type-id* and the smallest
107
- enclosing *type-id*, *new-type-id*, or *defining-type-id* is a
108
- *new-type-id*, *defining-type-id*, *trailing-return-type*, default
109
- argument of a *type-parameter* of a template, or *type-id* of a
110
- `static_cast`, `const_cast`, `reinterpret_cast`, or `dynamic_cast`.
111
-
112
- [*Example 3*:
113
 
114
  ``` cpp
115
  template<class T> T::R f(); // OK, return type of a function declaration at global scope
116
  template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
117
  // a void variable template
@@ -129,15 +170,16 @@ template<typename T> void f() {
129
  }
130
  ```
131
 
132
  — *end example*]
133
 
134
- A *qualified-id* that refers to a member of an unknown specialization,
135
- that is not prefixed by `typename`, and that is not otherwise assumed to
136
- name a type (see above) denotes a non-type.
 
137
 
138
- [*Example 4*:
139
 
140
  ``` cpp
141
  template <class T> void f(int i) {
142
  T::x * i; // expression, not the declaration of a variable i
143
  }
@@ -156,63 +198,53 @@ int main() {
156
  }
157
  ```
158
 
159
  — *end example*]
160
 
161
- Within the definition of a class template or within the definition of a
162
- member of a class template following the *declarator-id*, the keyword
163
- `typename` is not required when referring to a member of the current
164
- instantiation [[temp.dep.type]].
165
-
166
- [*Example 5*:
167
-
168
- ``` cpp
169
- template<class T> struct A {
170
- typedef int B;
171
- B b; // OK, no typename required
172
- };
173
- ```
174
-
175
- — *end example*]
176
-
177
  The validity of a template may be checked prior to any instantiation.
178
 
179
- [*Note 2*: Knowing which names are type names allows the syntax of
180
  every template to be checked in this way. — *end note*]
181
 
182
  The program is ill-formed, no diagnostic required, if:
183
 
184
- - no valid specialization can be generated for a template or a
185
- substatement of a constexpr if statement [[stmt.if]] within a template
186
- and the template is not instantiated, or
187
- - no substitution of template arguments into a *type-constraint* or
188
- *requires-clause* would result in a valid expression, or
 
 
 
189
  - every valid specialization of a variadic template requires an empty
190
  template parameter pack, or
191
  - a hypothetical instantiation of a template immediately following its
192
  definition would be ill-formed due to a construct that does not depend
193
  on a template parameter, or
194
  - the interpretation of such a construct in the hypothetical
195
  instantiation is different from the interpretation of the
196
  corresponding construct in any actual instantiation of the template.
197
- \[*Note 3*:
 
 
198
  This can happen in situations including the following:
 
199
  - a type used in a non-dependent name is incomplete at the point at
200
  which a template is defined but is complete at the point at which an
201
  instantiation is performed, or
202
  - lookup for a name in the template definition found a
203
- *using-declaration*, but the lookup in the corresponding scope in
204
- the instantiation does not find any declarations because the
205
- *using-declaration* was a pack expansion and the corresponding pack
206
- is empty, or
207
- - an instantiation uses a default argument or default template
208
- argument that had not been defined at the point at which the
209
- template was defined, or
210
  - constant expression evaluation [[expr.const]] within the template
211
  instantiation uses
212
- - the value of a const object of integral or unscoped enumeration
213
- type or
214
  - the value of a `constexpr` object or
215
  - the value of a reference or
216
  - the definition of a constexpr function,
217
 
218
  and that entity was not defined when the template was defined, or
@@ -226,23 +258,25 @@ The program is ill-formed, no diagnostic required, if:
226
  — *end note*]
227
 
228
  Otherwise, no diagnostic shall be issued for a template for which a
229
  valid specialization can be generated.
230
 
231
- [*Note 4*: If a template is instantiated, errors will be diagnosed
232
  according to the other rules in this document. Exactly when these errors
233
  are diagnosed is a quality of implementation issue. — *end note*]
234
 
235
- [*Example 6*:
236
 
237
  ``` cpp
238
  int j;
239
  template<class T> class X {
240
  void f(T t, int i, char* p) {
241
  t = i; // diagnosed if X::f is instantiated, and the assignment to t is an error
242
  p = i; // may be diagnosed even if X::f is not instantiated
243
  p = j; // may be diagnosed even if X::f is not instantiated
 
 
244
  }
245
  void g(T t) {
246
  +; // may be diagnosed even if X::g is not instantiated
247
  }
248
  };
@@ -254,79 +288,11 @@ template<class... T> union X : T... { }; // error: union with base cl
254
  template<class... T> struct A : T..., T... { }; // error: duplicate base class
255
  ```
256
 
257
  — *end example*]
258
 
259
- When looking for the declaration of a name used in a template
260
- definition, the usual lookup rules ([[basic.lookup.unqual]],
261
- [[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
262
- names dependent on the template parameters is postponed until the actual
263
- template argument is known [[temp.dep]].
264
-
265
- [*Example 7*:
266
-
267
- ``` cpp
268
- #include <iostream>
269
- using namespace std;
270
-
271
- template<class T> class Set {
272
- T* p;
273
- int cnt;
274
- public:
275
- Set();
276
- Set<T>(const Set<T>&);
277
- void printall() {
278
- for (int i = 0; i<cnt; i++)
279
- cout << p[i] << '\n';
280
- }
281
- };
282
- ```
283
-
284
- In the example, `i` is the local variable `i` declared in `printall`,
285
- `cnt` is the member `cnt` declared in `Set`, and `cout` is the standard
286
- output stream declared in `iostream`. However, not every declaration can
287
- be found this way; the resolution of some names must be postponed until
288
- the actual *template-argument*s are known. For example, even though the
289
- name `operator<<` is known within the definition of `printall()` and a
290
- declaration of it can be found in `<iostream>`, the actual declaration
291
- of `operator<<` needed to print `p[i]` cannot be known until it is known
292
- what type `T` is [[temp.dep]].
293
-
294
- — *end example*]
295
-
296
- If a name does not depend on a *template-parameter* (as defined in 
297
- [[temp.dep]]), a declaration (or set of declarations) for that name
298
- shall be in scope at the point where the name appears in the template
299
- definition; the name is bound to the declaration (or declarations) found
300
- at that point and this binding is not affected by declarations that are
301
- visible at the point of instantiation.
302
-
303
- [*Example 8*:
304
-
305
- ``` cpp
306
- void f(char);
307
-
308
- template<class T> void g(T t) {
309
- f(1); // f(char)
310
- f(T(1)); // dependent
311
- f(t); // dependent
312
- dd++; // not dependent; error: declaration for dd not found
313
- }
314
-
315
- enum E { e };
316
- void f(E);
317
-
318
- double dd;
319
- void h() {
320
- g(e); // will cause one call of f(char) followed by two calls of f(E)
321
- g('a'); // will cause three calls of f(char)
322
- }
323
- ```
324
-
325
- — *end example*]
326
-
327
- [*Note 5*: For purposes of name lookup, default arguments and
328
  *noexcept-specifier*s of function templates and default arguments and
329
  *noexcept-specifier*s of member functions of class templates are
330
  considered definitions [[temp.decls]]. — *end note*]
331
 
332
  ### Locally declared names <a id="temp.local">[[temp.local]]</a>
@@ -337,18 +303,18 @@ as a *template-name* or a *type-name*. When it is used with a
337
  *template-argument-list*, as a *template-argument* for a template
338
  *template-parameter*, or as the final identifier in the
339
  *elaborated-type-specifier* of a friend class template declaration, it
340
  is a *template-name* that refers to the class template itself.
341
  Otherwise, it is a *type-name* equivalent to the *template-name*
342
- followed by the *template-parameter*s of the class template enclosed in
343
- `<>`.
 
344
 
345
- Within the scope of a class template specialization or partial
346
- specialization, when the injected-class-name is used as a *type-name*,
347
- it is equivalent to the *template-name* followed by the
348
- *template-argument*s of the class template specialization or partial
349
- specialization enclosed in `<>`.
350
 
351
  [*Example 1*:
352
 
353
  ``` cpp
354
  template<template<class> class T> class A { };
@@ -365,11 +331,11 @@ template<> class Y<int> {
365
 
366
  — *end example*]
367
 
368
  The injected-class-name of a class template or class template
369
  specialization can be used as either a *template-name* or a *type-name*
370
- wherever it is in scope.
371
 
372
  [*Example 2*:
373
 
374
  ``` cpp
375
  template <class T> struct Base {
@@ -378,12 +344,12 @@ template <class T> struct Base {
378
 
379
  template <class T> struct Derived: public Base<T> {
380
  typename Derived::Base* p; // meaning Derived::Base<T>
381
  };
382
 
383
- template<class T, template<class> class U = T::template Base> struct Third { };
384
- Third<Derived<int> > t; // OK: default argument uses injected-class-name as a template
385
  ```
386
 
387
  — *end example*]
388
 
389
  A lookup that finds an injected-class-name [[class.member.lookup]] can
@@ -421,135 +387,105 @@ template<class T> class X {
421
  };
422
  ```
423
 
424
  — *end example*]
425
 
426
- The name of a *template-parameter* shall not be redeclared within its
427
- scope (including nested scopes). A *template-parameter* shall not have
428
- the same name as the template name.
429
 
430
  [*Example 5*:
431
 
432
  ``` cpp
433
  template<class T, int i> class Y {
434
- int T; // error: template-parameter redeclared
435
  void f() {
436
- char T; // error: template-parameter redeclared
437
  }
 
438
  };
439
 
440
- template<class X> class X; // error: template-parameter redeclared
441
  ```
442
 
443
  — *end example*]
444
 
445
- In the definition of a member of a class template that appears outside
446
- of the class template definition, the name of a member of the class
447
- template hides the name of a *template-parameter* of any enclosing class
448
- templates (but not a *template-parameter* of the member if the member is
449
- a class or function template).
 
 
 
450
 
451
  [*Example 6*:
452
 
453
  ``` cpp
454
- template<class T> struct A {
455
- struct B { ... };
 
 
456
  typedef void C;
457
  void f();
458
  template<class U> void g(U);
459
  };
460
-
461
- template<class B> void A<B>::f() {
462
- B b; // A's B, not the template parameter
463
  }
464
 
465
- template<class B> template<class C> void A<B>::g(C) {
466
- B b; // A's B, not the template parameter
467
- C c; // the template parameter C, not A's C
468
  }
469
- ```
470
-
471
- — *end example*]
472
-
473
- In the definition of a member of a class template that appears outside
474
- of the namespace containing the class template definition, the name of a
475
- *template-parameter* hides the name of a member of this namespace.
476
 
477
- [*Example 7*:
478
-
479
- ``` cpp
480
- namespace N {
481
- class C { };
482
- template<class T> class B {
483
- void f(T);
484
- };
485
- }
486
- template<class C> void N::B<C>::f(C) {
487
- C b; // C is the template parameter, not N::C
488
  }
489
  ```
490
 
491
  — *end example*]
492
 
493
- In the definition of a class template or in the definition of a member
494
- of such a template that appears outside of the template definition, for
495
- each non-dependent base class [[temp.dep.type]], if the name of the base
496
- class or the name of a member of the base class is the same as the name
497
- of a *template-parameter*, the base class name or member name hides the
498
- *template-parameter* name [[basic.scope.hiding]].
499
-
500
- [*Example 8*:
501
-
502
- ``` cpp
503
- struct A {
504
- struct B { ... };
505
- int a;
506
- int Y;
507
- };
508
-
509
- template<class B, class a> struct X : A {
510
- B b; // A's B
511
- a b; // error: A's a isn't a type name
512
- };
513
- ```
514
-
515
- — *end example*]
516
-
517
  ### Dependent names <a id="temp.dep">[[temp.dep]]</a>
518
 
 
 
519
  Inside a template, some constructs have semantics which may differ from
520
  one instantiation to another. Such a construct *depends* on the template
521
  parameters. In particular, types and expressions may depend on the type
522
  and/or value of template parameters (as determined by the template
523
  arguments) and this determines the context for name lookup for certain
524
  names. An expression may be *type-dependent* (that is, its type may
525
  depend on a template parameter) or *value-dependent* (that is, its value
526
  when evaluated as a constant expression [[expr.const]] may depend on a
527
- template parameter) as described in this subclause.
528
 
529
- In an expression of the form:
 
530
 
531
  ``` bnf
532
  postfix-expression '(' expression-listₒₚₜ ')'
533
  ```
534
 
535
- where the *postfix-expression* is an *unqualified-id*, the
536
- *unqualified-id* denotes a *dependent name* if
537
 
538
  - any of the expressions in the *expression-list* is a pack expansion
539
- [[temp.variadic]],
540
  - any of the expressions or *braced-init-list*s in the *expression-list*
541
  is type-dependent [[temp.dep.expr]], or
542
  - the *unqualified-id* is a *template-id* in which any of the template
543
  arguments depends on a template parameter.
544
 
545
- If an operand of an operator is a type-dependent expression, the
546
- operator also denotes a dependent name.
547
 
548
- [*Note 1*: Such names are unbound and are looked up at the point of the
549
- template instantiation [[temp.point]] in both the context of the
550
- template definition and the context of the point of instantiation
 
 
 
 
 
551
  [[temp.dep.candidate]]. — *end note*]
552
 
553
  [*Example 1*:
554
 
555
  ``` cpp
@@ -565,88 +501,30 @@ template<class T> struct X : B<T> {
565
  The base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
566
  and `pb->j` explicitly depend on the *template-parameter*.
567
 
568
  — *end example*]
569
 
570
- In the definition of a class or class template, the scope of a dependent
571
- base class [[temp.dep.type]] is not examined during unqualified name
572
- lookup either at the point of definition of the class template or member
573
- or during an instantiation of the class template or member.
574
-
575
- [*Example 2*:
576
-
577
- ``` cpp
578
- typedef double A;
579
- template<class T> class B {
580
- typedef int A;
581
- };
582
- template<class T> struct X : B<T> {
583
- A a; // a has type double
584
- };
585
- ```
586
-
587
- The type name `A` in the definition of `X<T>` binds to the typedef name
588
- defined in the global namespace scope, not to the typedef name defined
589
- in the base class `B<T>`.
590
-
591
- — *end example*]
592
-
593
- [*Example 3*:
594
-
595
- ``` cpp
596
- struct A {
597
- struct B { ... };
598
- int a;
599
- int Y;
600
- };
601
-
602
- int a;
603
-
604
- template<class T> struct Y : T {
605
- struct B { ... };
606
- B b; // The B defined in Y
607
- void f(int i) { a = i; } // ::a
608
- Y* p; // Y<T>
609
- };
610
-
611
- Y<A> ya;
612
- ```
613
-
614
- The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
615
- not affect the binding of names in `Y<A>`.
616
-
617
- — *end example*]
618
-
619
  #### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
620
 
621
- A name refers to the *current instantiation* if it is
622
 
623
  - in the definition of a class template, a nested class of a class
624
  template, a member of a class template, or a member of a nested class
625
  of a class template, the injected-class-name [[class.pre]] of the
626
  class template or nested class,
627
  - in the definition of a primary class template or a member of a primary
628
  class template, the name of the class template followed by the
629
- template argument list of the primary template (as described below)
630
- enclosed in `<>` (or an equivalent template alias specialization),
631
  - in the definition of a nested class of a class template, the name of
632
  the nested class referenced as a member of the current instantiation,
633
  or
634
- - in the definition of a partial specialization or a member of a partial
635
- specialization, the name of the class template followed by the
636
- template argument list of the partial specialization enclosed in `<>`
637
- (or an equivalent template alias specialization). If the nᵗʰ template
638
- parameter is a template parameter pack, the nᵗʰ template argument is a
639
- pack expansion [[temp.variadic]] whose pattern is the name of the
640
- template parameter pack.
641
-
642
- The template argument list of a primary template is a template argument
643
- list in which the nᵗʰ template argument has the value of the nᵗʰ
644
- template parameter of the class template. If the nᵗʰ template parameter
645
- is a template parameter pack [[temp.variadic]], the nᵗʰ template
646
- argument is a pack expansion [[temp.variadic]] whose pattern is the name
647
- of the template parameter pack.
648
 
649
  A template argument that is equivalent to a template parameter can be
650
  used in place of that template parameter in a reference to the current
651
  instantiation. For a template *type-parameter*, a template argument is
652
  equivalent to a template parameter if it denotes the same type. For a
@@ -728,32 +606,17 @@ template<class T> struct A<T>::B::C : A<T> {
728
 
729
  — *end example*]
730
 
731
  — *end note*]
732
 
733
- A name is a *member of the current instantiation* if it is
 
734
 
735
- - An unqualified name that, when looked up, refers to at least one
736
- member of a class that is the current instantiation or a non-dependent
737
- base class thereof. \[*Note 3*: This can only occur when looking up a
738
- name in a scope enclosed by the definition of a class
739
- template. — *end note*]
740
- - A *qualified-id* in which the *nested-name-specifier* refers to the
741
- current instantiation and that, when looked up, refers to at least one
742
- member of a class that is the current instantiation or a non-dependent
743
- base class thereof. \[*Note 4*: If no such member is found, and the
744
- current instantiation has any dependent base classes, then the
745
- *qualified-id* is a member of an unknown specialization; see
746
- below. — *end note*]
747
- - An *id-expression* denoting the member in a class member access
748
- expression [[expr.ref]] for which the type of the object expression is
749
- the current instantiation, and the *id-expression*, when looked up
750
- [[basic.lookup.classref]], refers to at least one member of a class
751
- that is the current instantiation or a non-dependent base class
752
- thereof. \[*Note 5*: If no such member is found, and the current
753
- instantiation has any dependent base classes, then the *id-expression*
754
- is a member of an unknown specialization; see below. — *end note*]
755
 
756
  [*Example 3*:
757
 
758
  ``` cpp
759
  template <class T> class A {
@@ -769,66 +632,51 @@ template <class T> int A<T>::f() {
769
  }
770
  ```
771
 
772
  — *end example*]
773
 
774
- A name is a *dependent member of the current instantiation* if it is a
775
- member of the current instantiation that, when looked up, refers to at
776
- least one member of a class that is the current instantiation.
 
 
777
 
778
- A name is a *member of an unknown specialization* if it is
779
 
780
- - A *qualified-id* in which the *nested-name-specifier* names a
781
- dependent type that is not the current instantiation.
782
- - A *qualified-id* in which the *nested-name-specifier* refers to the
783
- current instantiation, the current instantiation has at least one
784
- dependent base class, and name lookup of the *qualified-id* does not
785
- find any member of a class that is the current instantiation or a
786
- non-dependent base class thereof.
787
- - An *id-expression* denoting the member in a class member access
788
- expression [[expr.ref]] in which either
789
- - the type of the object expression is the current instantiation, the
790
- current instantiation has at least one dependent base class, and
791
- name lookup of the *id-expression* does not find a member of a class
792
- that is the current instantiation or a non-dependent base class
793
- thereof; or
794
- - the type of the object expression is not the current instantiation
795
- and the object expression is type-dependent.
796
-
797
- If a *qualified-id* in which the *nested-name-specifier* refers to the
798
- current instantiation is not a member of the current instantiation or a
799
- member of an unknown specialization, the program is ill-formed even if
800
- the template containing the *qualified-id* is not instantiated; no
801
- diagnostic required. Similarly, if the *id-expression* in a class member
802
- access expression for which the type of the object expression is the
803
- current instantiation does not refer to a member of the current
804
- instantiation or a member of an unknown specialization, the program is
805
- ill-formed even if the template containing the member access expression
806
- is not instantiated; no diagnostic required.
807
 
808
  [*Example 4*:
809
 
810
  ``` cpp
811
- template<class T> class A {
812
- typedef int type;
813
- void f() {
814
- A<T>::type i; // OK: refers to a member of the current instantiation
815
- typename A<T>::other j; // error: neither a member of the current instantiation nor
816
- // a member of an unknown specialization
817
- }
818
  };
 
 
 
 
 
 
819
  ```
820
 
821
  — *end example*]
822
 
823
  If, for a given set of template arguments, a specialization of a
824
  template is instantiated that refers to a member of the current
825
- instantiation with a *qualified-id* or class member access expression,
826
- the name in the *qualified-id* or class member access expression is
827
- looked up in the template instantiation context. If the result of this
828
- lookup differs from the result of name lookup in the template definition
829
- context, name lookup is ambiguous.
830
 
831
  [*Example 5*:
832
 
833
  ``` cpp
834
  struct A {
@@ -844,84 +692,87 @@ struct C : A, T {
844
  int f() { return this->m; } // finds A::m in the template definition context
845
  int g() { return m; } // finds A::m in the template definition context
846
  };
847
 
848
  template int C<B>::f(); // error: finds both A::m and B::m
849
- template int C<B>::g(); // OK: transformation to class member access syntax
850
- // does not occur in the template definition context; see~[class.mfct.non-static]
851
  ```
852
 
853
  — *end example*]
854
 
855
  A type is dependent if it is
856
 
857
  - a template parameter,
858
- - a member of an unknown specialization,
859
- - a nested class or enumeration that is a dependent member of the
860
- current instantiation,
861
  - a cv-qualified type where the cv-unqualified type is dependent,
862
  - a compound type constructed from any dependent type,
863
  - an array type whose element type is dependent or whose bound (if any)
864
  is value-dependent,
 
 
865
  - a function type whose exception specification is value-dependent,
866
  - denoted by a *simple-template-id* in which either the template name is
867
  a template parameter or any of the template arguments is a dependent
868
  type or an expression that is type-dependent or value-dependent or is
869
- a pack expansion \[*Note 6*: This includes an injected-class-name
870
- [[class.pre]] of a class template used without a
871
- *template-argument-list*. — *end note*] , or
872
  - denoted by `decltype(`*expression*`)`, where *expression* is
873
  type-dependent [[temp.dep.expr]].
874
 
875
- [*Note 7*: Because typedefs do not introduce new types, but instead
876
  simply refer to other types, a name that refers to a typedef that is a
877
  member of the current instantiation is dependent only if the type
878
  referred to is dependent. — *end note*]
879
 
880
  #### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
881
 
882
  Except as described below, an expression is type-dependent if any
883
  subexpression is type-dependent.
884
 
885
- `this`
886
-
887
- is type-dependent if the class type of the enclosing member function is
888
  dependent [[temp.dep.type]].
889
 
890
- An *id-expression* is type-dependent if it is not a concept-id and it
891
- contains
892
 
893
- - an *identifier* associated by name lookup with one or more
894
- declarations declared with a dependent type,
895
- - an *identifier* associated by name lookup with a non-type
896
- *template-parameter* declared with a type that contains a placeholder
897
- type [[dcl.spec.auto]],
898
- - an *identifier* associated by name lookup with a variable declared
899
- with a type that contains a placeholder type [[dcl.spec.auto]] where
900
- the initializer is type-dependent,
901
- - an *identifier* associated by name lookup with one or more
902
- declarations of member functions of the current instantiation declared
903
- with a return type that contains a placeholder type,
904
- - an *identifier* associated by name lookup with a structured binding
905
- declaration [[dcl.struct.bind]] whose *brace-or-equal-initializer* is
906
  type-dependent,
 
 
 
 
 
 
 
 
 
907
  - the *identifier* `__func__` [[dcl.fct.def.general]], where any
908
  enclosing function is a template, a member of a class template, or a
909
  generic lambda,
910
- - a *template-id* that is dependent,
911
  - a *conversion-function-id* that specifies a dependent type, or
912
- - a *nested-name-specifier* or a *qualified-id* that names a member of
913
- an unknown specialization;
914
 
915
  or if it names a dependent member of the current instantiation that is a
916
  static data member of type “array of unknown bound of `T`” for some `T`
917
  [[temp.static]]. Expressions of the following forms are type-dependent
918
- only if the type specified by the *type-id*, *simple-type-specifier* or
919
- *new-type-id* is dependent, even if any subexpression is type-dependent:
 
920
 
921
  ``` bnf
922
  simple-type-specifier '(' expression-listₒₚₜ ')'
 
 
 
923
  '::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
924
  '::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
925
  dynamic_cast '<' type-id '>' '(' expression ')'
926
  static_cast '<' type-id '>' '(' expression ')'
927
  const_cast '<' type-id '>' '(' expression ')'
@@ -948,21 +799,19 @@ noexcept '(' expression ')'
948
 
949
  [*Note 1*: For the standard library macro `offsetof`, see 
950
  [[support.types]]. — *end note*]
951
 
952
  A class member access expression [[expr.ref]] is type-dependent if the
 
953
  expression refers to a member of the current instantiation and the type
954
- of the referenced member is dependent, or the class member access
955
- expression refers to a member of an unknown specialization.
956
 
957
  [*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
958
  the expression is usually the type of the member `y` of the class of `x`
959
  (or the class pointed to by `xp`). However, if `x` or `xp` refers to a
960
  dependent type that is not the current instantiation, the type of `y` is
961
- always dependent. If `x` or `xp` refers to a non-dependent type or
962
- refers to the current instantiation, the type of `y` is the type of the
963
- class member access expression. — *end note*]
964
 
965
  A *braced-init-list* is type-dependent if any element is type-dependent
966
  or is a pack expansion.
967
 
968
  A *fold-expression* is type-dependent.
@@ -1039,38 +888,12 @@ the constant expression it specifies is value-dependent.
1039
  Furthermore, a non-type *template-argument* is dependent if the
1040
  corresponding non-type *template-parameter* is of reference or pointer
1041
  type and the *template-argument* designates or points to a member of the
1042
  current instantiation or a member of a dependent type.
1043
 
1044
- A template *template-argument* is dependent if it names a
1045
- *template-parameter* or is a *qualified-id* that refers to a member of
1046
- an unknown specialization.
1047
-
1048
- ### Non-dependent names <a id="temp.nondep">[[temp.nondep]]</a>
1049
-
1050
- Non-dependent names used in a template definition are found using the
1051
- usual name lookup and bound at the point they are used.
1052
-
1053
- [*Example 1*:
1054
-
1055
- ``` cpp
1056
- void g(double);
1057
- void h();
1058
-
1059
- template<class T> class Z {
1060
- public:
1061
- void f() {
1062
- g(1); // calls g(double)
1063
- h++; // ill-formed: cannot increment function; this could be diagnosed
1064
- // either here or at the point of instantiation
1065
- }
1066
- };
1067
-
1068
- void g(int); // not in scope at the point of the template definition, not considered for the call g(1)
1069
- ```
1070
-
1071
- — *end example*]
1072
 
1073
  ### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
1074
 
1075
  #### Point of instantiation <a id="temp.point">[[temp.point]]</a>
1076
 
@@ -1142,26 +965,17 @@ If two different points of instantiation give a template specialization
1142
  different meanings according to the one-definition rule
1143
  [[basic.def.odr]], the program is ill-formed, no diagnostic required.
1144
 
1145
  #### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
1146
 
1147
- For a function call where the *postfix-expression* is a dependent name,
1148
- the candidate functions are found using the usual lookup rules from the
1149
- template definition context ([[basic.lookup.unqual]],
1150
- [[basic.lookup.argdep]]).
1151
-
1152
- [*Note 1*: For the part of the lookup using associated namespaces
1153
- [[basic.lookup.argdep]], function declarations found in the template
1154
- instantiation context are found by this lookup, as described in
1155
- [[basic.lookup.argdep]]. — *end note*]
1156
-
1157
- If the call would be ill-formed or would find a better match had the
1158
- lookup within the associated namespaces considered all the function
1159
- declarations with external linkage introduced in those namespaces in all
1160
- translation units, not just considering those declarations found in the
1161
- template definition and template instantiation contexts, then the
1162
- program has undefined behavior.
1163
 
1164
  [*Example 1*:
1165
 
1166
  Source file \`"X.h"\`
1167
 
@@ -1261,11 +1075,11 @@ Module interface unit of \`M\`
1261
  module;
1262
  #include "X.h"
1263
  export module M;
1264
  import F;
1265
  void g(X x) {
1266
- f(x); // OK: instantiates f from F,
1267
  // operator+ is visible in instantiation context
1268
  }
1269
  ```
1270
 
1271
  — *end example*]
@@ -1357,36 +1171,5 @@ void k() {
1357
  }
1358
  ```
1359
 
1360
  — *end example*]
1361
 
1362
- ### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
1363
-
1364
- Friend classes or functions can be declared within a class template.
1365
- When a template is instantiated, the names of its friends are treated as
1366
- if the specialization had been explicitly declared at its point of
1367
- instantiation.
1368
-
1369
- As with non-template classes, the names of namespace-scope friend
1370
- functions of a class template specialization are not visible during an
1371
- ordinary lookup unless explicitly declared at namespace scope
1372
- [[class.friend]]. Such names may be found under the rules for associated
1373
- classes [[basic.lookup.argdep]].[^11]
1374
-
1375
- [*Example 1*:
1376
-
1377
- ``` cpp
1378
- template<typename T> struct number {
1379
- number(int);
1380
- friend number gcd(number x, number y) { return 0; };
1381
- };
1382
-
1383
- void g() {
1384
- number<double> a(3), b(4);
1385
- a = gcd(a,b); // finds gcd because number<double> is an associated class,
1386
- // making gcd visible in its namespace (global scope)
1387
- b = gcd(3,4); // error: gcd is not visible
1388
- }
1389
- ```
1390
-
1391
- — *end example*]
1392
-
 
1
  ## Name resolution <a id="temp.res">[[temp.res]]</a>
2
 
3
+ ### General <a id="temp.res.general">[[temp.res.general]]</a>
4
 
5
+ A name that appears in a declaration D of a template T is looked up from
6
+ where it appears in an unspecified declaration of T that either is D
7
+ itself or is reachable from D and from which no other declaration of T
8
+ that contains the usage of the name is reachable. If the name is
9
+ dependent (as specified in [[temp.dep]]), it is looked up for each
10
+ specialization (after substitution) because the lookup depends on a
11
+ template parameter.
12
 
13
+ [*Note 1*: Some dependent names are also looked up during parsing to
14
+ determine that they are dependent or to interpret following `<` tokens.
15
+ Uses of other names might be type-dependent or value-dependent
16
+ [[temp.dep.expr]], [[temp.dep.constexpr]]. A *using-declarator* is never
17
+ dependent in a specialization and is therefore replaced during lookup
18
+ for that specialization [[basic.lookup]]. — *end note*]
19
 
20
  [*Example 1*:
21
 
22
  ``` cpp
23
+ struct A { operator int(); };
24
+ template<class B, class T>
25
+ struct D : B {
26
+ T get() { return operator T(); } // conversion-function-id is dependent
27
+ };
28
+ int f(D<A, int> d) { return d.get(); } // OK, lookup finds A::operator int
29
+ ```
30
+
31
+ *end example*]
32
+
33
+ [*Example 2*:
34
+
35
+ ``` cpp
36
+ void f(char);
37
+
38
+ template<class T> void g(T t) {
39
+ f(1); // f(char)
40
+ f(T(1)); // dependent
41
+ f(t); // dependent
42
+ dd++; // not dependent; error: declaration for dd not found
43
  }
44
+
45
+ enum E { e };
46
+ void f(E);
47
+
48
+ double dd;
49
+ void h() {
50
+ g(e); // will cause one call of f(char) followed by two calls of f(E)
51
+ g('a'); // will cause three calls of f(char)
52
+ }
53
+ ```
54
+
55
+ — *end example*]
56
+
57
+ [*Example 3*:
58
+
59
+ ``` cpp
60
+ struct A {
61
+ struct B { ... };
62
+ int a;
63
+ int Y;
64
+ };
65
+
66
+ int a;
67
+
68
+ template<class T> struct Y : T {
69
+ struct B { ... };
70
+ B b; // The B defined in Y
71
+ void f(int i) { a = i; } // ::a
72
+ Y* p; // Y<T>
73
  };
74
+
75
+ Y<A> ya;
76
  ```
77
 
78
+ The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
79
+ not affect the binding of names in `Y<A>`.
80
+
81
  — *end example*]
82
 
83
+ If the validity or meaning of the program would be changed by
84
+ considering a default argument or default template argument introduced
85
+ in a declaration that is reachable from the point of instantiation of a
86
+ specialization [[temp.point]] but is not found by lookup for the
87
+ specialization, the program is ill-formed, no diagnostic required.
88
+
89
  ``` bnf
90
  typename-specifier:
91
  typename nested-name-specifier identifier
92
  typename nested-name-specifier 'templateₒₚₜ ' simple-template-id
93
  ```
94
 
95
+ The component names of a *typename-specifier* are its *identifier* (if
96
+ any) and those of its *nested-name-specifier* and *simple-template-id*
97
+ (if any). A *typename-specifier* denotes the type or class template
98
+ denoted by the *simple-type-specifier* [[dcl.type.simple]] formed by
99
+ omitting the keyword `typename`.
100
 
101
+ [*Note 2*: The usual qualified name lookup [[basic.lookup.qual]]
102
+ applies even in the presence of `typename`. — *end note*]
103
+
104
+ [*Example 4*:
105
 
106
  ``` cpp
107
  struct A {
108
  struct X { };
109
  int X;
 
115
  typename T::X x;
116
  }
117
  void foo() {
118
  A a;
119
  B b;
120
+ f(b); // OK, T::X refers to B::X
121
  f(a); // error: T::X refers to the data member A::X not the struct A::X
122
  }
123
  ```
124
 
125
  — *end example*]
126
 
127
+ A qualified or unqualified name is said to be in a *type-only context*
128
+ if it is the terminal name of
 
 
 
 
 
129
 
130
+ - a *typename-specifier*, *nested-name-specifier*,
131
+ *elaborated-type-specifier*, *class-or-decltype*, or
132
+ - a *type-specifier* of a
133
+ - *new-type-id*,
134
+ - *defining-type-id*,
135
+ - *conversion-type-id*,
136
+ - *trailing-return-type*,
137
+ - default argument of a *type-parameter*, or
138
+ - *type-id* of a `static_cast`, `const_cast`, `reinterpret_cast`, or
139
+ `dynamic_cast`, or
140
+ - a *decl-specifier* of the *decl-specifier-seq* of a
141
  - *simple-declaration* or a *function-definition* in namespace scope,
142
  - *member-declaration*,
143
+ - *parameter-declaration* in a *member-declaration*,[^10] unless that
144
  *parameter-declaration* appears in a default argument,
145
  - *parameter-declaration* in a *declarator* of a function or function
146
  template declaration whose *declarator-id* is qualified, unless that
147
  *parameter-declaration* appears in a default argument,
148
  - *parameter-declaration* in a *lambda-declarator* or
149
  *requirement-parameter-list*, unless that *parameter-declaration*
150
  appears in a default argument, or
151
  - *parameter-declaration* of a (non-type) *template-parameter*.
152
 
153
+ [*Example 5*:
 
 
 
 
 
 
 
154
 
155
  ``` cpp
156
  template<class T> T::R f(); // OK, return type of a function declaration at global scope
157
  template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
158
  // a void variable template
 
170
  }
171
  ```
172
 
173
  — *end example*]
174
 
175
+ A *qualified-id* whose terminal name is dependent and that is in a
176
+ type-only context is considered to denote a type. A name that refers to
177
+ a *using-declarator* whose terminal name is dependent is interpreted as
178
+ a *typedef-name* if the *using-declarator* uses the keyword `typename`.
179
 
180
+ [*Example 6*:
181
 
182
  ``` cpp
183
  template <class T> void f(int i) {
184
  T::x * i; // expression, not the declaration of a variable i
185
  }
 
198
  }
199
  ```
200
 
201
  — *end example*]
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  The validity of a template may be checked prior to any instantiation.
204
 
205
+ [*Note 3*: Knowing which names are type names allows the syntax of
206
  every template to be checked in this way. — *end note*]
207
 
208
  The program is ill-formed, no diagnostic required, if:
209
 
210
+ - no valid specialization, ignoring *static_assert-declaration*s that
211
+ fail, can be generated for a template or a substatement of a constexpr
212
+ if statement [[stmt.if]] within a template and the template is not
213
+ instantiated, or
214
+ - any *constraint-expression* in the program, introduced or otherwise,
215
+ has (in its normal form) an atomic constraint A where no satisfaction
216
+ check of A could be well-formed and no satisfaction check of A is
217
+ performed, or
218
  - every valid specialization of a variadic template requires an empty
219
  template parameter pack, or
220
  - a hypothetical instantiation of a template immediately following its
221
  definition would be ill-formed due to a construct that does not depend
222
  on a template parameter, or
223
  - the interpretation of such a construct in the hypothetical
224
  instantiation is different from the interpretation of the
225
  corresponding construct in any actual instantiation of the template.
226
+
227
+ [*Note 4*:
228
+
229
  This can happen in situations including the following:
230
+
231
  - a type used in a non-dependent name is incomplete at the point at
232
  which a template is defined but is complete at the point at which an
233
  instantiation is performed, or
234
  - lookup for a name in the template definition found a
235
+ *using-declaration*, but the lookup in the corresponding scope in the
236
+ instantiation does not find any declarations because the
237
+ *using-declaration* was a pack expansion and the corresponding pack is
238
+ empty, or
239
+ - an instantiation uses a default argument or default template argument
240
+ that had not been defined at the point at which the template was
241
+ defined, or
242
  - constant expression evaluation [[expr.const]] within the template
243
  instantiation uses
244
+ - the value of a const object of integral or unscoped enumeration type
245
+ or
246
  - the value of a `constexpr` object or
247
  - the value of a reference or
248
  - the definition of a constexpr function,
249
 
250
  and that entity was not defined when the template was defined, or
 
258
  — *end note*]
259
 
260
  Otherwise, no diagnostic shall be issued for a template for which a
261
  valid specialization can be generated.
262
 
263
+ [*Note 5*: If a template is instantiated, errors will be diagnosed
264
  according to the other rules in this document. Exactly when these errors
265
  are diagnosed is a quality of implementation issue. — *end note*]
266
 
267
+ [*Example 7*:
268
 
269
  ``` cpp
270
  int j;
271
  template<class T> class X {
272
  void f(T t, int i, char* p) {
273
  t = i; // diagnosed if X::f is instantiated, and the assignment to t is an error
274
  p = i; // may be diagnosed even if X::f is not instantiated
275
  p = j; // may be diagnosed even if X::f is not instantiated
276
+ X<T>::g(t); // OK
277
+ X<T>::h(); // may be diagnosed even if X::f is not instantiated
278
  }
279
  void g(T t) {
280
  +; // may be diagnosed even if X::g is not instantiated
281
  }
282
  };
 
288
  template<class... T> struct A : T..., T... { }; // error: duplicate base class
289
  ```
290
 
291
  — *end example*]
292
 
293
+ [*Note 6*: For purposes of name lookup, default arguments and
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  *noexcept-specifier*s of function templates and default arguments and
295
  *noexcept-specifier*s of member functions of class templates are
296
  considered definitions [[temp.decls]]. — *end note*]
297
 
298
  ### Locally declared names <a id="temp.local">[[temp.local]]</a>
 
303
  *template-argument-list*, as a *template-argument* for a template
304
  *template-parameter*, or as the final identifier in the
305
  *elaborated-type-specifier* of a friend class template declaration, it
306
  is a *template-name* that refers to the class template itself.
307
  Otherwise, it is a *type-name* equivalent to the *template-name*
308
+ followed by the template argument list
309
+ [[temp.decls.general]], [[temp.arg.general]] of the class template
310
+ enclosed in `<>`.
311
 
312
+ When the injected-class-name of a class template specialization or
313
+ partial specialization is used as a *type-name*, it is equivalent to the
314
+ *template-name* followed by the *template-argument*s of the class
315
+ template specialization or partial specialization enclosed in `<>`.
 
316
 
317
  [*Example 1*:
318
 
319
  ``` cpp
320
  template<template<class> class T> class A { };
 
331
 
332
  — *end example*]
333
 
334
  The injected-class-name of a class template or class template
335
  specialization can be used as either a *template-name* or a *type-name*
336
+ wherever it is named.
337
 
338
  [*Example 2*:
339
 
340
  ``` cpp
341
  template <class T> struct Base {
 
344
 
345
  template <class T> struct Derived: public Base<T> {
346
  typename Derived::Base* p; // meaning Derived::Base<T>
347
  };
348
 
349
+ template<class T, template<class> class U = T::Base> struct Third { };
350
+ Third<Derived<int> > t; // OK, default argument uses injected-class-name as a template
351
  ```
352
 
353
  — *end example*]
354
 
355
  A lookup that finds an injected-class-name [[class.member.lookup]] can
 
387
  };
388
  ```
389
 
390
  — *end example*]
391
 
392
+ The name of a *template-parameter* shall not be bound to any following
393
+ declaration whose locus is contained by the scope to which the
394
+ template-parameter belongs.
395
 
396
  [*Example 5*:
397
 
398
  ``` cpp
399
  template<class T, int i> class Y {
400
+ int T; // error: template-parameter hidden
401
  void f() {
402
+ char T; // error: template-parameter hidden
403
  }
404
+ friend void T(); // OK, no name bound
405
  };
406
 
407
+ template<class X> class X; // error: hidden by template-parameter
408
  ```
409
 
410
  — *end example*]
411
 
412
+ Unqualified name lookup considers the template parameter scope of a
413
+ *template-declaration* immediately after the outermost scope associated
414
+ with the template declared (even if its parent scope does not contain
415
+ the *template-parameter-list*).
416
+
417
+ [*Note 1*: The scope of a class template, including its non-dependent
418
+ base classes [[temp.dep.type]], [[class.member.lookup]], is searched
419
+ before its template parameter scope. — *end note*]
420
 
421
  [*Example 6*:
422
 
423
  ``` cpp
424
+ struct B { };
425
+ namespace N {
426
+ typedef void V;
427
+ template<class T> struct A : B {
428
  typedef void C;
429
  void f();
430
  template<class U> void g(U);
431
  };
 
 
 
432
  }
433
 
434
+ template<class V> void N::A<V>::f() { // N::V not considered here
435
+ V v; // V is still the template parameter, not N::V
 
436
  }
 
 
 
 
 
 
 
437
 
438
+ template<class B> template<class C> void N::A<B>::g(C) {
439
+ B b; // B is the base class, not the template parameter
440
+ C c; // C is the template parameter, not A's C
 
 
 
 
 
 
 
 
441
  }
442
  ```
443
 
444
  — *end example*]
445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  ### Dependent names <a id="temp.dep">[[temp.dep]]</a>
447
 
448
+ #### General <a id="temp.dep.general">[[temp.dep.general]]</a>
449
+
450
  Inside a template, some constructs have semantics which may differ from
451
  one instantiation to another. Such a construct *depends* on the template
452
  parameters. In particular, types and expressions may depend on the type
453
  and/or value of template parameters (as determined by the template
454
  arguments) and this determines the context for name lookup for certain
455
  names. An expression may be *type-dependent* (that is, its type may
456
  depend on a template parameter) or *value-dependent* (that is, its value
457
  when evaluated as a constant expression [[expr.const]] may depend on a
458
+ template parameter) as described below.
459
 
460
+ A *dependent call* is an expression, possibly formed as a non-member
461
+ candidate for an operator [[over.match.oper]], of the form:
462
 
463
  ``` bnf
464
  postfix-expression '(' expression-listₒₚₜ ')'
465
  ```
466
 
467
+ where the *postfix-expression* is an *unqualified-id* and
 
468
 
469
  - any of the expressions in the *expression-list* is a pack expansion
470
+ [[temp.variadic]], or
471
  - any of the expressions or *braced-init-list*s in the *expression-list*
472
  is type-dependent [[temp.dep.expr]], or
473
  - the *unqualified-id* is a *template-id* in which any of the template
474
  arguments depends on a template parameter.
475
 
476
+ The component name of an *unqualified-id* [[expr.prim.id.unqual]] is
477
+ dependent if
478
 
479
+ - it is a *conversion-function-id* whose *conversion-type-id* is
480
+ dependent, or
481
+ - it is `operator=` and the current class is a templated entity, or
482
+ - the *unqualified-id* is the *postfix-expression* in a dependent call.
483
+
484
+ [*Note 1*: Such names are looked up only at the point of the template
485
+ instantiation [[temp.point]] in both the context of the template
486
+ definition and the context of the point of instantiation
487
  [[temp.dep.candidate]]. — *end note*]
488
 
489
  [*Example 1*:
490
 
491
  ``` cpp
 
501
  The base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
502
  and `pb->j` explicitly depend on the *template-parameter*.
503
 
504
  — *end example*]
505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506
  #### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
507
 
508
+ A name or *template-id* refers to the *current instantiation* if it is
509
 
510
  - in the definition of a class template, a nested class of a class
511
  template, a member of a class template, or a member of a nested class
512
  of a class template, the injected-class-name [[class.pre]] of the
513
  class template or nested class,
514
  - in the definition of a primary class template or a member of a primary
515
  class template, the name of the class template followed by the
516
+ template argument list of its *template-head* [[temp.arg]] enclosed in
517
+ `<>` (or an equivalent template alias specialization),
518
  - in the definition of a nested class of a class template, the name of
519
  the nested class referenced as a member of the current instantiation,
520
  or
521
+ - in the definition of a class template partial specialization or a
522
+ member of a class template partial specialization, the name of the
523
+ class template followed by a template argument list equivalent to that
524
+ of the partial specialization [[temp.spec.partial]] enclosed in `<>`
525
+ (or an equivalent template alias specialization).
 
 
 
 
 
 
 
 
 
526
 
527
  A template argument that is equivalent to a template parameter can be
528
  used in place of that template parameter in a reference to the current
529
  instantiation. For a template *type-parameter*, a template argument is
530
  equivalent to a template parameter if it denotes the same type. For a
 
606
 
607
  — *end example*]
608
 
609
  — *end note*]
610
 
611
+ A qualified [[basic.lookup.qual]] or unqualified name is a *member of
612
+ the current instantiation* if
613
 
614
+ - its lookup context, if it is a qualified name, is the current
615
+ instantiation, and
616
+ - lookup for it finds any member of a class that is the current
617
+ instantiation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
 
619
  [*Example 3*:
620
 
621
  ``` cpp
622
  template <class T> class A {
 
632
  }
633
  ```
634
 
635
  — *end example*]
636
 
637
+ A qualified or unqualified name names a *dependent member of the current
638
+ instantiation* if it is a member of the current instantiation that, when
639
+ looked up, refers to at least one member declaration (including a
640
+ *using-declarator* whose terminal name is dependent) of a class that is
641
+ the current instantiation.
642
 
643
+ A qualified name [[basic.lookup.qual]] is dependent if
644
 
645
+ - it is a *conversion-function-id* whose *conversion-type-id* is
646
+ dependent, or
647
+ - its lookup context is dependent and is not the current instantiation,
648
+ or
649
+ - its lookup context is the current instantiation and it is
650
+ `operator=`,[^11] or
651
+ - its lookup context is the current instantiation and has at least one
652
+ dependent base class, and qualified name lookup for the name finds
653
+ nothing [[basic.lookup.qual]].
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
 
655
  [*Example 4*:
656
 
657
  ``` cpp
658
+ struct A {
659
+ using B = int;
660
+ A f();
 
 
 
 
661
  };
662
+ struct C : A {};
663
+ template<class T>
664
+ void g(T t) {
665
+ decltype(t.A::f())::B i; // error: typename needed to interpret B as a type
666
+ }
667
+ template void g(C); // … even though A is ::A here
668
  ```
669
 
670
  — *end example*]
671
 
672
  If, for a given set of template arguments, a specialization of a
673
  template is instantiated that refers to a member of the current
674
+ instantiation with a qualified name, the name is looked up in the
675
+ template instantiation context. If the result of this lookup differs
676
+ from the result of name lookup in the template definition context, name
677
+ lookup is ambiguous.
 
678
 
679
  [*Example 5*:
680
 
681
  ``` cpp
682
  struct A {
 
692
  int f() { return this->m; } // finds A::m in the template definition context
693
  int g() { return m; } // finds A::m in the template definition context
694
  };
695
 
696
  template int C<B>::f(); // error: finds both A::m and B::m
697
+ template int C<B>::g(); // OK, transformation to class member access syntax
698
+ // does not occur in the template definition context; see~[class.mfct.non.static]
699
  ```
700
 
701
  — *end example*]
702
 
703
  A type is dependent if it is
704
 
705
  - a template parameter,
706
+ - denoted by a dependent (qualified) name,
707
+ - a nested class or enumeration that is a direct member of a class that
708
+ is the current instantiation,
709
  - a cv-qualified type where the cv-unqualified type is dependent,
710
  - a compound type constructed from any dependent type,
711
  - an array type whose element type is dependent or whose bound (if any)
712
  is value-dependent,
713
+ - a function type whose parameters include one or more function
714
+ parameter packs,
715
  - a function type whose exception specification is value-dependent,
716
  - denoted by a *simple-template-id* in which either the template name is
717
  a template parameter or any of the template arguments is a dependent
718
  type or an expression that is type-dependent or value-dependent or is
719
+ a pack expansion,[^12] or
 
 
720
  - denoted by `decltype(`*expression*`)`, where *expression* is
721
  type-dependent [[temp.dep.expr]].
722
 
723
+ [*Note 3*: Because typedefs do not introduce new types, but instead
724
  simply refer to other types, a name that refers to a typedef that is a
725
  member of the current instantiation is dependent only if the type
726
  referred to is dependent. — *end note*]
727
 
728
  #### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
729
 
730
  Except as described below, an expression is type-dependent if any
731
  subexpression is type-dependent.
732
 
733
+ `this` is type-dependent if the current class [[expr.prim.this]] is
 
 
734
  dependent [[temp.dep.type]].
735
 
736
+ An *id-expression* is type-dependent if it is a *template-id* that is
737
+ not a concept-id and is dependent; or if its terminal name is
738
 
739
+ - associated by name lookup with one or more declarations declared with
740
+ a dependent type,
741
+ - associated by name lookup with a non-type *template-parameter*
742
+ declared with a type that contains a placeholder type
743
+ [[dcl.spec.auto]],
744
+ - associated by name lookup with a variable declared with a type that
745
+ contains a placeholder type [[dcl.spec.auto]] where the initializer is
 
 
 
 
 
 
746
  type-dependent,
747
+ - associated by name lookup with one or more declarations of member
748
+ functions of a class that is the current instantiation declared with a
749
+ return type that contains a placeholder type,
750
+ - associated by name lookup with a structured binding declaration
751
+ [[dcl.struct.bind]] whose *brace-or-equal-initializer* is
752
+ type-dependent,
753
+ - associated by name lookup with an entity captured by copy
754
+ [[expr.prim.lambda.capture]] in a *lambda-expression* that has an
755
+ explicit object parameter whose type is dependent [[dcl.fct]],
756
  - the *identifier* `__func__` [[dcl.fct.def.general]], where any
757
  enclosing function is a template, a member of a class template, or a
758
  generic lambda,
 
759
  - a *conversion-function-id* that specifies a dependent type, or
760
+ - dependent
 
761
 
762
  or if it names a dependent member of the current instantiation that is a
763
  static data member of type “array of unknown bound of `T`” for some `T`
764
  [[temp.static]]. Expressions of the following forms are type-dependent
765
+ only if the type specified by the *type-id*, *simple-type-specifier*,
766
+ *typename-specifier*, or *new-type-id* is dependent, even if any
767
+ subexpression is type-dependent:
768
 
769
  ``` bnf
770
  simple-type-specifier '(' expression-listₒₚₜ ')'
771
+ simple-type-specifier braced-init-list
772
+ typename-specifier '(' expression-listₒₚₜ ')'
773
+ typename-specifier braced-init-list
774
  '::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
775
  '::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
776
  dynamic_cast '<' type-id '>' '(' expression ')'
777
  static_cast '<' type-id '>' '(' expression ')'
778
  const_cast '<' type-id '>' '(' expression ')'
 
799
 
800
  [*Note 1*: For the standard library macro `offsetof`, see 
801
  [[support.types]]. — *end note*]
802
 
803
  A class member access expression [[expr.ref]] is type-dependent if the
804
+ terminal name of its *id-expression*, if any, is dependent or the
805
  expression refers to a member of the current instantiation and the type
806
+ of the referenced member is dependent.
 
807
 
808
  [*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
809
  the expression is usually the type of the member `y` of the class of `x`
810
  (or the class pointed to by `xp`). However, if `x` or `xp` refers to a
811
  dependent type that is not the current instantiation, the type of `y` is
812
+ always dependent. *end note*]
 
 
813
 
814
  A *braced-init-list* is type-dependent if any element is type-dependent
815
  or is a pack expansion.
816
 
817
  A *fold-expression* is type-dependent.
 
888
  Furthermore, a non-type *template-argument* is dependent if the
889
  corresponding non-type *template-parameter* is of reference or pointer
890
  type and the *template-argument* designates or points to a member of the
891
  current instantiation or a member of a dependent type.
892
 
893
+ A template *template-parameter* is dependent if it names a
894
+ *template-parameter* or its terminal name is dependent.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
895
 
896
  ### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
897
 
898
  #### Point of instantiation <a id="temp.point">[[temp.point]]</a>
899
 
 
965
  different meanings according to the one-definition rule
966
  [[basic.def.odr]], the program is ill-formed, no diagnostic required.
967
 
968
  #### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
969
 
970
+ If a dependent call [[temp.dep]] would be ill-formed or would find a
971
+ better match had the lookup for its dependent name considered all the
972
+ function declarations with external linkage introduced in the associated
973
+ namespaces in all translation units, not just considering those
974
+ declarations found in the template definition and template instantiation
975
+ contexts [[basic.lookup.argdep]], then the program is ill-formed, no
976
+ diagnostic required.
 
 
 
 
 
 
 
 
 
977
 
978
  [*Example 1*:
979
 
980
  Source file \`"X.h"\`
981
 
 
1075
  module;
1076
  #include "X.h"
1077
  export module M;
1078
  import F;
1079
  void g(X x) {
1080
+ f(x); // OK, instantiates f from F,
1081
  // operator+ is visible in instantiation context
1082
  }
1083
  ```
1084
 
1085
  — *end example*]
 
1171
  }
1172
  ```
1173
 
1174
  — *end example*]
1175