From Jason Turner

[temp.inst]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpoqv4w795/{from.md → to.md} +125 -78
tmp/tmpoqv4w795/{from.md → to.md} RENAMED
@@ -3,21 +3,73 @@
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. The implicit
9
- instantiation of a class template specialization causes the implicit
10
- instantiation of the declarations, but not of the definitions, default
11
- arguments, or *exception-specification*s of the class member functions,
12
- member classes, scoped member enumerations, static data members and
13
- member templates; and it causes the implicit instantiation of the
14
- definitions of unscoped member enumerations and member anonymous unions.
15
- However, for the purpose of determining whether an instantiated
16
- redeclaration of a member is valid according to  [[class.mem]], a
17
- declaration that corresponds to a definition in the template is
18
- considered to be a definition.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  ``` cpp
21
  template<class T, class U>
22
  struct Outer {
23
  template<class X, class Y> struct Inner;
@@ -33,28 +85,43 @@ Outer<int, int> outer; // error at #2
33
  defined but noted as being associated with a definition in
34
  `Outer<T, U>`.) \#2 is also a redeclaration of \#1a. It is noted as
35
  associated with a definition, so it is an invalid redeclaration of the
36
  same partial specialization.
37
 
 
 
 
 
 
 
 
 
 
 
38
  Unless a member of a class template or a member template has been
39
  explicitly instantiated or explicitly specialized, the specialization of
40
  the member is implicitly instantiated when the specialization is
41
  referenced in a context that requires the member definition to exist; in
42
- particular, the initialization (and any associated side-effects) of a
43
  static data member does not occur unless the static data member is
44
  itself used in a way that requires the definition of the static data
45
  member to exist.
46
 
47
  Unless a function template specialization has been explicitly
48
  instantiated or explicitly specialized, the function template
49
  specialization is implicitly instantiated when the specialization is
50
- referenced in a context that requires a function definition to exist.
51
- Unless a call is to a function template explicit specialization or to a
52
- member function of an explicitly specialized class template, a default
53
- argument for a function template or a member function of a class
54
- template is implicitly instantiated when the function is called in a
55
- context that requires the value of the default argument.
 
 
 
 
 
56
 
57
  ``` cpp
