From Jason Turner

[temp.fct]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpmffq9es8/{from.md → to.md} +180 -53
tmp/tmpmffq9es8/{from.md → to.md} RENAMED
@@ -12,14 +12,14 @@ template<class T> void sort(Array<T>&);
12
  ```
13
 
14
  — *end example*]
15
 
16
  A function template can be overloaded with other function templates and
17
- with non-template functions ([[dcl.fct]]). A non-template function is
18
- not related to a function template (i.e., it is never considered to be a
19
  specialization), even if it has the same name and type as a potentially
20
- generated function template specialization.[^5]
21
 
22
  #### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
23
 
24
  It is possible to overload function templates so that two different
25
  function template specializations have the same type.
@@ -45,16 +45,16 @@ void h(int* p) {
45
  ```
46
 
47
  — *end example*]
48
 
49
  Such specializations are distinct functions and do not violate the
50
- one-definition rule ([[basic.def.odr]]).
51
 
52
- The signature of a function template is defined in Clause 
53
- [[intro.defs]]. The names of the template parameters are significant
54
- only for establishing the relationship between the template parameters
55
- and the rest of the signature.
56
 
57
  [*Note 1*:
58
 
59
  Two distinct function templates may have identical function return types
60
  and function parameter lists, even if overload resolution alone cannot
@@ -92,120 +92,186 @@ template parameters, but it is possible for an expression to reference a
92
  type parameter. For example, a template type parameter can be used in
93
  the `sizeof` operator. — *end note*]
94
 
95
  Two expressions involving template parameters are considered
96
  *equivalent* if two function definitions containing the expressions
97
- would satisfy the one-definition rule ([[basic.def.odr]]), except that
98
- the tokens used to name the template parameters may differ as long as a
99
  token used to name a template parameter in one expression is replaced by
100
  another token that names the same template parameter in the other
101
- expression. For determining whether two dependent names ([[temp.dep]])
102
- are equivalent, only the name itself is considered, not the result of
103
- name lookup in the context of the template. If multiple declarations of
104
- the same function template differ in the result of this name lookup, the
105
- result for the first declaration is used.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  [*Example 3*:
108
 
109
  ``` cpp
110
  template <int I, int J> void f(A<I+J>); // #1
111
  template <int K, int L> void f(A<K+L>); // same as #1
112
 
113
  template <class T> decltype(g(T())) h();
114
  int g(int);
115
- template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
116
- { return g(T()); } // ...although the lookup here does find g(int)
117
  int i = h<int>(); // template argument substitution fails; g(int)
118
  // was not in scope at the first declaration of h()
 
 
 
 
 
 
 
 
119
  ```
120
 
121
  — *end example*]
122
 
123
- Two expressions involving template parameters that are not equivalent
124
- are *functionally equivalent* if, for any given set of template
125
- arguments, the evaluation of the expression results in the same value.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  Two function templates are *equivalent* if they are declared in the same
128
- scope, have the same name, have identical template parameter lists, and
129
- have return types and parameter lists that are equivalent using the
130
- rules described above to compare expressions involving template
131
- parameters. Two function templates are *functionally equivalent* if they
132
- are equivalent except that one or more expressions that involve template
133
- parameters in the return types and parameter lists are functionally
134
- equivalent using the rules described above to compare expressions
135
- involving template parameters. If a program contains declarations of
136
- function templates that are functionally equivalent but not equivalent,
137
- the program is ill-formed, no diagnostic required.
 
 
138
 
139
- [*Note 3*:
140
 
141
  This rule guarantees that equivalent declarations will be linked with
142
  one another, while not requiring implementations to use heroic efforts
143
  to guarantee that functionally equivalent declarations will be treated
144
  as distinct. For example, the last two declarations are functionally
145
  equivalent and would cause a program to be ill-formed:
146
 
147
  ``` cpp
148
- // Guaranteed to be the same
149
  template <int I> void f(A<I>, A<I+10>);
150
  template <int I> void f(A<I>, A<I+10>);
151
 
152
- // Guaranteed to be different
153
  template <int I> void f(A<I>, A<I+10>);
154
  template <int I> void f(A<I>, A<I+11>);
155
 
156
- // Ill-formed, no diagnostic required
157
  template <int I> void f(A<I>, A<I+10>);
158
  template <int I> void f(A<I>, A<I+1+2+3+4>);
159
  ```
160
 
161
  — *end note*]
