From Jason Turner

[temp.inst]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpqouvx_vd/{from.md → to.md} +216 -82
tmp/tmpqouvx_vd/{from.md → to.md} RENAMED
@@ -1,15 +1,30 @@
1
  ### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
2
 
3
- Unless a class template specialization has been explicitly
4
- instantiated ([[temp.explicit]]) or explicitly specialized (
5
- [[temp.expl.spec]]), the class template specialization is implicitly
6
- instantiated when the specialization is referenced in a context that
7
- requires a completely-defined object type or when the completeness of
8
- the class type affects the semantics of the program.
9
-
10
- [*Note 1*: In particular, if the semantics of an expression depend on
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  the member or base class lists of a class template specialization, the
12
  class template specialization is implicitly generated. For instance,
13
  deleting a pointer to class type depends on whether or not the class
14
  declares a destructor, and a conversion between pointers to class type
15
  depends on the inheritance relationship between the two classes
@@ -32,45 +47,66 @@ void g(D<int>* p, D<char>* pp, D<double>* ppp) {
32
  ```
33
 
34
  — *end example*]
35
 
36
  If a class template has been declared, but not defined, at the point of
37
- instantiation ([[temp.point]]), the instantiation yields an incomplete
38
- class type ([[basic.types]]).
39
 
40
  [*Example 2*:
41
 
42
  ``` cpp
43
  template<class T> class X;
44
  X<char> ch; // error: incomplete type X<char>
45
  ```
46
 
47
  — *end example*]
48
 
49
- [*Note 2*: Within a template declaration, a local class (
50
- [[class.local]]) or enumeration and the members of a local class are
51
- never considered to be entities that can be separately instantiated
52
- (this includes their default arguments, *noexcept-specifier*s, and
53
- non-static data member initializers, if any). As a result, the dependent
54
- names are looked up, the semantic constraints are checked, and any
55
- templates used are instantiated as part of the instantiation of the
56
- entity within which the local class or enumeration is
57
- declared. — *end note*]
58
 
59
- The implicit instantiation of a class template specialization causes the
60
- implicit instantiation of the declarations, but not of the definitions,
61
- default arguments, or *noexcept-specifier*s of the class member
62
- functions, member classes, scoped member enumerations, static data
63
- members, member templates, and friends; and it causes the implicit
64
- instantiation of the definitions of unscoped member enumerations and
65
- member anonymous unions. However, for the purpose of determining whether
66
- an instantiated redeclaration is valid according to  [[basic.def.odr]]
67
- and [[class.mem]], a declaration that corresponds to a definition in the
68
- template is considered to be a definition.
 
 
69
 
70
  [*Example 3*:
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  ``` cpp
73
  template<class T, class U>
74
  struct Outer {
75
  template<class X, class Y> struct Inner;
76
  template<class Y> struct Inner<T, Y>; // #1a
@@ -90,38 +126,47 @@ same partial specialization.
90
  ``` cpp
91
  template<typename T> struct Friendly {
92
  template<typename U> friend int f(U) { return sizeof(T); }
93
  };
94
  Friendly<char> fc;
95
- Friendly<float> ff; // ill-formed: produces second definition of f(U)
96
  ```
97
 
98
  — *end example*]
99
 
100
- Unless a member of a class template or a member template has been
101
- explicitly instantiated or explicitly specialized, the specialization of
102
- the member is implicitly instantiated when the specialization is
103
- referenced in a context that requires the member definition to exist; in
 
104
  particular, the initialization (and any associated side effects) of a
105
  static data member does not occur unless the static data member is
106
  itself used in a way that requires the definition of the static data
107
  member to exist.
108
 
109
- Unless a function template specialization has been explicitly
110
- instantiated or explicitly specialized, the function template
111
- specialization is implicitly instantiated when the specialization is
112
- referenced in a context that requires a function definition to exist. A
113
- function whose declaration was instantiated from a friend function
114
- definition is implicitly instantiated when it is referenced in a context
115
- that requires a function definition to exist. Unless a call is to a
116
- function template explicit specialization or to a member function of an
117
- explicitly specialized class template, a default argument for a function
118
- template or a member function of a class template is implicitly
119
- instantiated when the function is called in a context that requires the
120
- value of the default argument.
 
121
 
122
- [*Example 4*:
 
 
 
 
 
 
 
123
 
124
  ``` cpp
125
  template<class T> struct Z {
126
  void f();
127
  void g();
@@ -141,22 +186,46 @@ void h() {
141
  Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
142
  `Z<char>::f()` to be implicitly instantiated.
143
 
144
  — *end example*]
145
 
146
- Unless a variable template specialization has been explicitly
147
- instantiated or explicitly specialized, the variable template
148
- specialization is implicitly instantiated when the specialization is
149
- used. A default template argument for a variable template is implicitly
150
- instantiated when the variable template is referenced in a context that
151
- requires the value of the default argument.
152
-
153
- If the function selected by overload resolution ([[over.match]]) can be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  determined without instantiating a class template definition, it is
155
  unspecified whether that instantiation actually takes place.
156
 
157
- [*Example 5*:
158
 
159
  ``` cpp
160
  template <class T> struct S {
161
  operator int();
162
  };
@@ -173,34 +242,41 @@ void g(S<int>& sr) {
173
 
174
  — *end example*]
175
 
176
  If a function template or a member function template specialization is
177
  used in a way that involves overload resolution, a declaration of the
178
- specialization is implicitly instantiated ([[temp.over]]).
179
 
180
  An implementation shall not implicitly instantiate a function template,
181
  a variable template, a member template, a non-virtual member function, a
182
  member class, a static data member of a class template, or a
183
- substatement of a constexpr if statement ([[stmt.if]]), unless such
184
- instantiation is required. It is unspecified whether or not an
185
- implementation implicitly instantiates a virtual member function of a
186
- class template if the virtual member function would not otherwise be
187
- instantiated. The use of a template specialization in a default argument
188
- shall not cause the template to be implicitly instantiated except that a
189
- class template may be instantiated where its complete type is needed to
190
- determine the correctness of the default argument. The use of a default
191
- argument in a function call causes specializations in the default
192
- argument to be implicitly instantiated.
 
 
 
 
 
 
 
193
 
194
  Implicitly instantiated class, function, and variable template
195
  specializations are placed in the namespace where the template is
196
  defined. Implicitly instantiated specializations for members of a class
197
  template are placed in the namespace where the enclosing class template
198
  is defined. Implicitly instantiated member templates are placed in the
199
  namespace where the enclosing class or class template is defined.
200
 
201
- [*Example 6*:
202
 
203
  ``` cpp
204
  namespace N {
205
  template<class T> class List {
206
  public:
@@ -217,11 +293,11 @@ public:
217
  void g(Map<const char*,int>& m) {
218
  int i = m.get("Nicholas");
219
  }
220
  ```
221
 
222
- a call of `lt.get()` from `Map<const char*,int>::get()` would place
223
  `List<int>::get()` in the namespace `N` rather than in the global
224
  namespace.
225
 
226
  — *end example*]
227
 
@@ -230,19 +306,19 @@ argument to be used, the dependent names are looked up, the semantics
230
  constraints are checked, and the instantiation of any template used in
231
  the default argument is done as if the default argument had been an
232
  initializer used in a function template specialization with the same
233
  scope, the same template parameters and the same access as that of the
234
  function template `f` used at that point, except that the scope in which
235
- a closure type is declared ([[expr.prim.lambda.closure]]) – and
236
- therefore its associated namespaces – remain as determined from the
237
- context of the definition for the default argument. This analysis is
238
- called *default argument instantiation*. The instantiated default
239
- argument is then used as the argument of `f`.
240
 
241
  Each default argument is instantiated independently.
242
 
243
- [*Example 7*:
244
 
245
  ``` cpp
246
  template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
247
 
248
  class A { };
@@ -250,34 +326,34 @@ class A { };
250
  A zdef(A);
251
 
252
  void g(A a, A b, A c) {
253
  f(a, b, c); // no default argument instantiation
254
  f(a, b); // default argument z = zdef(T()) instantiated
255
- f(a); // ill-formed; ydef is not declared
256
  }
257
  ```
258
 
259
  — *end example*]
260
 
261
  The *noexcept-specifier* of a function template specialization is not
262
  instantiated along with the function declaration; it is instantiated
263
- when needed ([[except.spec]]). If such an *noexcept-specifier* is
264
- needed but has not yet been instantiated, the dependent names are looked
265
- up, the semantics constraints are checked, and the instantiation of any
266
  template used in the *noexcept-specifier* is done as if it were being
267
  done as part of instantiating the declaration of the specialization at
268
  that point.
269
 
270
- [*Note 3*: [[temp.point]] defines the point of instantiation of a
271
  template specialization. — *end note*]
272
 
273
  There is an *implementation-defined* quantity that specifies the limit
274
- on the total depth of recursive instantiations ([[implimits]]), which
275
  could involve more than one template. The result of an infinite
276
  recursion in instantiation is undefined.
277
 
278
- [*Example 8*:
279
 
280
  ``` cpp
281
  template<class T> class X {
282
  X<T>* p; // OK
283
  X<T*> a; // implicit generation of X<T> requires
@@ -286,5 +362,63 @@ template<class T> class X {
286
  };
287
  ```
288
 
289
  — *end example*]
290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
2
 
3
+ A template specialization E is a *declared specialization* if there is a
4
+ reachable explicit instantiation definition [[temp.explicit]] or
5
+ explicit specialization declaration [[temp.expl.spec]] for E, or if
6
+ there is a reachable explicit instantiation declaration for E and E is
7
+ not
8
+
9
+ - an inline function,
10
+ - declared with a type deduced from its initializer or return value
11
+ [[dcl.spec.auto]],
12
+ - a potentially-constant variable [[expr.const]], or
13
+ - a specialization of a templated class.
14
+
15
+ [*Note 1*: An implicit instantiation in an importing translation unit
16
+ cannot use names with internal linkage from an imported translation unit
17
+ [[basic.link]]. — *end note*]
18
+
19
+ Unless a class template specialization is a declared specialization, the
20
+ class template specialization is implicitly instantiated when the
21
+ specialization is referenced in a context that requires a
22
+ completely-defined object type or when the completeness of the class
23
+ type affects the semantics of the program.
24
+
25
+ [*Note 2*: In particular, if the semantics of an expression depend on
26
  the member or base class lists of a class template specialization, the
27
  class template specialization is implicitly generated. For instance,
28
  deleting a pointer to class type depends on whether or not the class
29
  declares a destructor, and a conversion between pointers to class type
30
  depends on the inheritance relationship between the two classes
 
47
  ```
48
 
49
  — *end example*]
50
 
51
  If a class template has been declared, but not defined, at the point of
52
+ instantiation [[temp.point]], the instantiation yields an incomplete
53
+ class type [[basic.types]].
54
 
55
  [*Example 2*:
56
 
57
  ``` cpp
58
  template<class T> class X;
59
  X<char> ch; // error: incomplete type X<char>
60
  ```
61
 
62
  — *end example*]
63
 
64
+ [*Note 3*: Within a template declaration, a local class [[class.local]]
65
+ or enumeration and the members of a local class are never considered to
66
+ be entities that can be separately instantiated (this includes their
67
+ default arguments, *noexcept-specifier*s, and non-static data member
68
+ initializers, if any, but not their *type-constraint*s or
69
+ *requires-clause*s). As a result, the dependent names are looked up, the
70
+ semantic constraints are checked, and any templates used are
71
+ instantiated as part of the instantiation of the entity within which the
72
+ local class or enumeration is declared. — *end note*]
73
 
74
+ The implicit instantiation of a class template specialization causes
75
+
76
+ - the implicit instantiation of the declarations, but not of the
77
+ definitions, of the non-deleted class member functions, member
78
+ classes, scoped member enumerations, static data members, member
79
+ templates, and friends; and
80
+ - the implicit instantiation of the definitions of deleted member
81
+ functions, unscoped member enumerations, and member anonymous unions.
82
+
83
+ The implicit instantiation of a class template specialization does not
84
+ cause the implicit instantiation of default arguments or
85
+ *noexcept-specifier*s of the class member functions.
86
 
87
  [*Example 3*:
88
 
89
+ ``` cpp
90
+ template<class T>
91
+ struct C {
92
+ void f() { T x; }
93
+ void g() = delete;
94
+ };
95
+ C<void> c; // OK, definition of C<void>::f is not instantiated at this point
96
+ template<> void C<int>::g() { } // error: redefinition of C<int>::g
97
+ ```
98
+
99
+ — *end example*]
100
+
101
+ However, for the purpose of determining whether an instantiated
102
+ redeclaration is valid according to  [[basic.def.odr]] and
103
+ [[class.mem]], a declaration that corresponds to a definition in the
104
+ template is considered to be a definition.
105
+
106
+ [*Example 4*:
107
+
108
  ``` cpp
109
  template<class T, class U>
110
  struct Outer {
111
  template<class X, class Y> struct Inner;
112
  template<class Y> struct Inner<T, Y>; // #1a
 
126
  ``` cpp
127
  template<typename T> struct Friendly {
128
  template<typename U> friend int f(U) { return sizeof(T); }
129
  };
130
  Friendly<char> fc;
131
+ Friendly<float> ff; // error: produces second definition of f(U)
132
  ```
133
 
134
  — *end example*]
135
 
136
+ Unless a member of a class template or a member template is a declared
137
+ specialization, the specialization of the member is implicitly
138
+ instantiated when the specialization is referenced in a context that
139
+ requires the member definition to exist or if the existence of the
140
+ definition of the member affects the semantics of the program; in
141
  particular, the initialization (and any associated side effects) of a
142
  static data member does not occur unless the static data member is
143
  itself used in a way that requires the definition of the static data
144
  member to exist.
145
 
146
+ Unless a function template specialization is a declared specialization,
147
+ the function template specialization is implicitly instantiated when the
148
+ specialization is referenced in a context that requires a function
149
+ definition to exist or if the existence of the definition affects the
150
+ semantics of the program. A function whose declaration was instantiated
151
+ from a friend function definition is implicitly instantiated when it is
152
+ referenced in a context that requires a function definition to exist or
153
+ if the existence of the definition affects the semantics of the program.
154
+ Unless a call is to a function template explicit specialization or to a
155
+ member function of an explicitly specialized class template, a default
156
+ argument for a function template or a member function of a class
157
+ template is implicitly instantiated when the function is called in a
158
+ context that requires the value of the default argument.
159
 
160
+ [*Note 4*: An inline function that is the subject of an explicit
161
+ instantiation declaration is not a declared specialization; the intent
162
+ is that it still be implicitly instantiated when odr-used
163
+ [[basic.def.odr]] so that the body can be considered for inlining, but
164
+ that no out-of-line copy of it be generated in the translation
165
+ unit. — *end note*]
166
+
167
+ [*Example 5*:
168
 
169
  ``` cpp
170
  template<class T> struct Z {
171
  void f();
172
  void g();
 
186
  Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
187
  `Z<char>::f()` to be implicitly instantiated.
188
 
189
  — *end example*]
190
 
191
+ Unless a variable template specialization is a declared specialization,
192
+ the variable template specialization is implicitly instantiated when it
193
+ is referenced in a context that requires a variable definition to exist
194
+ or if the existence of the definition affects the semantics of the
195
+ program. A default template argument for a variable template is
196
+ implicitly instantiated when the variable template is referenced in a
197
+ context that requires the value of the default argument.
198
+
199
+ The existence of a definition of a variable or function is considered to
200
+ affect the semantics of the program if the variable or function is
201
+ needed for constant evaluation by an expression [[expr.const]], even if
202
+ constant evaluation of the expression is not required or if constant
203
+ expression evaluation does not use the definition.
204
+
205
+ [*Example 6*:
206
+
207
+ ``` cpp
208
+ template<typename T> constexpr int f() { return T::value; }
209
+ template<bool B, typename T> void g(decltype(B ? f<T>() : 0));
210
+ template<bool B, typename T> void g(...);
211
+ template<bool B, typename T> void h(decltype(int{B ? f<T>() : 0}));
212
+ template<bool B, typename T> void h(...);
213
+ void x() {
214
+ g<false, int>(0); // OK, B ? f<T>() :\ 0 is not potentially constant evaluated
215
+ h<false, int>(0); // error, instantiates f<int> even though B evaluates to false and
216
+ // list-initialization of int from int cannot be narrowing
217
+ }
218
+ ```
219
+
220
+ — *end example*]
221
+
222
+ If the function selected by overload resolution [[over.match]] can be
223
  determined without instantiating a class template definition, it is
224
  unspecified whether that instantiation actually takes place.
225
 
226
+ [*Example 7*:
227
 
228
  ``` cpp
229
  template <class T> struct S {
230
  operator int();
231
  };
 
242
 
243
  — *end example*]
244
 
245
  If a function template or a member function template specialization is
246
  used in a way that involves overload resolution, a declaration of the
247
+ specialization is implicitly instantiated [[temp.over]].
248
 
249
  An implementation shall not implicitly instantiate a function template,
250
  a variable template, a member template, a non-virtual member function, a
251
  member class, a static data member of a class template, or a
252
+ substatement of a constexpr if statement [[stmt.if]], unless such
253
+ instantiation is required.
254
+
255
+ [*Note 5*: The instantiation of a generic lambda does not require
256
+ instantiation of substatements of a constexpr if statement within its
257
+ *compound-statement* unless the call operator template is
258
+ instantiated. *end note*]
259
+
260
+ It is unspecified whether or not an implementation implicitly
261
+ instantiates a virtual member function of a class template if the
262
+ virtual member function would not otherwise be instantiated. The use of
263
+ a template specialization in a default argument shall not cause the
264
+ template to be implicitly instantiated except that a class template may
265
+ be instantiated where its complete type is needed to determine the
266
+ correctness of the default argument. The use of a default argument in a
267
+ function call causes specializations in the default argument to be
268
+ implicitly instantiated.
269
 
270
  Implicitly instantiated class, function, and variable template
271
  specializations are placed in the namespace where the template is
272
  defined. Implicitly instantiated specializations for members of a class
273
  template are placed in the namespace where the enclosing class template
274
  is defined. Implicitly instantiated member templates are placed in the
275
  namespace where the enclosing class or class template is defined.
276
 
277
+ [*Example 8*:
278
 
279
  ``` cpp
280
  namespace N {
281
  template<class T> class List {
282
  public:
 
293
  void g(Map<const char*,int>& m) {
294
  int i = m.get("Nicholas");
295
  }
296
  ```
297
 
298
+ A call of `lt.get()` from `Map<const char*,int>::get()` would place
299
  `List<int>::get()` in the namespace `N` rather than in the global
300
  namespace.
301
 
302
  — *end example*]
303
 
 
306
  constraints are checked, and the instantiation of any template used in
307
  the default argument is done as if the default argument had been an
308
  initializer used in a function template specialization with the same
309
  scope, the same template parameters and the same access as that of the
310
  function template `f` used at that point, except that the scope in which
311
+ a closure type is declared [[expr.prim.lambda.closure]] – and therefore
312
+ its associated namespaces – remain as determined from the context of the
313
+ definition for the default argument. This analysis is called *default
314
+ argument instantiation*. The instantiated default argument is then used
315
+ as the argument of `f`.
316
 
317
  Each default argument is instantiated independently.
318
 
319
+ [*Example 9*:
320
 
321
  ``` cpp
322
  template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
323
 
324
  class A { };
 
326
  A zdef(A);
327
 
328
  void g(A a, A b, A c) {
329
  f(a, b, c); // no default argument instantiation
330
  f(a, b); // default argument z = zdef(T()) instantiated
331
+ f(a); // error: ydef is not declared
332
  }
333
  ```
334
 
335
  — *end example*]
336
 
337
  The *noexcept-specifier* of a function template specialization is not
338
  instantiated along with the function declaration; it is instantiated
339
+ when needed [[except.spec]]. If such an *noexcept-specifier* is needed
340
+ but has not yet been instantiated, the dependent names are looked up,
341
+ the semantics constraints are checked, and the instantiation of any
342
  template used in the *noexcept-specifier* is done as if it were being
343
  done as part of instantiating the declaration of the specialization at
344
  that point.
345
 
346
+ [*Note 6*: [[temp.point]] defines the point of instantiation of a
347
  template specialization. — *end note*]
348
 
349
  There is an *implementation-defined* quantity that specifies the limit
350
+ on the total depth of recursive instantiations [[implimits]], which
351
  could involve more than one template. The result of an infinite
352
  recursion in instantiation is undefined.
353
 
354
+ [*Example 10*:
355
 
356
  ``` cpp
357
  template<class T> class X {
358
  X<T>* p; // OK
359
  X<T*> a; // implicit generation of X<T> requires
 
362
  };
363
  ```
364
 
365
  — *end example*]
366
 
367
+ The *type-constraint*s and *requires-clause* of a template
368
+ specialization or member function are not instantiated along with the
369
+ specialization or function itself, even for a member function of a local
370
+ class; substitution into the atomic constraints formed from them is
371
+ instead performed as specified in [[temp.constr.decl]] and
372
+ [[temp.constr.atomic]] when determining whether the constraints are
373
+ satisfied or as specified in [[temp.constr.decl]] when comparing
374
+ declarations.
375
+
376
+ [*Note 7*: The satisfaction of constraints is determined during
377
+ template argument deduction [[temp.deduct]] and overload resolution
378
+ [[over.match]]. — *end note*]
379
+
380
+ [*Example 11*:
381
+
382
+ ``` cpp
383
+ template<typename T> concept C = sizeof(T) > 2;
384
+ template<typename T> concept D = C<T> && sizeof(T) > 4;
385
+
386
+ template<typename T> struct S {
387
+ S() requires C<T> { } // #1
388
+ S() requires D<T> { } // #2
389
+ };
390
+
391
+ S<char> s1; // error: no matching constructor
392
+ S<char[8]> s2; // OK, calls #2
393
+ ```
394
+
395
+ When `S<char>` is instantiated, both constructors are part of the
396
+ specialization. Their constraints are not satisfied, and they suppress
397
+ the implicit declaration of a default constructor for `S<char>`
398
+ [[class.default.ctor]], so there is no viable constructor for `s1`.
399
+
400
+ — *end example*]
401
+
402
+ [*Example 12*:
403
+
404
+ ``` cpp
405
+ template<typename T> struct S1 {
406
+ template<typename U>
407
+ requires false
408
+ struct Inner1; // ill-formed, no diagnostic required
409
+ };
410
+
411
+ template<typename T> struct S2 {
412
+ template<typename U>
413
+ requires (sizeof(T[-(int)sizeof(T)]) > 1)
414
+ struct Inner2; // ill-formed, no diagnostic required
415
+ };
416
+ ```
417
+
418
+ The class `S1<T>::Inner1` is ill-formed, no diagnostic required, because
419
+ it has no valid specializations. `S2` is ill-formed, no diagnostic
420
+ required, since no substitution into the constraints of its `Inner2`
421
+ template would result in a valid expression.
422
+
423
+ — *end example*]
424
+