58
  template<class T> struct Z {
59
  void f();
60
  void g();
@@ -72,47 +139,25 @@ void h() {
72
  ```
73
 
74
  Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
75
  `Z<char>::f()` to be implicitly instantiated.
76
 
 
 
77
  Unless a variable template specialization has been explicitly
78
  instantiated or explicitly specialized, the variable template
79
  specialization is implicitly instantiated when the specialization is
80
  used. A default template argument for a variable template is implicitly
81
  instantiated when the variable template is referenced in a context that
82
  requires the value of the default argument.
83
 
84
- A class template specialization is implicitly instantiated if the class
85
- type is used in a context that requires a completely-defined object type
86
- or if the completeness of the class type might affect the semantics of
87
- the program. In particular, if the semantics of an expression depend on
88
- the member or base class lists of a class template specialization, the
89
- class template specialization is implicitly generated. For instance,
90
- deleting a pointer to class type depends on whether or not the class
91
- declares a destructor, and conversion between pointer to class types
92
- depends on the inheritance relationship between the two classes
93
- involved.
94
-
95
- ``` cpp
96
- template<class T> class B { /* ... */ };
97
- template<class T> class D : public B<T> { /* ... */ };
98
-
99
- void f(void*);
100
- void f(B<int>*);
101
-
102
- void g(D<int>* p, D<char>* pp, D<double>* ppp) {
103
- f(p); // instantiation of D<int> required: call f(B<int>*)
104
- B<char>* q = pp; // instantiation of D<char> required:
105
- // convert D<char>* to B<char>*
106
- delete ppp; // instantiation of D<double> required
107
- }
108
- ```
109
-
110
- If the overload resolution process can determine the correct function to
111
- call without instantiating a class template definition, it is
112
  unspecified whether that instantiation actually takes place.
113
 
 
 
114
  ``` cpp
115
  template <class T> struct S {
116
  operator int();
117
  };
118
 
@@ -124,31 +169,21 @@ void g(S<int>& sr) {
124
  f(sr); // instantiation of S<int> allowed but not required
125
  // instantiation of S<float> allowed but not required
126
  };
127
  ```
128
 
129
- If an implicit instantiation of a class template specialization is
130
- required and the template is declared but not defined, the program is
131
- ill-formed.
132
-
133
- ``` cpp
134
- template<class T> class X;
135
-
136
- X<char> ch; // error: definition of X required
137
- ```
138
-
139
- The implicit instantiation of a class template does not cause any static
140
- data members of that class to be implicitly instantiated.
141
 
142
  If a function template or a member function template specialization is
143
  used in a way that involves overload resolution, a declaration of the
144
  specialization is implicitly instantiated ([[temp.over]]).
145
 
146
  An implementation shall not implicitly instantiate a function template,
147
  a variable template, a member template, a non-virtual member function, a
148
- member class, or a static data member of a class template that does not
149
- require instantiation. It is unspecified whether or not an
 
150
  implementation implicitly instantiates a virtual member function of a
151
  class template if the virtual member function would not otherwise be
152
  instantiated. The use of a template specialization in a default argument
153
  shall not cause the template to be implicitly instantiated except that a
154
  class template may be instantiated where its complete type is needed to
@@ -161,10 +196,12 @@ specializations are placed in the namespace where the template is
161
  defined. Implicitly instantiated specializations for members of a class
162
  template are placed in the namespace where the enclosing class template
163
  is defined. Implicitly instantiated member templates are placed in the
164
  namespace where the enclosing class or class template is defined.
165
 
 
 
166
  ``` cpp
167
  namespace N {
168
  template<class T> class List {
169
  public:
170
  T* get();
@@ -184,25 +221,29 @@ void g(Map<const char*,int>& m) {
184
 
185
  a call of `lt.get()` from `Map<const char*,int>::get()` would place
186
  `List<int>::get()` in the namespace `N` rather than in the global
187
  namespace.
188
 
 
 
189
  If a function template `f` is called in a way that requires a default
190
  argument to be used, the dependent names are looked up, the semantics
191
  constraints are checked, and the instantiation of any template used in
192
  the default argument is done as if the default argument had been an
193
  initializer used in a function template specialization with the same
194
  scope, the same template parameters and the same access as that of the
195
  function template `f` used at that point, except that the scope in which
196
- a closure type is declared ([[expr.prim.lambda]]) – and therefore its
197
- associated namespaces – remain as determined from the context of the
198
- definition for the default argument. This analysis is called *default
199
- argument instantiation*. The instantiated default argument is then used
200
- as the argument of `f`.
201
 
202
  Each default argument is instantiated independently.
203
 
 
 
204
  ``` cpp
205
  template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
206
 
207
  class A { };
208
 
@@ -213,31 +254,37 @@ void g(A a, A b, A c) {
213
  f(a, b); // default argument z = zdef(T()) instantiated
214
  f(a); // ill-formed; ydef is not declared
215
  }
216
  ```
217
 
218
- The *exception-specification* of a function template specialization is
219
- not instantiated along with the function declaration; it is instantiated
220
- when needed ([[except.spec]]). If such an *exception-specification* is
 
 
221
  needed but has not yet been instantiated, the dependent names are looked
222
  up, the semantics constraints are checked, and the instantiation of any
223
- template used in the *exception-specification* is done as if it were
224
- being done as part of instantiating the declaration of the
225
- specialization at that point.
226
 
227
- [[temp.point]] defines the point of instantiation of a template
228
- specialization.
229
 
230
- There is an implementation-defined quantity that specifies the limit on
231
- the total depth of recursive instantiations, which could involve more
232
- than one template. The result of an infinite recursion in instantiation
233
- is undefined.
 
 
234
 
235
  ``` cpp
236
  template<class T> class X {
237
  X<T>* p; // OK
238
  X<T*> a; // implicit generation of X<T> requires
239
  // the implicit instantiation of X<T*> which requires
240
- // the implicit instantiation of X<T**> which ...
241
  };
242
  ```
243
 
 
 
 
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
16
+ involved. *end note*]
17
+
18
+ [*Example 1*:
19
+
20
+ ``` cpp
21
+ template<class T> class B { ... };
22
+ template<class T> class D : public B<T> { ... };
23
+
24
+ void f(void*);
25
+ void f(B<int>*);
26
+
27
+ void g(D<int>* p, D<char>* pp, D<double>* ppp) {
28
+ f(p); // instantiation of D<int> required: call f(B<int>*)
29
+ B<char>* q = pp; // instantiation of D<char> required: convert D<char>* to B<char>*
30
+ delete ppp; // instantiation of D<double> required
31
+ }
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;
 
85
  defined but noted as being associated with a definition in
86
  `Outer<T, U>`.) \#2 is also a redeclaration of \#1a. It is noted as
87
  associated with a definition, so it is an invalid redeclaration of the
88
  same partial specialization.
89
 
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();
 
139
  ```
140
 
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
  };
163
 
 
169
  f(sr); // instantiation of S<int> allowed but not required
170
  // instantiation of S<float> allowed but not required
171
  };
172
  ```
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
 
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:
207
  T* get();
 
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
+
228
  If a function template `f` is called in a way that requires a default
229
  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 { };
249
 
 
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
284
  // the implicit instantiation of X<T*> which requires
285
+ // the implicit instantiation of X<T**> which
286
  };
287
  ```
288
 
289
+ — *end example*]
290
+