162
 
163
  #### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
164
 
165
  If a function template is overloaded, the use of a function template
166
- specialization might be ambiguous because template argument deduction (
167
- [[temp.deduct]]) may associate the function template specialization with
168
  more than one function template declaration. *Partial ordering* of
169
  overloaded function template declarations is used in the following
170
  contexts to select the function template to which a function template
171
  specialization refers:
172
 
173
  - during overload resolution for a call to a function template
174
- specialization ([[over.match.best]]);
175
  - when the address of a function template specialization is taken;
176
  - when a placement operator delete that is a function template
177
  specialization is selected to match a placement operator new (
178
  [[basic.stc.dynamic.deallocation]], [[expr.new]]);
179
- - when a friend function declaration ([[temp.friend]]), an explicit
180
- instantiation ([[temp.explicit]]) or an explicit specialization (
181
- [[temp.expl.spec]]) refers to a function template specialization.
182
 
183
  Partial ordering selects which of two function templates is more
184
  specialized than the other by transforming each template in turn (see
185
  next paragraph) and performing template argument deduction using the
186
  function type. The deduction process determines whether one of the
187
  templates is more specialized than the other. If so, the more
188
  specialized template is the one chosen by the partial ordering process.
 
 
189
 
190
  To produce the transformed template, for each type, non-type, or
191
- template template parameter (including template parameter packs (
192
- [[temp.variadic]]) thereof) synthesize a unique type, value, or class
193
  template respectively and substitute it for each occurrence of that
194
  parameter in the function type of the template.
195
 
196
  [*Note 1*: The type replacing the placeholder in the type of the value
197
  synthesized for a non-type template parameter is also a unique
198
  synthesized type. — *end note*]
199
 
200
- If only one of the function templates *M* is a non-static member of some
201
- class *A*, *M* is considered to have a new first parameter inserted in
202
- its function parameter list. Given cv as the cv-qualifiers of *M* (if
203
- any), the new parameter is of type “rvalue reference to cv *A*” if the
204
- optional *ref-qualifier* of *M* is `&&` or if *M* has no *ref-qualifier*
205
- and the first parameter of the other template has rvalue reference type.
206
- Otherwise, the new parameter is of type “lvalue reference to cv *A*”.
 
 
 
 
 
 
 
 
 
207
 
208
  [*Note 2*: This allows a non-static member to be ordered with respect
209
  to a non-member function and for the results to be equivalent to the
210
  ordering of two equivalent non-members. — *end note*]
211
 
@@ -223,11 +289,11 @@ template<class T, class R> int operator*(T&, R&); // #2
223
  // template<class R> int operator*(B<A>&, R&);\quad\quad\quad// #1a
224
 
225
  int main() {
226
  A a;
227
  B<A> b;
228
- b * a; // calls #1a
229
  }
230
  ```
231
 
232
  — *end example*]
233
 
@@ -264,12 +330,12 @@ void m() {
264
 
265
  — *end example*]
266
 
267
  [*Note 3*:
268
 
269
- Since partial ordering in a call context considers only parameters for
270
- which there are explicit call arguments, some parameters are ignored
271
  (namely, function parameter packs, parameters with default arguments,
272
  and ellipsis parameters).
273
 
274
  [*Example 3*:
275
 
@@ -316,14 +382,75 @@ template<class T, class... U> void f(T, U...); // #1
316
  template<class T > void f(T); // #2
317
  template<class T, class... U> void g(T*, U...); // #3
318
  template<class T > void g(T); // #4
319
 
320
  void h(int i) {
321
- f(&i); // error: ambiguous
322
  g(&i); // OK: calls #3
323
  }
324
  ```
325
 
326
  — *end example*]
327
 
328
  — *end note*]
329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ```
13
 
14
  — *end example*]
15
 
16
  A function template can be overloaded with other function templates and
17
+ with non-template functions [[dcl.fct]]. A non-template function is not
18
+ related to a function template (i.e., it is never considered to be a
19
  specialization), even if it has the same name and type as a potentially
20
+ generated function template specialization.[^9]
21
 
22
  #### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
23
 
24
  It is possible to overload function templates so that two different
25
  function template specializations have the same type.
 
45
  ```
46
 
47
  — *end example*]
48
 
49
  Such specializations are distinct functions and do not violate the
50
+ one-definition rule [[basic.def.odr]].
51
 
52
+ The signature of a function template is defined in [[intro.defs]]. The
53
+ names of the template parameters are significant only for establishing
54
+ the relationship between the template parameters and the rest of the
55
+ signature.
56
 
57
  [*Note 1*:
58
 
59
  Two distinct function templates may have identical function return types
60
  and function parameter lists, even if overload resolution alone cannot
 
92
  type parameter. For example, a template type parameter can be used in
93
  the `sizeof` operator. — *end note*]
94
 
95
  Two expressions involving template parameters are considered
96
  *equivalent* if two function definitions containing the expressions
97
+ would satisfy the one-definition rule [[basic.def.odr]], except that the
98
+ tokens used to name the template parameters may differ as long as a
99
  token used to name a template parameter in one expression is replaced by
100
  another token that names the same template parameter in the other
101
+ expression. Two unevaluated operands that do not involve template
102
+ parameters are considered equivalent if two function definitions
103
+ containing the expressions would satisfy the one-definition rule, except
104
+ that the tokens used to name types and declarations may differ as long
105
+ as they name the same entities, and the tokens used to form concept-ids
106
+ may differ as long as the two *template-id*s are the same [[temp.type]].
107
+
108
+ [*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
109
+ type. — *end note*]
110
+
111
+ Two *lambda-expression*s are never considered equivalent.
112
+
113
+ [*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
114
+ signature of a function template with external linkage. — *end note*]
115
+
116
+ For determining whether two dependent names [[temp.dep]] are equivalent,
117
+ only the name itself is considered, not the result of name lookup in the
118
+ context of the template. If multiple declarations of the same function
119
+ template differ in the result of this name lookup, the result for the
120
+ first declaration is used.
121
 
122
  [*Example 3*:
123
 
124
  ``` cpp
125
  template <int I, int J> void f(A<I+J>); // #1
126
  template <int K, int L> void f(A<K+L>); // same as #1
127
 
128
  template <class T> decltype(g(T())) h();
129
  int g(int);
130
+ template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
131
+ { return g(T()); } // …{} although the lookup here does find g(int)
132
  int i = h<int>(); // template argument substitution fails; g(int)
133
  // was not in scope at the first declaration of h()
134
+
135
+ // ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
136
+ template <int N> void foo(const char (*s)[([]{}, N)]);
137
+ template <int N> void foo(const char (*s)[([]{}, N)]);
138
+
139
+ // two different declarations because the non-dependent portions are not considered equivalent
140
+ template <class T> void spam(decltype([]{}) (*s)[sizeof(T)]);
141
+ template <class T> void spam(decltype([]{}) (*s)[sizeof(T)]);
142
  ```
143
 
144
  — *end example*]
145
 
146
+ Two potentially-evaluated expressions involving template parameters that
147
+ are not equivalent are *functionally equivalent* if, for any given set
148
+ of template arguments, the evaluation of the expression results in the
149
+ same value. Two unevaluated operands that are not equivalent are
150
+ functionally equivalent if, for any given set of template arguments, the
151
+ expressions perform the same operations in the same order with the same
152
+ entities.
153
+
154
+ [*Note 5*: For instance, one could have redundant
155
+ parentheses. — *end note*]
156
+
157
+ Two *template-head*s are *equivalent* if their
158
+ *template-parameter-list*s have the same length, corresponding
159
+ *template-parameter*s are equivalent and are both declared with
160
+ *type-constraint*s that are equivalent if either *template-parameter* is
161
+ declared with a *type-constraint*, and if either *template-head* has a
162
+ *requires-clause*, they both have *requires-clause*s and the
163
+ corresponding *constraint-expression*s are equivalent. Two
164
+ *template-parameter*s are *equivalent* under the following conditions:
165
+
166
+ - they declare template parameters of the same kind,
167
+ - if either declares a template parameter pack, they both do,
168
+ - if they declare non-type template parameters, they have equivalent
169
+ types ignoring the use of *type-constraint*s for placeholder types,
170
+ and
171
+ - if they declare template template parameters, their template
172
+ parameters are equivalent.
173
+
174
+ When determining whether types or *type-constraint*s are equivalent, the
175
+ rules above are used to compare expressions involving template
176
+ parameters. Two *template-head*s are *functionally equivalent* if they
177
+ accept and are satisfied by [[temp.constr.constr]] the same set of
178
+ template argument lists.
179
 
180
  Two function templates are *equivalent* if they are declared in the same
181
+ scope, have the same name, have equivalent *template-head*s, and have
182
+ return types, parameter lists, and trailing *requires-clause*s (if any)
183
+ that are equivalent using the rules described above to compare
184
+ expressions involving template parameters. Two function templates are
185
+ *functionally equivalent* if they are declared in the same scope, have
186
+ the same name, accept and are satisfied by the same set of template
187
+ argument lists, and have return types and parameter lists that are
188
+ functionally equivalent using the rules described above to compare
189
+ expressions involving template parameters. If the validity or meaning of
190
+ the program depends on whether two constructs are equivalent, and they
191
+ are functionally equivalent but not equivalent, the program is
192
+ ill-formed, no diagnostic required.
193
 
194
+ [*Note 6*:
195
 
196
  This rule guarantees that equivalent declarations will be linked with
197
  one another, while not requiring implementations to use heroic efforts
198
  to guarantee that functionally equivalent declarations will be treated
199
  as distinct. For example, the last two declarations are functionally
200
  equivalent and would cause a program to be ill-formed:
201
 
202
  ``` cpp
203
+ // guaranteed to be the same
204
  template <int I> void f(A<I>, A<I+10>);
205
  template <int I> void f(A<I>, A<I+10>);
206
 
207
+ // guaranteed to be different
208
  template <int I> void f(A<I>, A<I+10>);
209
  template <int I> void f(A<I>, A<I+11>);
210
 
211
+ // ill-formed, no diagnostic required
212
  template <int I> void f(A<I>, A<I+10>);
213
  template <int I> void f(A<I>, A<I+1+2+3+4>);
214
  ```
215
 
216
  — *end note*]
217
 
218
  #### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
219
 
220
  If a function template is overloaded, the use of a function template
221
+ specialization might be ambiguous because template argument deduction
222
+ [[temp.deduct]] may associate the function template specialization with
223
  more than one function template declaration. *Partial ordering* of
224
  overloaded function template declarations is used in the following
225
  contexts to select the function template to which a function template
226
  specialization refers:
227
 
228
  - during overload resolution for a call to a function template
229
+ specialization [[over.match.best]];
230
  - when the address of a function template specialization is taken;
231
  - when a placement operator delete that is a function template
232
  specialization is selected to match a placement operator new (
233
  [[basic.stc.dynamic.deallocation]], [[expr.new]]);
234
+ - when a friend function declaration [[temp.friend]], an explicit
235
+ instantiation [[temp.explicit]] or an explicit specialization
236
+ [[temp.expl.spec]] refers to a function template specialization.
237
 
238
  Partial ordering selects which of two function templates is more
239
  specialized than the other by transforming each template in turn (see
240
  next paragraph) and performing template argument deduction using the
241
  function type. The deduction process determines whether one of the
242
  templates is more specialized than the other. If so, the more
243
  specialized template is the one chosen by the partial ordering process.
244
+ If both deductions succeed, the partial ordering selects the more
245
+ constrained template (if one exists) as determined below.
246
 
247
  To produce the transformed template, for each type, non-type, or
248
+ template template parameter (including template parameter packs
249
+ [[temp.variadic]] thereof) synthesize a unique type, value, or class
250
  template respectively and substitute it for each occurrence of that
251
  parameter in the function type of the template.
252
 
253
  [*Note 1*: The type replacing the placeholder in the type of the value
254
  synthesized for a non-type template parameter is also a unique
255
  synthesized type. — *end note*]
256
 
257
+ Each function template M that is a member function is considered to have
258
+ a new first parameter of type X(M), described below, inserted in its
259
+ function parameter list. If exactly one of the function templates was
260
+ considered by overload resolution via a rewritten candidate
261
+ [[over.match.oper]] with a reversed order of parameters, then the order
262
+ of the function parameters in its transformed template is reversed. For
263
+ a function template M with cv-qualifiers cv that is a member of a class
264
+ A:
265
+
266
+ - The type X(M) is “rvalue reference to cv A” if the optional
267
+ *ref-qualifier* of M is `&&` or if M has no *ref-qualifier* and the
268
+ positionally-corresponding parameter of the other transformed template
269
+ has rvalue reference type; if this determination depends recursively
270
+ upon whether X(M) is an rvalue reference type, it is not considered to
271
+ have rvalue reference type.
272
+ - Otherwise, X(M) is “lvalue reference to cv A”.
273
 
274
  [*Note 2*: This allows a non-static member to be ordered with respect
275
  to a non-member function and for the results to be equivalent to the
276
  ordering of two equivalent non-members. — *end note*]
277
 
 
289
  // template<class R> int operator*(B<A>&, R&);\quad\quad\quad// #1a
290
 
291
  int main() {
292
  A a;
293
  B<A> b;
294
+ b * a; // calls #1
295
  }
296
  ```
297
 
298
  — *end example*]
299
 
 
330
 
331
  — *end example*]
332
 
333
  [*Note 3*:
334
 
335
+ Since, in a call context, such type deduction considers only parameters
336
+ for which there are explicit call arguments, some parameters are ignored
337
  (namely, function parameter packs, parameters with default arguments,
338
  and ellipsis parameters).
339
 
340
  [*Example 3*:
341
 
 
382
  template<class T > void f(T); // #2
383
  template<class T, class... U> void g(T*, U...); // #3
384
  template<class T > void g(T); // #4
385
 
386
  void h(int i) {
387
+ f(&i); // OK: calls #2
388
  g(&i); // OK: calls #3
389
  }
390
  ```
391
 
392
  — *end example*]
393
 
394
  — *end note*]
395
 
396
+ If deduction against the other template succeeds for both transformed
397
+ templates, constraints can be considered as follows:
398
+
399
+ - If their *template-parameter-list*s (possibly including
400
+ *template-parameter*s invented for an abbreviated function template
401
+ [[dcl.fct]]) or function parameter lists differ in length, neither
402
+ template is more specialized than the other.
403
+ - Otherwise:
404
+ - If exactly one of the templates was considered by overload
405
+ resolution via a rewritten candidate with reversed order of
406
+ parameters:
407
+ - If, for either template, some of the template parameters are not
408
+ deducible from their function parameters, neither template is more
409
+ specialized than the other.
410
+ - If there is either no reordering or more than one reordering of
411
+ the associated *template-parameter-list* such that
412
+ - the corresponding *template-parameter*s of the
413
+ *template-parameter-list*s are equivalent and
414
+ - the function parameters that positionally correspond between the
415
+ two templates are of the same type,
416
+
417
+ neither template is more specialized than the other.
418
+ - Otherwise, if the corresponding *template-parameter*s of the
419
+ *template-parameter-list*s are not equivalent [[temp.over.link]] or
420
+ if the function parameters that positionally correspond between the
421
+ two templates are not of the same type, neither template is more
422
+ specialized than the other.
423
+ - Otherwise, if the context in which the partial ordering is done is
424
+ that of a call to a conversion function and the return types of the
425
+ templates are not the same, then neither template is more specialized
426
+ than the other.
427
+ - Otherwise, if one template is more constrained than the other
428
+ [[temp.constr.order]], the more constrained template is more
429
+ specialized than the other.
430
+ - Otherwise, neither template is more specialized than the other.
431
+
432
+ [*Example 6*:
433
+
434
+ ``` cpp
435
+ template <typename> constexpr bool True = true;
436
+ template <typename T> concept C = True<T>;
437
+
438
+ void f(C auto &, auto &) = delete;
439
+ template <C Q> void f(Q &, C auto &);
440
+
441
+ void g(struct A *ap, struct B *bp) {
442
+ f(*ap, *bp); // OK: Can use different methods to produce template parameters
443
+ }
444
+
445
+ template <typename T, typename U> struct X {};
446
+
447
+ template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
448
+ template <C T, C U, C V> bool operator==(T, X<U, V>);
449
+
450
+ void h() {
451
+ X<void *, int>{} == 0; // OK: Correspondence of [T, U, V] and [U, V, T]
452
+ }
453
+ ```
454
+
455
+ — *end example*]
456
+