- tmp/tmpqs4fl7w8/{from.md → to.md} +456 -301
tmp/tmpqs4fl7w8/{from.md → to.md}
RENAMED
|
@@ -17,20 +17,41 @@ template-parameter-list:
|
|
| 17 |
The `>` token following the of a may be the product of replacing a
|
| 18 |
`>{>}` token by two consecutive `>` tokens ([[temp.names]]).
|
| 19 |
|
| 20 |
The *declaration* in a *template-declaration* shall
|
| 21 |
|
| 22 |
-
- declare or define a function or a
|
| 23 |
- define a member function, a member class, a member enumeration, or a
|
| 24 |
static data member of a class template or of a class nested within a
|
| 25 |
class template, or
|
| 26 |
- define a member template of a class or class template, or
|
| 27 |
- be an *alias-declaration*.
|
| 28 |
|
| 29 |
A *template-declaration* is a *declaration*. A *template-declaration* is
|
| 30 |
-
also a definition if its *declaration* defines a function, a class,
|
| 31 |
-
static data member.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
A *template-declaration* can appear only as a namespace scope or class
|
| 34 |
scope declaration. In a function template declaration, the last
|
| 35 |
component of the *declarator-id* shall not be a *template-id*. That last
|
| 36 |
component may be an *identifier*, an *operator-function-id*, a
|
|
@@ -60,20 +81,21 @@ and must also obey the one definition rule.
|
|
| 60 |
|
| 61 |
A class template shall not have the same name as any other template,
|
| 62 |
class, function, variable, enumeration, enumerator, namespace, or type
|
| 63 |
in the same scope ([[basic.scope]]), except as specified in (
|
| 64 |
[[temp.class.spec]]). Except that a function template can be overloaded
|
| 65 |
-
either by
|
| 66 |
-
function templates with the same name ([[temp.over]]), a template
|
| 67 |
-
declared in namespace scope or in class scope shall be unique in
|
| 68 |
-
scope.
|
| 69 |
|
| 70 |
-
A function template, member function of a class template,
|
| 71 |
-
member of a class template shall be defined in
|
| 72 |
-
which it is implicitly instantiated (
|
| 73 |
-
corresponding specialization is explicitly
|
| 74 |
-
[[temp.explicit]]) in some translation unit; no
|
|
|
|
| 75 |
|
| 76 |
## Template parameters <a id="temp.param">[[temp.param]]</a>
|
| 77 |
|
| 78 |
The syntax for *template-parameter*s is:
|
| 79 |
|
|
@@ -98,11 +120,12 @@ The `>` token following the of a may be the product of replacing a
|
|
| 98 |
|
| 99 |
There is no semantic difference between `class` and `typename` in a
|
| 100 |
*template-parameter*. `typename` followed by an *unqualified-id* names a
|
| 101 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 102 |
the type in a non-type [^1] *parameter-declaration*. A storage class
|
| 103 |
-
shall not be specified in a *template-parameter* declaration.
|
|
|
|
| 104 |
parameter may be a class template. For example,
|
| 105 |
|
| 106 |
``` cpp
|
| 107 |
template<class T> class myarray { /* ... */ };
|
| 108 |
|
|
@@ -227,18 +250,19 @@ default *template-argument*, each subsequent *template-parameter* shall
|
|
| 227 |
either have a default *template-argument* supplied or be a template
|
| 228 |
parameter pack. If a *template-parameter* of a primary class template or
|
| 229 |
alias template is a template parameter pack, it shall be the last
|
| 230 |
*template-parameter*. A template parameter pack of a function template
|
| 231 |
shall not be followed by another template parameter unless that template
|
| 232 |
-
parameter can be deduced
|
|
|
|
| 233 |
|
| 234 |
``` cpp
|
| 235 |
template<class T1 = int, class T2> class B; // error
|
| 236 |
|
| 237 |
-
// U
|
| 238 |
-
template<class... T, class... U> void f() { }
|
| 239 |
-
template<class... T, class U> void g() { }
|
| 240 |
```
|
| 241 |
|
| 242 |
A *template-parameter* shall not be given default arguments by two
|
| 243 |
different declarations in the same scope.
|
| 244 |
|
|
@@ -294,11 +318,11 @@ expansion shall not expand a parameter pack declared in the same
|
|
| 294 |
template <class... Types> class Tuple; // Types is a template type parameter pack
|
| 295 |
// but not a pack expansion
|
| 296 |
template <class T, int... Dims> struct multi_array; // Dims is a non-type template parameter pack
|
| 297 |
// but not a pack expansion
|
| 298 |
template<class... T> struct value_holder {
|
| 299 |
-
template<T... Values> apply { };
|
| 300 |
// and a pack expansion
|
| 301 |
};
|
| 302 |
template<class... T, T... Values> struct static_array;// error: Values expands template type parameter
|
| 303 |
// pack T within the same template parameter list
|
| 304 |
```
|
|
@@ -346,13 +370,13 @@ For a *template-name* to be explicitly qualified by the template
|
|
| 346 |
arguments, the name must be known to refer to a template.
|
| 347 |
|
| 348 |
After name lookup ([[basic.lookup]]) finds that a name is a
|
| 349 |
*template-name* or that an *operator-function-id* or a
|
| 350 |
*literal-operator-id* refers to a set of overloaded functions any member
|
| 351 |
-
of which is a function template if this is followed by a `<`, the `<`
|
| 352 |
-
always taken as the delimiter of a *template-argument-list* and never
|
| 353 |
-
the less-than operator. When parsing a *template-argument-list*, the
|
| 354 |
first non-nested `>`[^2] is taken as the ending delimiter rather than a
|
| 355 |
greater-than operator. Similarly, the first non-nested `>{>}` is treated
|
| 356 |
as two consecutive but distinct `>` tokens, the first of which is taken
|
| 357 |
as the end of the and completes the . The second `>` token produced by
|
| 358 |
this replacement rule may terminate an enclosing construct or it may be
|
|
@@ -417,11 +441,11 @@ template <class T> struct B {
|
|
| 417 |
template <class T2> struct C { };
|
| 418 |
};
|
| 419 |
|
| 420 |
// OK: T::template C names a class template:
|
| 421 |
template <class T, template <class X> class TT = T::template C> struct D { };
|
| 422 |
-
D<
|
| 423 |
```
|
| 424 |
|
| 425 |
A *simple-template-id* that names a class template specialization is a
|
| 426 |
*class-name* (Clause [[class]]).
|
| 427 |
|
|
@@ -602,22 +626,24 @@ shall be one of:
|
|
| 602 |
- for a non-type *template-parameter* of integral or enumeration type, a
|
| 603 |
converted constant expression ([[expr.const]]) of the type of the
|
| 604 |
*template-parameter*; or
|
| 605 |
- the name of a non-type *template-parameter*; or
|
| 606 |
- a constant expression ([[expr.const]]) that designates the address of
|
| 607 |
-
|
| 608 |
-
linkage or a function with external or internal linkage,
|
| 609 |
-
function templates and function *template-id*s but excluding
|
| 610 |
non-static class members, expressed (ignoring parentheses) as `&`
|
| 611 |
-
*id-expression*,
|
| 612 |
-
|
|
|
|
| 613 |
*template-parameter* is a reference; or
|
| 614 |
- a constant expression that evaluates to a null pointer value (
|
| 615 |
[[conv.ptr]]); or
|
| 616 |
- a constant expression that evaluates to a null member pointer value (
|
| 617 |
[[conv.mem]]); or
|
| 618 |
-
- a pointer to member expressed as described in [[expr.unary.op]]
|
|
|
|
| 619 |
|
| 620 |
A string literal ([[lex.string]]) does not satisfy the requirements of
|
| 621 |
any of these categories and thus is not an acceptable
|
| 622 |
*template-argument*.
|
| 623 |
|
|
@@ -671,18 +697,17 @@ the program is ill-formed.
|
|
| 671 |
- for a non-type *template-parameter* of type pointer to object,
|
| 672 |
qualification conversions ([[conv.qual]]) and the array-to-pointer
|
| 673 |
conversion ([[conv.array]]) are applied; if the *template-argument*
|
| 674 |
is of type `std::nullptr_t`, the null pointer conversion (
|
| 675 |
[[conv.ptr]]) is applied. In particular, neither the null pointer
|
| 676 |
-
conversion for a zero-valued
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
*template-
|
| 681 |
-
|
| 682 |
-
*template-
|
| 683 |
-
“pointer to int.”
|
| 684 |
- For a non-type *template-parameter* of type reference to object, no
|
| 685 |
conversions apply. The type referred to by the reference may be more
|
| 686 |
cv-qualified than the (otherwise identical) type of the
|
| 687 |
*template-argument*. The *template-parameter* is bound directly to the
|
| 688 |
*template-argument*, which shall be an lvalue.
|
|
@@ -738,15 +763,15 @@ only primary class templates are considered when matching the template
|
|
| 738 |
template argument with the corresponding parameter; partial
|
| 739 |
specializations are not considered even if their parameter lists match
|
| 740 |
that of the template template parameter.
|
| 741 |
|
| 742 |
Any partial specializations ([[temp.class.spec]]) associated with the
|
| 743 |
-
primary class template
|
| 744 |
-
template *template-parameter* is
|
| 745 |
-
not visible at the point of
|
| 746 |
-
selected had it been visible, the
|
| 747 |
-
is required.
|
| 748 |
|
| 749 |
``` cpp
|
| 750 |
template<class T> class A { // primary template
|
| 751 |
int x;
|
| 752 |
};
|
|
@@ -761,10 +786,26 @@ C<A> c; // V<int> within C<A> uses the primary template,
|
|
| 761 |
// so c.y.x has type int
|
| 762 |
// V<int*> within C<A> uses the partial specialization,
|
| 763 |
// so c.z.x has type long
|
| 764 |
```
|
| 765 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
``` cpp
|
| 767 |
template<class T> class A { /* ... */ };
|
| 768 |
template<class T, class U = T> class B { /* ... */ };
|
| 769 |
template <class ... Types> class C { /* ... */ };
|
| 770 |
|
|
@@ -778,22 +819,10 @@ X<C> xc; // ill-formed: a template parameter pack does not match a te
|
|
| 778 |
Y<A> ya; // OK
|
| 779 |
Y<B> yb; // OK
|
| 780 |
Y<C> yc; // OK
|
| 781 |
```
|
| 782 |
|
| 783 |
-
A *template-argument* matches a template *template-parameter* (call it
|
| 784 |
-
`P`) when each of the template parameters in the
|
| 785 |
-
*template-parameter-list* of the *template-argument*’s corresponding
|
| 786 |
-
class template or alias template (call it `A`) matches the corresponding
|
| 787 |
-
template parameter in the *template-parameter-list* of `P`. When `P`’s
|
| 788 |
-
*template-parameter-list* contains a template parameter pack (
|
| 789 |
-
[[temp.variadic]]), the template parameter pack will match zero or more
|
| 790 |
-
template parameters or template parameter packs in the
|
| 791 |
-
*template-parameter-list* of `A` with the same type and form as the
|
| 792 |
-
template parameter pack in `P` (ignoring whether those template
|
| 793 |
-
parameters are template parameter packs)
|
| 794 |
-
|
| 795 |
``` cpp
|
| 796 |
template <class T> struct eval;
|
| 797 |
|
| 798 |
template <template <class, class...> class TT, class T1, class... Rest>
|
| 799 |
struct eval<TT<T1, Rest...>> { };
|
|
@@ -811,11 +840,11 @@ eval<D<int, 17>> eD; // error: D does not match TT in partial special
|
|
| 811 |
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
|
| 812 |
```
|
| 813 |
|
| 814 |
## Type equivalence <a id="temp.type">[[temp.type]]</a>
|
| 815 |
|
| 816 |
-
Two *template-id*s refer to the same class or
|
| 817 |
|
| 818 |
- their *template-name*s, *operator-function-id*s, or
|
| 819 |
*literal-operator-id*s refer to the same template and
|
| 820 |
- their corresponding type *template-argument*s are the same type and
|
| 821 |
- their corresponding non-type template arguments of integral or
|
|
@@ -878,15 +907,17 @@ template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
|
|
| 878 |
```
|
| 879 |
|
| 880 |
However, this syntax is allowed in class template partial
|
| 881 |
specializations ([[temp.class.spec]]).
|
| 882 |
|
| 883 |
-
For purposes of name lookup and instantiation, default arguments
|
| 884 |
-
function templates and default arguments
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
|
|
|
|
|
|
| 888 |
|
| 889 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 890 |
possible to partially or explicitly specialize an alias template.
|
| 891 |
|
| 892 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
|
@@ -990,12 +1021,12 @@ v1[3] = 7; // Array<int>::operator[]()
|
|
| 990 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 991 |
```
|
| 992 |
|
| 993 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 994 |
|
| 995 |
-
A
|
| 996 |
-
template definition in which it is declared. The
|
| 997 |
defined before its first use that requires an instantiation (
|
| 998 |
[[temp.inst]]). For example,
|
| 999 |
|
| 1000 |
``` cpp
|
| 1001 |
template<class T> struct A {
|
|
@@ -1006,18 +1037,27 @@ template<class T> class A<T>::B { };
|
|
| 1006 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 1007 |
```
|
| 1008 |
|
| 1009 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 1010 |
|
| 1011 |
-
A definition for a static data member
|
| 1012 |
-
scope enclosing the definition of the static
|
|
|
|
| 1013 |
|
| 1014 |
``` cpp
|
| 1015 |
template<class T> class X {
|
| 1016 |
static T s;
|
| 1017 |
};
|
| 1018 |
template<class T> T X<T>::s = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1019 |
```
|
| 1020 |
|
| 1021 |
An explicit specialization of a static data member declared as an array
|
| 1022 |
of unknown bound can have a different bound from its definition, if any.
|
| 1023 |
|
|
@@ -1061,27 +1101,27 @@ template<class T> struct string {
|
|
| 1061 |
|
| 1062 |
template<class T> template<class T2> int string<T>::compare(const T2& s) {
|
| 1063 |
}
|
| 1064 |
```
|
| 1065 |
|
| 1066 |
-
A local class shall not have member templates.
|
| 1067 |
-
(Clause [[class.access]]) apply to member template
|
| 1068 |
-
shall not be a member template. A
|
| 1069 |
-
with a given name and type and a member
|
| 1070 |
-
name, which could be used to generate a
|
| 1071 |
-
can both be declared in a class. When
|
| 1072 |
-
type refers to the non-template
|
| 1073 |
-
argument list is supplied.
|
| 1074 |
|
| 1075 |
``` cpp
|
| 1076 |
template <class T> struct A {
|
| 1077 |
void f(int);
|
| 1078 |
template <class T2> void f(T2);
|
| 1079 |
};
|
| 1080 |
|
| 1081 |
-
template <> void A<int>::f(int) { } // non-template member
|
| 1082 |
-
template <> template <> void A<int>::f<>(int) { } // template
|
| 1083 |
|
| 1084 |
int main() {
|
| 1085 |
A<char> ac;
|
| 1086 |
ac.f(1); // non-template
|
| 1087 |
ac.f('c'); // template
|
|
@@ -1198,12 +1238,13 @@ the following contexts:
|
|
| 1198 |
*type-parameter* without the ellipsis.
|
| 1199 |
- In an *initializer-list* ([[dcl.init]]); the pattern is an
|
| 1200 |
*initializer-clause*.
|
| 1201 |
- In a *base-specifier-list* (Clause [[class.derived]]); the pattern is
|
| 1202 |
a *base-specifier*.
|
| 1203 |
-
- In a *mem-initializer-list* ([[class.base.init]])
|
| 1204 |
-
*mem-initializer*
|
|
|
|
| 1205 |
- In a *template-argument-list* ([[temp.arg]]); the pattern is a
|
| 1206 |
*template-argument*.
|
| 1207 |
- In a *dynamic-exception-specification* ([[except.spec]]); the pattern
|
| 1208 |
is a *type-id*.
|
| 1209 |
- In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
|
|
@@ -1213,10 +1254,15 @@ the following contexts:
|
|
| 1213 |
- In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
|
| 1214 |
*capture*.
|
| 1215 |
- In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
|
| 1216 |
*identifier*.
|
| 1217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1218 |
``` cpp
|
| 1219 |
template<class ... Types> void f(Types ... rest);
|
| 1220 |
template<class ... Types> void g(Types ... rest) {
|
| 1221 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 1222 |
}
|
|
@@ -1260,18 +1306,28 @@ template<class ... Args>
|
|
| 1260 |
The instantiation of a pack expansion that is not a `sizeof...`
|
| 1261 |
expression produces a list
|
| 1262 |
$\mathtt{E}_1, \mathtt{E}_2, ..., \mathtt{E}_N$, where N is the number
|
| 1263 |
of elements in the pack expansion parameters. Each Eᵢ is generated by
|
| 1264 |
instantiating the pattern and replacing each pack expansion parameter
|
| 1265 |
-
with its ith element.
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
the
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
|
| 1272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1273 |
|
| 1274 |
``` cpp
|
| 1275 |
template<class... T> struct X : T... { };
|
| 1276 |
template<class... T> void f(T... values) {
|
| 1277 |
X<T...> x(values...);
|
|
@@ -1287,25 +1343,25 @@ parameter pack it expands.
|
|
| 1287 |
|
| 1288 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 1289 |
|
| 1290 |
A friend of a class or class template can be a function template or
|
| 1291 |
class template, a specialization of a function template or class
|
| 1292 |
-
template, or
|
| 1293 |
-
|
| 1294 |
|
| 1295 |
- if the name of the friend is a qualified or unqualified *template-id*,
|
| 1296 |
the friend declaration refers to a specialization of a function
|
| 1297 |
-
template, otherwise
|
| 1298 |
- if the name of the friend is a *qualified-id* and a matching
|
| 1299 |
non-template function is found in the specified class or namespace,
|
| 1300 |
the friend declaration refers to that function, otherwise,
|
| 1301 |
- if the name of the friend is a *qualified-id* and a matching function
|
| 1302 |
template is found in the specified class or namespace, the friend
|
| 1303 |
declaration refers to the deduced specialization of that function
|
| 1304 |
template ([[temp.deduct.decl]]), otherwise,
|
| 1305 |
-
- the name shall be an *unqualified-id* that declares (or redeclares)
|
| 1306 |
-
|
| 1307 |
|
| 1308 |
``` cpp
|
| 1309 |
template<class T> class task;
|
| 1310 |
template<class T> task<T>* preempt(task<T>*);
|
| 1311 |
|
|
@@ -1323,11 +1379,11 @@ template<class T> class task {
|
|
| 1323 |
Here, each specialization of the `task` class template has the function
|
| 1324 |
`next_time` as a friend; because `process` does not have explicit
|
| 1325 |
*template-argument*s, each specialization of the `task` class template
|
| 1326 |
has an appropriately typed function `process` as a friend, and this
|
| 1327 |
friend is not a function template specialization; because the friend
|
| 1328 |
-
`preempt` has an explicit *template-argument* `
|
| 1329 |
of the `task` class template has the appropriate specialization of the
|
| 1330 |
function template `preempt` as a friend; and each specialization of the
|
| 1331 |
`task` class template has all specializations of the function template
|
| 1332 |
`func` as friends. Similarly, each specialization of the `task` class
|
| 1333 |
template has the class template specialization `task<int>` as a friend,
|
|
@@ -1362,14 +1418,14 @@ class X {
|
|
| 1362 |
template<class T> struct A { X::Y ab; }; // OK
|
| 1363 |
template<class T> struct A<T*> { X::Y ab; }; // OK
|
| 1364 |
```
|
| 1365 |
|
| 1366 |
When a function is defined in a friend function declaration in a class
|
| 1367 |
-
template, the function is instantiated when the function is odr-used
|
| 1368 |
-
The same restrictions on multiple declarations and
|
| 1369 |
-
apply to non-template function declarations and
|
| 1370 |
-
to these implicit definitions.
|
| 1371 |
|
| 1372 |
A member of a class template may be declared to be a friend of a
|
| 1373 |
non-template class. In this case, the corresponding member of every
|
| 1374 |
specialization of the class template is a friend of the class granting
|
| 1375 |
friendship. For explicit specializations the corresponding member is the
|
|
@@ -1537,10 +1593,12 @@ following restrictions apply:
|
|
| 1537 |
int array[5];
|
| 1538 |
template< int X > class A<X,&array> { }; // error
|
| 1539 |
```
|
| 1540 |
- The argument list of the specialization shall not be identical to the
|
| 1541 |
implicit argument list of the primary template.
|
|
|
|
|
|
|
| 1542 |
- The template parameter list of a specialization shall not contain
|
| 1543 |
default template argument values.[^4]
|
| 1544 |
- An argument shall not contain an unexpanded parameter pack. If an
|
| 1545 |
argument is a pack expansion ([[temp.variadic]]), it shall be the
|
| 1546 |
last argument in the template argument list.
|
|
@@ -1705,12 +1763,12 @@ family of sort functions might be declared like this:
|
|
| 1705 |
template<class T> class Array { };
|
| 1706 |
template<class T> void sort(Array<T>&);
|
| 1707 |
```
|
| 1708 |
|
| 1709 |
A function template can be overloaded with other function templates and
|
| 1710 |
-
with
|
| 1711 |
-
to a function template (i.e., it is never considered to be a
|
| 1712 |
specialization), even if it has the same name and type as a potentially
|
| 1713 |
generated function template specialization.[^5]
|
| 1714 |
|
| 1715 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1716 |
|
|
@@ -1775,15 +1833,26 @@ Two expressions involving template parameters are considered
|
|
| 1775 |
*equivalent* if two function definitions containing the expressions
|
| 1776 |
would satisfy the one definition rule ([[basic.def.odr]]), except that
|
| 1777 |
the tokens used to name the template parameters may differ as long as a
|
| 1778 |
token used to name a template parameter in one expression is replaced by
|
| 1779 |
another token that names the same template parameter in the other
|
| 1780 |
-
expression.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1781 |
|
| 1782 |
``` cpp
|
| 1783 |
template <int I, int J> void f(A<I+J>); // #1
|
| 1784 |
template <int K, int L> void f(A<K+L>); // same as #1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1785 |
```
|
| 1786 |
|
| 1787 |
Two expressions involving template parameters that are not equivalent
|
| 1788 |
are *functionally equivalent* if, for any given set of template
|
| 1789 |
arguments, the evaluation of the expression results in the same value.
|
|
@@ -1850,18 +1919,19 @@ specialized template is the one chosen by the partial ordering process.
|
|
| 1850 |
To produce the transformed template, for each type, non-type, or
|
| 1851 |
template template parameter (including template parameter packs (
|
| 1852 |
[[temp.variadic]]) thereof) synthesize a unique type, value, or class
|
| 1853 |
template respectively and substitute it for each occurrence of that
|
| 1854 |
parameter in the function type of the template. If only one of the
|
| 1855 |
-
function templates is a non-static member
|
| 1856 |
-
considered to have a new first parameter inserted
|
| 1857 |
-
|
| 1858 |
-
|
| 1859 |
-
|
| 1860 |
-
|
| 1861 |
-
|
| 1862 |
-
|
|
|
|
| 1863 |
|
| 1864 |
``` cpp
|
| 1865 |
struct A { };
|
| 1866 |
template<class T> struct B {
|
| 1867 |
template<class R> int operator*(R&); // #1
|
|
@@ -2132,18 +2202,17 @@ template<class T> struct A {
|
|
| 2132 |
B b; // OK, no typename required
|
| 2133 |
};
|
| 2134 |
```
|
| 2135 |
|
| 2136 |
Knowing which names are type names allows the syntax of every template
|
| 2137 |
-
|
| 2138 |
-
|
| 2139 |
-
|
| 2140 |
-
|
| 2141 |
-
|
| 2142 |
-
|
| 2143 |
-
|
| 2144 |
-
used in a non-dependent name is incomplete at the point at which a
|
| 2145 |
template is defined but is complete at the point at which an
|
| 2146 |
instantiation is done, and if the completeness of that type affects
|
| 2147 |
whether or not the program is well-formed or affects the semantics of
|
| 2148 |
the program, the program is ill-formed; no diagnostic is required. If a
|
| 2149 |
template is instantiated, errors will be diagnosed according to the
|
|
@@ -2234,13 +2303,14 @@ void h() {
|
|
| 2234 |
// by two calls of f(E)
|
| 2235 |
g('a'); // will cause three calls of f(char)
|
| 2236 |
}
|
| 2237 |
```
|
| 2238 |
|
| 2239 |
-
For purposes of name lookup, default arguments
|
| 2240 |
-
|
| 2241 |
-
|
|
|
|
| 2242 |
|
| 2243 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 2244 |
|
| 2245 |
Like normal (non-template) classes, class templates have an
|
| 2246 |
injected-class-name (Clause [[class]]). The injected-class-name can be
|
|
@@ -2402,19 +2472,19 @@ and/or value of template parameters (as determined by the template
|
|
| 2402 |
arguments) and this determines the context for name lookup for certain
|
| 2403 |
names. Expressions may be *type-dependent* (on the type of a template
|
| 2404 |
parameter) or *value-dependent* (on the value of a non-type template
|
| 2405 |
parameter). In an expression of the form:
|
| 2406 |
|
| 2407 |
-
where the *postfix-expression* is an *
|
| 2408 |
-
*
|
| 2409 |
|
| 2410 |
- any of the expressions in the *expression-list* is a pack expansion (
|
| 2411 |
[[temp.variadic]]),
|
| 2412 |
- any of the expressions in the *expression-list* is a type-dependent
|
| 2413 |
expression ([[temp.dep.expr]]), or
|
| 2414 |
-
- if the *unqualified-id*
|
| 2415 |
-
|
| 2416 |
|
| 2417 |
If an operand of an operator is a type-dependent expression, the
|
| 2418 |
operator also denotes a dependent name. Such names are unbound and are
|
| 2419 |
looked up at the point of the template instantiation ([[temp.point]])
|
| 2420 |
in both the context of the template definition and the context of the
|
|
@@ -2546,27 +2616,27 @@ template <class T1, class T2, int I> struct B {
|
|
| 2546 |
```
|
| 2547 |
|
| 2548 |
A name is a *member of the current instantiation* if it is
|
| 2549 |
|
| 2550 |
- An unqualified name that, when looked up, refers to at least one
|
| 2551 |
-
member of the current instantiation or a non-dependent
|
| 2552 |
-
thereof. This can only occur when looking up a name in a
|
| 2553 |
-
enclosed by the definition of a class template.
|
| 2554 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2555 |
current instantiation and that, when looked up, refers to at least one
|
| 2556 |
-
member of the current instantiation or a non-dependent
|
| 2557 |
-
thereof. if no such member is found, and the current
|
| 2558 |
-
any dependent base classes, then the *qualified-id*
|
| 2559 |
-
unknown specialization; see below.
|
| 2560 |
- An *id-expression* denoting the member in a class member access
|
| 2561 |
expression ([[expr.ref]]) for which the type of the object expression
|
| 2562 |
is the current instantiation, and the *id-expression*, when looked
|
| 2563 |
-
up ([[basic.lookup.classref]]), refers to at least one member of
|
| 2564 |
-
current instantiation or a non-dependent base class
|
| 2565 |
-
such member is found, and the current instantiation has
|
| 2566 |
-
base classes, then the *id-expression* is a member of an
|
| 2567 |
-
specialization; see below.
|
| 2568 |
|
| 2569 |
``` cpp
|
| 2570 |
template <class T> class A {
|
| 2571 |
static const int i = 5;
|
| 2572 |
int n1[i]; // i refers to a member of the current instantiation
|
|
@@ -2578,25 +2648,30 @@ template <class T> class A {
|
|
| 2578 |
template <class T> int A<T>::f() {
|
| 2579 |
return i; // i refers to a member of the current instantiation
|
| 2580 |
}
|
| 2581 |
```
|
| 2582 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2583 |
A name is a *member of an unknown specialization* if it is
|
| 2584 |
|
| 2585 |
- A *qualified-id* in which the *nested-name-specifier* names a
|
| 2586 |
dependent type that is not the current instantiation.
|
| 2587 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2588 |
current instantiation, the current instantiation has at least one
|
| 2589 |
dependent base class, and name lookup of the *qualified-id* does not
|
| 2590 |
-
find any member of the current instantiation or a
|
| 2591 |
-
class thereof.
|
| 2592 |
- An *id-expression* denoting the member in a class member access
|
| 2593 |
expression ([[expr.ref]]) in which either
|
| 2594 |
- the type of the object expression is the current instantiation, the
|
| 2595 |
current instantiation has at least one dependent base class, and
|
| 2596 |
-
name lookup of the *id-expression* does not find a member of
|
| 2597 |
-
current instantiation or a non-dependent base class
|
|
|
|
| 2598 |
- the type of the object expression is dependent and is not the
|
| 2599 |
current instantiation.
|
| 2600 |
|
| 2601 |
If a *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2602 |
current instantiation is not a member of the current instantiation or a
|
|
@@ -2634,12 +2709,12 @@ base class of the current instantiation.
|
|
| 2634 |
|
| 2635 |
A type is dependent if it is
|
| 2636 |
|
| 2637 |
- a template parameter,
|
| 2638 |
- a member of an unknown specialization,
|
| 2639 |
-
- a nested class or enumeration that is a member of the
|
| 2640 |
-
instantiation,
|
| 2641 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 2642 |
- a compound type constructed from any dependent type,
|
| 2643 |
- an array type constructed from any dependent type or whose size is
|
| 2644 |
specified by a constant expression that is value-dependent,
|
| 2645 |
- a *simple-template-id* in which either the template name is a template
|
|
@@ -2665,20 +2740,25 @@ dependent ([[temp.dep.type]]).
|
|
| 2665 |
|
| 2666 |
An *id-expression* is type-dependent if it contains
|
| 2667 |
|
| 2668 |
- an *identifier* associated by name lookup with one or more
|
| 2669 |
declarations declared with a dependent type,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2670 |
- a *template-id* that is dependent,
|
| 2671 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 2672 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 2673 |
an unknown specialization;
|
| 2674 |
|
| 2675 |
-
or if it names a
|
| 2676 |
-
|
| 2677 |
-
[[temp.static]]). Expressions of the following forms are
|
| 2678 |
-
only if the type specified by the *type-id*,
|
| 2679 |
-
*new-type-id* is dependent, even if any
|
|
|
|
| 2680 |
|
| 2681 |
Expressions of the following forms are never type-dependent (because the
|
| 2682 |
type of the expression cannot be dependent):
|
| 2683 |
|
| 2684 |
For the standard library macro `offsetof`, see [[support.types]].
|
|
@@ -2698,16 +2778,21 @@ member access expression.
|
|
| 2698 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 2699 |
|
| 2700 |
Except as described below, a constant expression is value-dependent if
|
| 2701 |
any subexpression is value-dependent.
|
| 2702 |
|
| 2703 |
-
An *
|
| 2704 |
|
| 2705 |
-
- a name declared with a dependent type,
|
| 2706 |
-
- the name of a non-type template parameter,
|
| 2707 |
-
-
|
| 2708 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2709 |
|
| 2710 |
Expressions of the following form are value-dependent if the
|
| 2711 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 2712 |
dependent:
|
| 2713 |
|
|
@@ -2717,12 +2802,13 @@ Expressions of the following form are value-dependent if either the
|
|
| 2717 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 2718 |
*cast-expression* is value-dependent:
|
| 2719 |
|
| 2720 |
Expressions of the following form are value-dependent:
|
| 2721 |
|
| 2722 |
-
An
|
| 2723 |
-
|
|
|
|
| 2724 |
|
| 2725 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 2726 |
|
| 2727 |
A type *template-argument* is dependent if the type it specifies is
|
| 2728 |
dependent.
|
|
@@ -2790,10 +2876,21 @@ If a function template or member function of a class template is called
|
|
| 2790 |
in a way which uses the definition of a default argument of that
|
| 2791 |
function template or member function, the point of instantiation of the
|
| 2792 |
default argument is the point of instantiation of the function template
|
| 2793 |
or member function specialization.
|
| 2794 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2795 |
For a class template specialization, a class member template
|
| 2796 |
specialization, or a specialization for a class member of a class
|
| 2797 |
template, if the specialization is implicitly instantiated because it is
|
| 2798 |
referenced from within another template specialization, if the context
|
| 2799 |
from which the specialization is referenced depends on a template
|
|
@@ -2830,31 +2927,28 @@ units. If two different points of instantiation give a template
|
|
| 2830 |
specialization different meanings according to the one definition rule (
|
| 2831 |
[[basic.def.odr]]), the program is ill-formed, no diagnostic required.
|
| 2832 |
|
| 2833 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 2834 |
|
| 2835 |
-
For a function call
|
| 2836 |
-
functions are found using the usual lookup rules (
|
| 2837 |
-
[[basic.lookup.unqual]], [[basic.lookup.argdep]]
|
| 2838 |
-
[[basic.lookup.qual]]) except that:
|
| 2839 |
|
| 2840 |
- For the part of the lookup using unqualified name lookup (
|
| 2841 |
-
[[basic.lookup.unqual]])
|
| 2842 |
-
[[basic.lookup.qual]]), only function declarations from the template
|
| 2843 |
definition context are found.
|
| 2844 |
- For the part of the lookup using associated namespaces (
|
| 2845 |
[[basic.lookup.argdep]]), only function declarations found in either
|
| 2846 |
the template definition context or the template instantiation context
|
| 2847 |
are found.
|
| 2848 |
|
| 2849 |
-
If the
|
| 2850 |
-
|
| 2851 |
-
|
| 2852 |
-
|
| 2853 |
-
|
| 2854 |
-
|
| 2855 |
-
undefined behavior.
|
| 2856 |
|
| 2857 |
### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
|
| 2858 |
|
| 2859 |
Friend classes or functions can be declared within a class template.
|
| 2860 |
When a template is instantiated, the names of its friends are treated as
|
|
@@ -2970,18 +3064,19 @@ instantiated ([[temp.explicit]]) or explicitly specialized (
|
|
| 2970 |
[[temp.expl.spec]]), the class template specialization is implicitly
|
| 2971 |
instantiated when the specialization is referenced in a context that
|
| 2972 |
requires a completely-defined object type or when the completeness of
|
| 2973 |
the class type affects the semantics of the program. The implicit
|
| 2974 |
instantiation of a class template specialization causes the implicit
|
| 2975 |
-
instantiation of the declarations, but not of the definitions
|
| 2976 |
-
arguments, of the class member functions,
|
| 2977 |
-
enumerations, static data members and
|
| 2978 |
-
|
| 2979 |
-
enumerations and member anonymous unions.
|
| 2980 |
-
|
| 2981 |
-
according to [[class.mem]], a
|
| 2982 |
-
|
|
|
|
| 2983 |
|
| 2984 |
``` cpp
|
| 2985 |
template<class T, class U>
|
| 2986 |
struct Outer {
|
| 2987 |
template<class X, class Y> struct Inner;
|
|
@@ -3036,10 +3131,17 @@ void h() {
|
|
| 3036 |
```
|
| 3037 |
|
| 3038 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 3039 |
`Z<char>::f()` to be implicitly instantiated.
|
| 3040 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3041 |
A class template specialization is implicitly instantiated if the class
|
| 3042 |
type is used in a context that requires a completely-defined object type
|
| 3043 |
or if the completeness of the class type might affect the semantics of
|
| 3044 |
the program. In particular, if the semantics of an expression depend on
|
| 3045 |
the member or base class lists of a class template specialization, the
|
|
@@ -3099,28 +3201,28 @@ data members of that class to be implicitly instantiated.
|
|
| 3099 |
If a function template or a member function template specialization is
|
| 3100 |
used in a way that involves overload resolution, a declaration of the
|
| 3101 |
specialization is implicitly instantiated ([[temp.over]]).
|
| 3102 |
|
| 3103 |
An implementation shall not implicitly instantiate a function template,
|
| 3104 |
-
a member template, a non-virtual member function, a
|
| 3105 |
-
static data member of a class template that does not
|
| 3106 |
-
instantiation. It is unspecified whether or not an
|
| 3107 |
-
implicitly instantiates a virtual member function of a
|
| 3108 |
-
the virtual member function would not otherwise be
|
| 3109 |
-
of a template specialization in a default argument
|
| 3110 |
-
template to be implicitly instantiated except that a
|
| 3111 |
-
be instantiated where its complete type is needed to
|
| 3112 |
-
correctness of the default argument. The use of a default
|
| 3113 |
-
function call causes specializations in the default
|
| 3114 |
-
implicitly instantiated.
|
| 3115 |
|
| 3116 |
-
Implicitly instantiated class
|
| 3117 |
-
placed in the namespace where the template is
|
| 3118 |
-
instantiated specializations for members of a class
|
| 3119 |
-
in the namespace where the enclosing class template
|
| 3120 |
-
Implicitly instantiated member templates are placed in the
|
| 3121 |
-
where the enclosing class or class template is defined.
|
| 3122 |
|
| 3123 |
``` cpp
|
| 3124 |
namespace N {
|
| 3125 |
template<class T> class List {
|
| 3126 |
public:
|
|
@@ -3147,13 +3249,16 @@ If a function template `f` is called in a way that requires a default
|
|
| 3147 |
argument to be used, the dependent names are looked up, the semantics
|
| 3148 |
constraints are checked, and the instantiation of any template used in
|
| 3149 |
the default argument is done as if the default argument had been an
|
| 3150 |
initializer used in a function template specialization with the same
|
| 3151 |
scope, the same template parameters and the same access as that of the
|
| 3152 |
-
function template `f` used at that point
|
| 3153 |
-
|
| 3154 |
-
|
|
|
|
|
|
|
|
|
|
| 3155 |
|
| 3156 |
Each default argument is instantiated independently.
|
| 3157 |
|
| 3158 |
``` cpp
|
| 3159 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
|
@@ -3167,10 +3272,19 @@ void g(A a, A b, A c) {
|
|
| 3167 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 3168 |
f(a); // ill-formed; ydef is not declared
|
| 3169 |
}
|
| 3170 |
```
|
| 3171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3172 |
[[temp.point]] defines the point of instantiation of a template
|
| 3173 |
specialization.
|
| 3174 |
|
| 3175 |
There is an implementation-defined quantity that specifies the limit on
|
| 3176 |
the total depth of recursive instantiations, which could involve more
|
|
@@ -3186,16 +3300,17 @@ template<class T> class X {
|
|
| 3186 |
};
|
| 3187 |
```
|
| 3188 |
|
| 3189 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 3190 |
|
| 3191 |
-
A class,
|
| 3192 |
-
instantiated from its template. A member function, member
|
| 3193 |
-
static data member of a class template can be explicitly
|
| 3194 |
-
from the member definition associated with its class
|
| 3195 |
-
explicit instantiation of a function template or member
|
| 3196 |
-
class template shall not use the `inline` or `constexpr`
|
|
|
|
| 3197 |
|
| 3198 |
The syntax for explicit instantiation is:
|
| 3199 |
|
| 3200 |
``` bnf
|
| 3201 |
explicit-instantiation:
|
|
@@ -3215,17 +3330,18 @@ a *template-name* or *operator-function-id*. The declaration may declare
|
|
| 3215 |
a *qualified-id*, in which case the *unqualified-id* of the
|
| 3216 |
*qualified-id* must be a *template-id*. If the explicit instantiation is
|
| 3217 |
for a member function, a member class or a static data member of a class
|
| 3218 |
template specialization, the name of the class template specialization
|
| 3219 |
in the *qualified-id* for the member name shall be a
|
| 3220 |
-
*simple-template-id*.
|
| 3221 |
-
|
| 3222 |
-
explicit instantiation
|
| 3223 |
-
|
| 3224 |
-
|
| 3225 |
-
namespace
|
| 3226 |
-
|
|
|
|
| 3227 |
|
| 3228 |
``` cpp
|
| 3229 |
template<class T> class Array { void mf(); };
|
| 3230 |
template class Array<char>;
|
| 3231 |
template void Array<int>::mf();
|
|
@@ -3237,35 +3353,36 @@ namespace N {
|
|
| 3237 |
template<class T> void f(T&) { }
|
| 3238 |
}
|
| 3239 |
template void N::f<int>(int&);
|
| 3240 |
```
|
| 3241 |
|
| 3242 |
-
A declaration of a function template, a
|
| 3243 |
-
member of a class template, or a member function
|
| 3244 |
-
class template shall precede an explicit
|
| 3245 |
-
|
| 3246 |
-
|
| 3247 |
-
explicit instantiation of that entity
|
| 3248 |
-
|
| 3249 |
-
|
| 3250 |
-
names an implicitly-declared
|
| 3251 |
-
[[special]]), the program is
|
|
|
|
| 3252 |
|
| 3253 |
For a given set of template arguments, if an explicit instantiation of a
|
| 3254 |
template appears after a declaration of an explicit specialization for
|
| 3255 |
that template, the explicit instantiation has no effect. Otherwise, for
|
| 3256 |
an explicit instantiation definition the definition of a function
|
| 3257 |
-
template, a
|
| 3258 |
-
data member of a class template shall be present in
|
| 3259 |
-
unit in which it is explicitly instantiated.
|
| 3260 |
|
| 3261 |
-
An explicit instantiation of a class
|
| 3262 |
-
is placed in the namespace in which the template
|
| 3263 |
-
instantiation for a member of a class template
|
| 3264 |
-
namespace where the enclosing class template is
|
| 3265 |
-
instantiation for a member template is placed in
|
| 3266 |
-
enclosing class or class template is defined.
|
| 3267 |
|
| 3268 |
``` cpp
|
| 3269 |
namespace N {
|
| 3270 |
template<class T> class Y { void mf() { } };
|
| 3271 |
}
|
|
@@ -3296,28 +3413,31 @@ template void sort<>(Array<int>&);
|
|
| 3296 |
```
|
| 3297 |
|
| 3298 |
An explicit instantiation that names a class template specialization is
|
| 3299 |
also an explicit instantiation of the same kind (declaration or
|
| 3300 |
definition) of each of its members (not including members inherited from
|
| 3301 |
-
base classes
|
| 3302 |
-
|
| 3303 |
-
described below. In addition, it will
|
| 3304 |
-
|
|
|
|
| 3305 |
|
| 3306 |
An explicit instantiation definition that names a class template
|
| 3307 |
specialization explicitly instantiates the class template specialization
|
| 3308 |
and is an explicit instantiation definition of only those members that
|
| 3309 |
have been defined at the point of instantiation.
|
| 3310 |
|
| 3311 |
-
Except for inline functions
|
| 3312 |
-
|
| 3313 |
-
|
| 3314 |
-
|
| 3315 |
-
|
| 3316 |
-
|
| 3317 |
-
|
| 3318 |
-
the
|
|
|
|
|
|
|
| 3319 |
|
| 3320 |
If an entity is the subject of both an explicit instantiation
|
| 3321 |
declaration and an explicit instantiation definition in the same
|
| 3322 |
translation unit, the definition shall follow the declaration. An entity
|
| 3323 |
that is the subject of an explicit instantiation declaration and that is
|
|
@@ -3353,10 +3473,11 @@ template int g<int>(int); // OK even though &p isn't an int.
|
|
| 3353 |
|
| 3354 |
An explicit specialization of any of the following:
|
| 3355 |
|
| 3356 |
- function template
|
| 3357 |
- class template
|
|
|
|
| 3358 |
- member function of a class template
|
| 3359 |
- static data member of a class template
|
| 3360 |
- member class of a class template
|
| 3361 |
- member enumeration of a class template
|
| 3362 |
- member class template of a class or class template
|
|
@@ -3394,33 +3515,34 @@ enclosing namespace of the template, or, if the namespace is inline (
|
|
| 3394 |
[[namespace.def]]), any namespace from its enclosing namespace set. Such
|
| 3395 |
a declaration may also be a definition. If the declaration is not a
|
| 3396 |
definition, the specialization may be defined later (
|
| 3397 |
[[namespace.memdef]]).
|
| 3398 |
|
| 3399 |
-
A declaration of a function template
|
| 3400 |
-
specialized shall precede the declaration of
|
| 3401 |
-
specialization. A declaration, but not a definition of the
|
| 3402 |
-
required. The definition of a class or class template shall
|
| 3403 |
-
declaration of an explicit specialization for a member
|
| 3404 |
-
class or class template.
|
| 3405 |
|
| 3406 |
``` cpp
|
| 3407 |
template<> class X<int> { /* ... */ }; // error: X not a template
|
| 3408 |
|
| 3409 |
template<class T> class X;
|
| 3410 |
|
| 3411 |
template<> class X<char*> { /* ... */ }; // OK: X is a template
|
| 3412 |
```
|
| 3413 |
|
| 3414 |
A member function, a member function template, a member class, a member
|
| 3415 |
-
enumeration, a member class template,
|
| 3416 |
-
|
| 3417 |
-
is implicitly instantiated; in this
|
| 3418 |
-
|
| 3419 |
-
class template. If such an explicit
|
| 3420 |
-
|
| 3421 |
-
(Clause [[special]]), the
|
|
|
|
| 3422 |
|
| 3423 |
A member of an explicitly specialized class is not implicitly
|
| 3424 |
instantiated from the member declaration of the class template; instead,
|
| 3425 |
the member of the class template specialization shall itself be
|
| 3426 |
explicitly defined if its definition is required. In this case, the
|
|
@@ -3512,31 +3634,33 @@ template<class T> struct A {
|
|
| 3512 |
};
|
| 3513 |
template<> enum A<int>::E : int { eint }; // OK
|
| 3514 |
template<> enum class A<int>::S : int { sint }; // OK
|
| 3515 |
template<class T> enum A<T>::E : T { eT };
|
| 3516 |
template<class T> enum class A<T>::S : T { sT };
|
| 3517 |
-
template<> enum A<char>::E :
|
| 3518 |
// when A<char> was instantiated
|
| 3519 |
-
template<> enum class A<char>::S :
|
| 3520 |
```
|
| 3521 |
|
| 3522 |
The placement of explicit specialization declarations for function
|
| 3523 |
-
templates, class templates, member functions of
|
| 3524 |
-
data members of class templates, member classes
|
| 3525 |
-
|
| 3526 |
-
templates, member function templates of class
|
|
|
|
| 3527 |
functions of member templates of class templates, member functions of
|
| 3528 |
-
member templates of non-template classes,
|
| 3529 |
-
|
| 3530 |
-
|
| 3531 |
-
of
|
| 3532 |
-
|
| 3533 |
-
|
| 3534 |
-
|
| 3535 |
-
|
| 3536 |
-
|
| 3537 |
-
|
|
|
|
| 3538 |
|
| 3539 |
A template explicit specialization is in the scope of the namespace in
|
| 3540 |
which the template was defined.
|
| 3541 |
|
| 3542 |
``` cpp
|
|
@@ -3549,11 +3673,13 @@ namespace N {
|
|
| 3549 |
template<> class Y<double>; // forward declare intent to
|
| 3550 |
// specialize for double
|
| 3551 |
}
|
| 3552 |
|
| 3553 |
template<> class N::Y<double> { /* ... */ }; // OK: specialization
|
| 3554 |
-
// in
|
|
|
|
|
|
|
| 3555 |
```
|
| 3556 |
|
| 3557 |
A *simple-template-id* that names a class template explicit
|
| 3558 |
specialization that has been declared but not defined can be used
|
| 3559 |
exactly like the names of other incompletely-defined classes (
|
|
@@ -3594,13 +3720,14 @@ template<class T> inline T g(T) { /* ... */ }
|
|
| 3594 |
|
| 3595 |
template<> inline void f<>(int) { /* ... */ } // OK: inline
|
| 3596 |
template<> int g<>(int) { /* ... */ } // OK: not inline
|
| 3597 |
```
|
| 3598 |
|
| 3599 |
-
An explicit specialization of a static data member of a template
|
| 3600 |
-
|
| 3601 |
-
|
|
|
|
| 3602 |
requires default initialization must use a *braced-init-list*:
|
| 3603 |
|
| 3604 |
``` cpp
|
| 3605 |
template<> X Q<int>::x; // declaration
|
| 3606 |
template<> X Q<int>::x (); // error: declares a function
|
|
@@ -3683,12 +3810,13 @@ template <> template <> template<class T>
|
|
| 3683 |
template <class Y> template <>
|
| 3684 |
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
|
| 3685 |
// its enclosing class template A is not
|
| 3686 |
```
|
| 3687 |
|
| 3688 |
-
A specialization of a member function template
|
| 3689 |
-
of a non-specialized class template is
|
|
|
|
| 3690 |
|
| 3691 |
An explicit specialization declaration shall not be a friend
|
| 3692 |
declaration.
|
| 3693 |
|
| 3694 |
Default function arguments shall not be specified in a declaration or a
|
|
@@ -3794,13 +3922,13 @@ void h() {
|
|
| 3794 |
// int (*)(bool), Z is deduced to an empty sequence
|
| 3795 |
}
|
| 3796 |
```
|
| 3797 |
|
| 3798 |
An empty template argument list can be used to indicate that a given use
|
| 3799 |
-
refers to a specialization of a function template even when a
|
| 3800 |
-
|
| 3801 |
-
For example:
|
| 3802 |
|
| 3803 |
``` cpp
|
| 3804 |
template <class T> int f(T); // #1
|
| 3805 |
int f(int); // #2
|
| 3806 |
int k = f(1); // uses #2
|
|
@@ -3969,12 +4097,15 @@ int main() {
|
|
| 3969 |
`f<int>(1)` and `f<const int>(1)` call distinct functions even though
|
| 3970 |
both of the functions called have the same function type.
|
| 3971 |
|
| 3972 |
The resulting substituted and adjusted function type is used as the type
|
| 3973 |
of the function template for template argument deduction. If a template
|
| 3974 |
-
argument has not been deduced
|
| 3975 |
-
|
|
|
|
|
|
|
|
|
|
| 3976 |
|
| 3977 |
``` cpp
|
| 3978 |
template <class T, class U = double>
|
| 3979 |
void f(T t = 0, U u = 0);
|
| 3980 |
|
|
@@ -4007,24 +4138,40 @@ The substitution occurs in all types and expressions that are used in
|
|
| 4007 |
the function type and in template parameter declarations. The
|
| 4008 |
expressions include not only constant expressions such as those that
|
| 4009 |
appear in array bounds or as nontype template arguments but also general
|
| 4010 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 4011 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 4012 |
-
|
| 4013 |
-
|
| 4014 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4015 |
|
| 4016 |
If a substitution results in an invalid type or expression, type
|
| 4017 |
deduction fails. An invalid type or expression is one that would be
|
| 4018 |
-
ill-formed if written using the substituted
|
| 4019 |
-
|
| 4020 |
-
|
| 4021 |
-
|
| 4022 |
-
|
| 4023 |
-
|
| 4024 |
-
|
| 4025 |
-
|
|
|
|
| 4026 |
“immediate context” and can result in the program being ill-formed.
|
| 4027 |
|
| 4028 |
``` cpp
|
| 4029 |
struct X { };
|
| 4030 |
struct Y {
|
|
@@ -4154,22 +4301,26 @@ g({1,2,3}); // error: no argument deduced for T
|
|
| 4154 |
For a function parameter pack that occurs at the end of the
|
| 4155 |
*parameter-declaration-list*, the type `A` of each remaining argument of
|
| 4156 |
the call is compared with the type `P` of the *declarator-id* of the
|
| 4157 |
function parameter pack. Each comparison deduces template arguments for
|
| 4158 |
subsequent positions in the template parameter packs expanded by the
|
| 4159 |
-
function parameter pack.
|
| 4160 |
-
|
| 4161 |
-
|
| 4162 |
|
| 4163 |
``` cpp
|
| 4164 |
template<class ... Types> void f(Types& ...);
|
| 4165 |
template<class T1, class ... Types> void g(T1, Types ...);
|
|
|
|
| 4166 |
|
| 4167 |
void h(int x, float& y) {
|
| 4168 |
const int z = x;
|
| 4169 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 4170 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
|
|
|
|
|
|
|
|
|
| 4171 |
}
|
| 4172 |
```
|
| 4173 |
|
| 4174 |
If `P` is not a reference type:
|
| 4175 |
|
|
@@ -4275,17 +4426,22 @@ Template arguments can be deduced from the type specified when taking
|
|
| 4275 |
the address of an overloaded function ([[over.over]]). The function
|
| 4276 |
template’s function type and the specified type are used as the types of
|
| 4277 |
`P` and `A`, and the deduction is done as described in
|
| 4278 |
[[temp.deduct.type]].
|
| 4279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4280 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 4281 |
|
| 4282 |
Template argument deduction is done by comparing the return type of the
|
| 4283 |
-
conversion function template (call it `P`
|
| 4284 |
-
|
| 4285 |
-
|
| 4286 |
-
|
| 4287 |
|
| 4288 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 4289 |
of `P` for type deduction and for any further references to or
|
| 4290 |
transformations of `P` in the remainder of this section.
|
| 4291 |
|
|
@@ -4355,11 +4511,11 @@ argument template and template-1 as the parameter template.
|
|
| 4355 |
The types used to determine the ordering depend on the context in which
|
| 4356 |
the partial ordering is done:
|
| 4357 |
|
| 4358 |
- In the context of a function call, the types used are those function
|
| 4359 |
parameter types for which the function call has arguments.[^7]
|
| 4360 |
-
- In the context of a call to a conversion
|
| 4361 |
the conversion function templates are used.
|
| 4362 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 4363 |
function type is used.
|
| 4364 |
|
| 4365 |
Each type nominated above from the parameter template and the
|
|
@@ -4503,10 +4659,11 @@ specified, template argument deduction fails.
|
|
| 4503 |
|
| 4504 |
The non-deduced contexts are:
|
| 4505 |
|
| 4506 |
- The *nested-name-specifier* of a type that was specified using a
|
| 4507 |
*qualified-id*.
|
|
|
|
| 4508 |
- A non-type template argument or an array bound in which a
|
| 4509 |
subexpression references a template parameter.
|
| 4510 |
- A template parameter used in the parameter type of a function
|
| 4511 |
parameter that has a default argument that is being used in the call
|
| 4512 |
for which argument deduction is being done.
|
|
@@ -4526,11 +4683,11 @@ The non-deduced contexts are:
|
|
| 4526 |
``` cpp
|
| 4527 |
template<class T> void g(T);
|
| 4528 |
g({1,2,3}); // error: no argument deduced for T
|
| 4529 |
```
|
| 4530 |
- A function parameter pack that does not occur at the end of the
|
| 4531 |
-
*parameter-declaration-
|
| 4532 |
|
| 4533 |
When a type name is specified in a way that includes a non-deduced
|
| 4534 |
context, all of the types that comprise that type name are also
|
| 4535 |
non-deduced. However, a compound type can include both deduced and
|
| 4536 |
non-deduced types. If a type is specified as `A<T>::B<T2>`, both `T` and
|
|
@@ -4798,17 +4955,14 @@ int x = deduce<77>(a.xm, 62, b.ym);
|
|
| 4798 |
// A<int>::X
|
| 4799 |
// i is explicitly specified to be 77, b.ym must be convertible
|
| 4800 |
// to B<77>::Y
|
| 4801 |
```
|
| 4802 |
|
| 4803 |
-
If
|
| 4804 |
-
|
| 4805 |
-
|
| 4806 |
-
|
| 4807 |
-
the type of the *template-parameter* exactly, except that a
|
| 4808 |
-
*template-argument* deduced from an array bound may be of any integral
|
| 4809 |
-
type.[^8]
|
| 4810 |
|
| 4811 |
``` cpp
|
| 4812 |
template<int i> class A { /* ... */ };
|
| 4813 |
template<short s> void f(A<s>);
|
| 4814 |
void k1() {
|
|
@@ -5036,10 +5190,11 @@ explicitly generated, is present in some translation unit.
|
|
| 5036 |
[dcl.fct]: dcl.md#dcl.fct
|
| 5037 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5038 |
[dcl.init]: dcl.md#dcl.init
|
| 5039 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5040 |
[dcl.meaning]: dcl.md#dcl.meaning
|
|
|
|
| 5041 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 5042 |
[except.spec]: except.md#except.spec
|
| 5043 |
[expr.const]: expr.md#expr.const
|
| 5044 |
[expr.new]: expr.md#expr.new
|
| 5045 |
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
|
|
|
| 17 |
The `>` token following the of a may be the product of replacing a
|
| 18 |
`>{>}` token by two consecutive `>` tokens ([[temp.names]]).
|
| 19 |
|
| 20 |
The *declaration* in a *template-declaration* shall
|
| 21 |
|
| 22 |
+
- declare or define a function, a class, or a variable, or
|
| 23 |
- define a member function, a member class, a member enumeration, or a
|
| 24 |
static data member of a class template or of a class nested within a
|
| 25 |
class template, or
|
| 26 |
- define a member template of a class or class template, or
|
| 27 |
- be an *alias-declaration*.
|
| 28 |
|
| 29 |
A *template-declaration* is a *declaration*. A *template-declaration* is
|
| 30 |
+
also a definition if its *declaration* defines a function, a class, a
|
| 31 |
+
variable, or a static data member. A declaration introduced by a
|
| 32 |
+
template declaration of a variable is a *variable template*. A variable
|
| 33 |
+
template at class scope is a *static data member template*.
|
| 34 |
+
|
| 35 |
+
``` cpp
|
| 36 |
+
template<class T>
|
| 37 |
+
constexpr T pi = T(3.1415926535897932385L);
|
| 38 |
+
template<class T>
|
| 39 |
+
T circular_area(T r) {
|
| 40 |
+
return pi<T> * r * r;
|
| 41 |
+
}
|
| 42 |
+
struct matrix_constants {
|
| 43 |
+
template<class T>
|
| 44 |
+
using pauli = hermitian_matrix<T, 2>;
|
| 45 |
+
template<class T>
|
| 46 |
+
constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
|
| 47 |
+
template<class T>
|
| 48 |
+
constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };
|
| 49 |
+
template<class T>
|
| 50 |
+
constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
|
| 51 |
+
};
|
| 52 |
+
```
|
| 53 |
|
| 54 |
A *template-declaration* can appear only as a namespace scope or class
|
| 55 |
scope declaration. In a function template declaration, the last
|
| 56 |
component of the *declarator-id* shall not be a *template-id*. That last
|
| 57 |
component may be an *identifier*, an *operator-function-id*, a
|
|
|
|
| 81 |
|
| 82 |
A class template shall not have the same name as any other template,
|
| 83 |
class, function, variable, enumeration, enumerator, namespace, or type
|
| 84 |
in the same scope ([[basic.scope]]), except as specified in (
|
| 85 |
[[temp.class.spec]]). Except that a function template can be overloaded
|
| 86 |
+
either by non-template functions ([[dcl.fct]]) with the same name or by
|
| 87 |
+
other function templates with the same name ([[temp.over]]), a template
|
| 88 |
+
name declared in namespace scope or in class scope shall be unique in
|
| 89 |
+
that scope.
|
| 90 |
|
| 91 |
+
A function template, member function of a class template, variable
|
| 92 |
+
template, or static data member of a class template shall be defined in
|
| 93 |
+
every translation unit in which it is implicitly instantiated (
|
| 94 |
+
[[temp.inst]]) unless the corresponding specialization is explicitly
|
| 95 |
+
instantiated ([[temp.explicit]]) in some translation unit; no
|
| 96 |
+
diagnostic is required.
|
| 97 |
|
| 98 |
## Template parameters <a id="temp.param">[[temp.param]]</a>
|
| 99 |
|
| 100 |
The syntax for *template-parameter*s is:
|
| 101 |
|
|
|
|
| 120 |
|
| 121 |
There is no semantic difference between `class` and `typename` in a
|
| 122 |
*template-parameter*. `typename` followed by an *unqualified-id* names a
|
| 123 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 124 |
the type in a non-type [^1] *parameter-declaration*. A storage class
|
| 125 |
+
shall not be specified in a *template-parameter* declaration. Types
|
| 126 |
+
shall not be defined in a *template-parameter* declaration. A template
|
| 127 |
parameter may be a class template. For example,
|
| 128 |
|
| 129 |
``` cpp
|
| 130 |
template<class T> class myarray { /* ... */ };
|
| 131 |
|
|
|
|
| 250 |
either have a default *template-argument* supplied or be a template
|
| 251 |
parameter pack. If a *template-parameter* of a primary class template or
|
| 252 |
alias template is a template parameter pack, it shall be the last
|
| 253 |
*template-parameter*. A template parameter pack of a function template
|
| 254 |
shall not be followed by another template parameter unless that template
|
| 255 |
+
parameter can be deduced from the *parameter-type-list* of the function
|
| 256 |
+
template or has a default argument ([[temp.deduct]]).
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
template<class T1 = int, class T2> class B; // error
|
| 260 |
|
| 261 |
+
// U can be neither deduced from the parameter-type-list nor specified
|
| 262 |
+
template<class... T, class... U> void f() { } // error
|
| 263 |
+
template<class... T, class U> void g() { } // error
|
| 264 |
```
|
| 265 |
|
| 266 |
A *template-parameter* shall not be given default arguments by two
|
| 267 |
different declarations in the same scope.
|
| 268 |
|
|
|
|
| 318 |
template <class... Types> class Tuple; // Types is a template type parameter pack
|
| 319 |
// but not a pack expansion
|
| 320 |
template <class T, int... Dims> struct multi_array; // Dims is a non-type template parameter pack
|
| 321 |
// but not a pack expansion
|
| 322 |
template<class... T> struct value_holder {
|
| 323 |
+
template<T... Values> struct apply { }; // Values is a non-type template parameter pack
|
| 324 |
// and a pack expansion
|
| 325 |
};
|
| 326 |
template<class... T, T... Values> struct static_array;// error: Values expands template type parameter
|
| 327 |
// pack T within the same template parameter list
|
| 328 |
```
|
|
|
|
| 370 |
arguments, the name must be known to refer to a template.
|
| 371 |
|
| 372 |
After name lookup ([[basic.lookup]]) finds that a name is a
|
| 373 |
*template-name* or that an *operator-function-id* or a
|
| 374 |
*literal-operator-id* refers to a set of overloaded functions any member
|
| 375 |
+
of which is a function template, if this is followed by a `<`, the `<`
|
| 376 |
+
is always taken as the delimiter of a *template-argument-list* and never
|
| 377 |
+
as the less-than operator. When parsing a *template-argument-list*, the
|
| 378 |
first non-nested `>`[^2] is taken as the ending delimiter rather than a
|
| 379 |
greater-than operator. Similarly, the first non-nested `>{>}` is treated
|
| 380 |
as two consecutive but distinct `>` tokens, the first of which is taken
|
| 381 |
as the end of the and completes the . The second `>` token produced by
|
| 382 |
this replacement rule may terminate an enclosing construct or it may be
|
|
|
|
| 441 |
template <class T2> struct C { };
|
| 442 |
};
|
| 443 |
|
| 444 |
// OK: T::template C names a class template:
|
| 445 |
template <class T, template <class X> class TT = T::template C> struct D { };
|
| 446 |
+
D<B<int> > db;
|
| 447 |
```
|
| 448 |
|
| 449 |
A *simple-template-id* that names a class template specialization is a
|
| 450 |
*class-name* (Clause [[class]]).
|
| 451 |
|
|
|
|
| 626 |
- for a non-type *template-parameter* of integral or enumeration type, a
|
| 627 |
converted constant expression ([[expr.const]]) of the type of the
|
| 628 |
*template-parameter*; or
|
| 629 |
- the name of a non-type *template-parameter*; or
|
| 630 |
- a constant expression ([[expr.const]]) that designates the address of
|
| 631 |
+
a complete object with static storage duration and external or
|
| 632 |
+
internal linkage or a function with external or internal linkage,
|
| 633 |
+
including function templates and function *template-id*s but excluding
|
| 634 |
non-static class members, expressed (ignoring parentheses) as `&`
|
| 635 |
+
*id-expression*, where the *id-expression* is the name of an object or
|
| 636 |
+
function, except that the `&` may be omitted if the name refers to a
|
| 637 |
+
function or array and shall be omitted if the corresponding
|
| 638 |
*template-parameter* is a reference; or
|
| 639 |
- a constant expression that evaluates to a null pointer value (
|
| 640 |
[[conv.ptr]]); or
|
| 641 |
- a constant expression that evaluates to a null member pointer value (
|
| 642 |
[[conv.mem]]); or
|
| 643 |
+
- a pointer to member expressed as described in [[expr.unary.op]]; or
|
| 644 |
+
- a constant expression of type `std::nullptr_t`.
|
| 645 |
|
| 646 |
A string literal ([[lex.string]]) does not satisfy the requirements of
|
| 647 |
any of these categories and thus is not an acceptable
|
| 648 |
*template-argument*.
|
| 649 |
|
|
|
|
| 697 |
- for a non-type *template-parameter* of type pointer to object,
|
| 698 |
qualification conversions ([[conv.qual]]) and the array-to-pointer
|
| 699 |
conversion ([[conv.array]]) are applied; if the *template-argument*
|
| 700 |
is of type `std::nullptr_t`, the null pointer conversion (
|
| 701 |
[[conv.ptr]]) is applied. In particular, neither the null pointer
|
| 702 |
+
conversion for a zero-valued integer literal ([[conv.ptr]]) nor the
|
| 703 |
+
derived-to-base conversion ([[conv.ptr]]) are applied. Although `0`
|
| 704 |
+
is a valid *template-argument* for a non-type *template-parameter* of
|
| 705 |
+
integral type, it is not a valid *template-argument* for a non-type
|
| 706 |
+
*template-parameter* of pointer type. However, both `(int*)0` and
|
| 707 |
+
`nullptr` are valid *template-argument*s for a non-type
|
| 708 |
+
*template-parameter* of type “pointer to int.”
|
|
|
|
| 709 |
- For a non-type *template-parameter* of type reference to object, no
|
| 710 |
conversions apply. The type referred to by the reference may be more
|
| 711 |
cv-qualified than the (otherwise identical) type of the
|
| 712 |
*template-argument*. The *template-parameter* is bound directly to the
|
| 713 |
*template-argument*, which shall be an lvalue.
|
|
|
|
| 763 |
template argument with the corresponding parameter; partial
|
| 764 |
specializations are not considered even if their parameter lists match
|
| 765 |
that of the template template parameter.
|
| 766 |
|
| 767 |
Any partial specializations ([[temp.class.spec]]) associated with the
|
| 768 |
+
primary class template or primary variable template are considered when
|
| 769 |
+
a specialization based on the template *template-parameter* is
|
| 770 |
+
instantiated. If a specialization is not visible at the point of
|
| 771 |
+
instantiation, and it would have been selected had it been visible, the
|
| 772 |
+
program is ill-formed; no diagnostic is required.
|
| 773 |
|
| 774 |
``` cpp
|
| 775 |
template<class T> class A { // primary template
|
| 776 |
int x;
|
| 777 |
};
|
|
|
|
| 786 |
// so c.y.x has type int
|
| 787 |
// V<int*> within C<A> uses the partial specialization,
|
| 788 |
// so c.z.x has type long
|
| 789 |
```
|
| 790 |
|
| 791 |
+
A *template-argument* matches a template *template-parameter* (call it
|
| 792 |
+
`P`) when each of the template parameters in the
|
| 793 |
+
*template-parameter-list* of the *template-argument*’s corresponding
|
| 794 |
+
class template or alias template (call it `A`) matches the corresponding
|
| 795 |
+
template parameter in the *template-parameter-list* of `P`. Two template
|
| 796 |
+
parameters match if they are of the same kind (type, non-type,
|
| 797 |
+
template), for non-type *template-parameter*s, their types are
|
| 798 |
+
equivalent ([[temp.over.link]]), and for template
|
| 799 |
+
*template-parameter*s, each of their corresponding *template-parameter*s
|
| 800 |
+
matches, recursively. When `P`’s *template-parameter-list* contains a
|
| 801 |
+
template parameter pack ([[temp.variadic]]), the template parameter
|
| 802 |
+
pack will match zero or more template parameters or template parameter
|
| 803 |
+
packs in the *template-parameter-list* of `A` with the same type and
|
| 804 |
+
form as the template parameter pack in `P` (ignoring whether those
|
| 805 |
+
template parameters are template parameter packs).
|
| 806 |
+
|
| 807 |
``` cpp
|
| 808 |
template<class T> class A { /* ... */ };
|
| 809 |
template<class T, class U = T> class B { /* ... */ };
|
| 810 |
template <class ... Types> class C { /* ... */ };
|
| 811 |
|
|
|
|
| 819 |
Y<A> ya; // OK
|
| 820 |
Y<B> yb; // OK
|
| 821 |
Y<C> yc; // OK
|
| 822 |
```
|
| 823 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
``` cpp
|
| 825 |
template <class T> struct eval;
|
| 826 |
|
| 827 |
template <template <class, class...> class TT, class T1, class... Rest>
|
| 828 |
struct eval<TT<T1, Rest...>> { };
|
|
|
|
| 840 |
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
|
| 841 |
```
|
| 842 |
|
| 843 |
## Type equivalence <a id="temp.type">[[temp.type]]</a>
|
| 844 |
|
| 845 |
+
Two *template-id*s refer to the same class, function, or variable if
|
| 846 |
|
| 847 |
- their *template-name*s, *operator-function-id*s, or
|
| 848 |
*literal-operator-id*s refer to the same template and
|
| 849 |
- their corresponding type *template-argument*s are the same type and
|
| 850 |
- their corresponding non-type template arguments of integral or
|
|
|
|
| 907 |
```
|
| 908 |
|
| 909 |
However, this syntax is allowed in class template partial
|
| 910 |
specializations ([[temp.class.spec]]).
|
| 911 |
|
| 912 |
+
For purposes of name lookup and instantiation, default arguments and
|
| 913 |
+
*exception-specification*s of function templates and default arguments
|
| 914 |
+
and *exception-specification*s of member functions of class templates
|
| 915 |
+
are considered definitions; each default argument or
|
| 916 |
+
*exception-specification* is a separate definition which is unrelated to
|
| 917 |
+
the function template definition or to any other default arguments or
|
| 918 |
+
*exception-specification*s.
|
| 919 |
|
| 920 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 921 |
possible to partially or explicitly specialize an alias template.
|
| 922 |
|
| 923 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
|
|
|
| 1021 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 1022 |
```
|
| 1023 |
|
| 1024 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 1025 |
|
| 1026 |
+
A member class of a class template may be defined outside the class
|
| 1027 |
+
template definition in which it is declared. The member class must be
|
| 1028 |
defined before its first use that requires an instantiation (
|
| 1029 |
[[temp.inst]]). For example,
|
| 1030 |
|
| 1031 |
``` cpp
|
| 1032 |
template<class T> struct A {
|
|
|
|
| 1037 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 1038 |
```
|
| 1039 |
|
| 1040 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 1041 |
|
| 1042 |
+
A definition for a static data member or static data member template may
|
| 1043 |
+
be provided in a namespace scope enclosing the definition of the static
|
| 1044 |
+
member’s class template.
|
| 1045 |
|
| 1046 |
``` cpp
|
| 1047 |
template<class T> class X {
|
| 1048 |
static T s;
|
| 1049 |
};
|
| 1050 |
template<class T> T X<T>::s = 0;
|
| 1051 |
+
|
| 1052 |
+
struct limits {
|
| 1053 |
+
template<class T>
|
| 1054 |
+
static const T min; // declaration
|
| 1055 |
+
};
|
| 1056 |
+
|
| 1057 |
+
template<class T>
|
| 1058 |
+
const T limits::min = { }; // definition
|
| 1059 |
```
|
| 1060 |
|
| 1061 |
An explicit specialization of a static data member declared as an array
|
| 1062 |
of unknown bound can have a different bound from its definition, if any.
|
| 1063 |
|
|
|
|
| 1101 |
|
| 1102 |
template<class T> template<class T2> int string<T>::compare(const T2& s) {
|
| 1103 |
}
|
| 1104 |
```
|
| 1105 |
|
| 1106 |
+
A local class of non-closure type shall not have member templates.
|
| 1107 |
+
Access control rules (Clause [[class.access]]) apply to member template
|
| 1108 |
+
names. A destructor shall not be a member template. A non-template
|
| 1109 |
+
member function ([[dcl.fct]]) with a given name and type and a member
|
| 1110 |
+
function template of the same name, which could be used to generate a
|
| 1111 |
+
specialization of the same type, can both be declared in a class. When
|
| 1112 |
+
both exist, a use of that name and type refers to the non-template
|
| 1113 |
+
member unless an explicit template argument list is supplied.
|
| 1114 |
|
| 1115 |
``` cpp
|
| 1116 |
template <class T> struct A {
|
| 1117 |
void f(int);
|
| 1118 |
template <class T2> void f(T2);
|
| 1119 |
};
|
| 1120 |
|
| 1121 |
+
template <> void A<int>::f(int) { } // non-template member function
|
| 1122 |
+
template <> template <> void A<int>::f<>(int) { } // member function template specialization
|
| 1123 |
|
| 1124 |
int main() {
|
| 1125 |
A<char> ac;
|
| 1126 |
ac.f(1); // non-template
|
| 1127 |
ac.f('c'); // template
|
|
|
|
| 1238 |
*type-parameter* without the ellipsis.
|
| 1239 |
- In an *initializer-list* ([[dcl.init]]); the pattern is an
|
| 1240 |
*initializer-clause*.
|
| 1241 |
- In a *base-specifier-list* (Clause [[class.derived]]); the pattern is
|
| 1242 |
a *base-specifier*.
|
| 1243 |
+
- In a *mem-initializer-list* ([[class.base.init]]) for a
|
| 1244 |
+
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 1245 |
+
pattern is the *mem-initializer*.
|
| 1246 |
- In a *template-argument-list* ([[temp.arg]]); the pattern is a
|
| 1247 |
*template-argument*.
|
| 1248 |
- In a *dynamic-exception-specification* ([[except.spec]]); the pattern
|
| 1249 |
is a *type-id*.
|
| 1250 |
- In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
|
|
|
|
| 1254 |
- In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
|
| 1255 |
*capture*.
|
| 1256 |
- In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
|
| 1257 |
*identifier*.
|
| 1258 |
|
| 1259 |
+
For the purpose of determining whether a parameter pack satisfies a rule
|
| 1260 |
+
regarding entities other than parameter packs, the parameter pack is
|
| 1261 |
+
considered to be the entity that would result from an instantiation of
|
| 1262 |
+
the pattern in which it appears.
|
| 1263 |
+
|
| 1264 |
``` cpp
|
| 1265 |
template<class ... Types> void f(Types ... rest);
|
| 1266 |
template<class ... Types> void g(Types ... rest) {
|
| 1267 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 1268 |
}
|
|
|
|
| 1306 |
The instantiation of a pack expansion that is not a `sizeof...`
|
| 1307 |
expression produces a list
|
| 1308 |
$\mathtt{E}_1, \mathtt{E}_2, ..., \mathtt{E}_N$, where N is the number
|
| 1309 |
of elements in the pack expansion parameters. Each Eᵢ is generated by
|
| 1310 |
instantiating the pattern and replacing each pack expansion parameter
|
| 1311 |
+
with its ith element. Such an element, in the context of the
|
| 1312 |
+
instantiation, is interpreted as follows:
|
| 1313 |
+
|
| 1314 |
+
- if the pack is a template parameter pack, the element is a template
|
| 1315 |
+
parameter ([[temp.param]]) of the corresponding kind (type or
|
| 1316 |
+
non-type) designating the type or value from the template argument;
|
| 1317 |
+
otherwise,
|
| 1318 |
+
- if the pack is a function parameter pack, the element is an
|
| 1319 |
+
*id-expression* designating the function parameter that resulted from
|
| 1320 |
+
the instantiation of the pattern where the pack is declared.
|
| 1321 |
+
|
| 1322 |
+
All of the Eᵢ become elements in the enclosing list. The variety of list
|
| 1323 |
+
varies with the context: *expression-list*, *base-specifier-list*,
|
| 1324 |
+
*template-argument-list*, etc. When N is zero, the instantiation of the
|
| 1325 |
+
expansion produces an empty list. Such an instantiation does not alter
|
| 1326 |
+
the syntactic interpretation of the enclosing construct, even in cases
|
| 1327 |
+
where omitting the list entirely would otherwise be ill-formed or would
|
| 1328 |
+
result in an ambiguity in the grammar.
|
| 1329 |
|
| 1330 |
``` cpp
|
| 1331 |
template<class... T> struct X : T... { };
|
| 1332 |
template<class... T> void f(T... values) {
|
| 1333 |
X<T...> x(values...);
|
|
|
|
| 1343 |
|
| 1344 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 1345 |
|
| 1346 |
A friend of a class or class template can be a function template or
|
| 1347 |
class template, a specialization of a function template or class
|
| 1348 |
+
template, or a non-template function or class. For a friend function
|
| 1349 |
+
declaration that is not a template declaration:
|
| 1350 |
|
| 1351 |
- if the name of the friend is a qualified or unqualified *template-id*,
|
| 1352 |
the friend declaration refers to a specialization of a function
|
| 1353 |
+
template, otherwise,
|
| 1354 |
- if the name of the friend is a *qualified-id* and a matching
|
| 1355 |
non-template function is found in the specified class or namespace,
|
| 1356 |
the friend declaration refers to that function, otherwise,
|
| 1357 |
- if the name of the friend is a *qualified-id* and a matching function
|
| 1358 |
template is found in the specified class or namespace, the friend
|
| 1359 |
declaration refers to the deduced specialization of that function
|
| 1360 |
template ([[temp.deduct.decl]]), otherwise,
|
| 1361 |
+
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 1362 |
+
non-template function.
|
| 1363 |
|
| 1364 |
``` cpp
|
| 1365 |
template<class T> class task;
|
| 1366 |
template<class T> task<T>* preempt(task<T>*);
|
| 1367 |
|
|
|
|
| 1379 |
Here, each specialization of the `task` class template has the function
|
| 1380 |
`next_time` as a friend; because `process` does not have explicit
|
| 1381 |
*template-argument*s, each specialization of the `task` class template
|
| 1382 |
has an appropriately typed function `process` as a friend, and this
|
| 1383 |
friend is not a function template specialization; because the friend
|
| 1384 |
+
`preempt` has an explicit *template-argument* `T`, each specialization
|
| 1385 |
of the `task` class template has the appropriate specialization of the
|
| 1386 |
function template `preempt` as a friend; and each specialization of the
|
| 1387 |
`task` class template has all specializations of the function template
|
| 1388 |
`func` as friends. Similarly, each specialization of the `task` class
|
| 1389 |
template has the class template specialization `task<int>` as a friend,
|
|
|
|
| 1418 |
template<class T> struct A { X::Y ab; }; // OK
|
| 1419 |
template<class T> struct A<T*> { X::Y ab; }; // OK
|
| 1420 |
```
|
| 1421 |
|
| 1422 |
When a function is defined in a friend function declaration in a class
|
| 1423 |
+
template, the function is instantiated when the function is odr-used (
|
| 1424 |
+
[[basic.def.odr]]). The same restrictions on multiple declarations and
|
| 1425 |
+
definitions that apply to non-template function declarations and
|
| 1426 |
+
definitions also apply to these implicit definitions.
|
| 1427 |
|
| 1428 |
A member of a class template may be declared to be a friend of a
|
| 1429 |
non-template class. In this case, the corresponding member of every
|
| 1430 |
specialization of the class template is a friend of the class granting
|
| 1431 |
friendship. For explicit specializations the corresponding member is the
|
|
|
|
| 1593 |
int array[5];
|
| 1594 |
template< int X > class A<X,&array> { }; // error
|
| 1595 |
```
|
| 1596 |
- The argument list of the specialization shall not be identical to the
|
| 1597 |
implicit argument list of the primary template.
|
| 1598 |
+
- The specialization shall be more specialized than the primary
|
| 1599 |
+
template ([[temp.class.order]]).
|
| 1600 |
- The template parameter list of a specialization shall not contain
|
| 1601 |
default template argument values.[^4]
|
| 1602 |
- An argument shall not contain an unexpanded parameter pack. If an
|
| 1603 |
argument is a pack expansion ([[temp.variadic]]), it shall be the
|
| 1604 |
last argument in the template argument list.
|
|
|
|
| 1763 |
template<class T> class Array { };
|
| 1764 |
template<class T> void sort(Array<T>&);
|
| 1765 |
```
|
| 1766 |
|
| 1767 |
A function template can be overloaded with other function templates and
|
| 1768 |
+
with non-template functions ([[dcl.fct]]). A non-template function is
|
| 1769 |
+
not related to a function template (i.e., it is never considered to be a
|
| 1770 |
specialization), even if it has the same name and type as a potentially
|
| 1771 |
generated function template specialization.[^5]
|
| 1772 |
|
| 1773 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1774 |
|
|
|
|
| 1833 |
*equivalent* if two function definitions containing the expressions
|
| 1834 |
would satisfy the one definition rule ([[basic.def.odr]]), except that
|
| 1835 |
the tokens used to name the template parameters may differ as long as a
|
| 1836 |
token used to name a template parameter in one expression is replaced by
|
| 1837 |
another token that names the same template parameter in the other
|
| 1838 |
+
expression. For determining whether two dependent names ([[temp.dep]])
|
| 1839 |
+
are equivalent, only the name itself is considered, not the result of
|
| 1840 |
+
name lookup in the context of the template. If multiple declarations of
|
| 1841 |
+
the same function template differ in the result of this name lookup, the
|
| 1842 |
+
result for the first declaration is used.
|
| 1843 |
|
| 1844 |
``` cpp
|
| 1845 |
template <int I, int J> void f(A<I+J>); // #1
|
| 1846 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 1847 |
+
|
| 1848 |
+
template <class T> decltype(g(T())) h();
|
| 1849 |
+
int g(int);
|
| 1850 |
+
template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
|
| 1851 |
+
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 1852 |
+
int i = h<int>(); // template argument substitution fails; g(int)
|
| 1853 |
+
// was not in scope at the first declaration of h()
|
| 1854 |
```
|
| 1855 |
|
| 1856 |
Two expressions involving template parameters that are not equivalent
|
| 1857 |
are *functionally equivalent* if, for any given set of template
|
| 1858 |
arguments, the evaluation of the expression results in the same value.
|
|
|
|
| 1919 |
To produce the transformed template, for each type, non-type, or
|
| 1920 |
template template parameter (including template parameter packs (
|
| 1921 |
[[temp.variadic]]) thereof) synthesize a unique type, value, or class
|
| 1922 |
template respectively and substitute it for each occurrence of that
|
| 1923 |
parameter in the function type of the template. If only one of the
|
| 1924 |
+
function templates is a non-static member of some class `A`, that
|
| 1925 |
+
function template is considered to have a new first parameter inserted
|
| 1926 |
+
in its function parameter list. Given cv as the cv-qualifiers of the
|
| 1927 |
+
function template (if any), the new parameter is of type “rvalue
|
| 1928 |
+
reference to cv `A`” if the optional *ref-qualifier* of the function
|
| 1929 |
+
template is `&&`, or of type “lvalue reference to cv `A`” otherwise.
|
| 1930 |
+
This allows a non-static member to be ordered with respect to a
|
| 1931 |
+
nonmember function and for the results to be equivalent to the ordering
|
| 1932 |
+
of two equivalent nonmembers.
|
| 1933 |
|
| 1934 |
``` cpp
|
| 1935 |
struct A { };
|
| 1936 |
template<class T> struct B {
|
| 1937 |
template<class R> int operator*(R&); // #1
|
|
|
|
| 2202 |
B b; // OK, no typename required
|
| 2203 |
};
|
| 2204 |
```
|
| 2205 |
|
| 2206 |
Knowing which names are type names allows the syntax of every template
|
| 2207 |
+
to be checked. No diagnostic shall be issued for a template for which a
|
| 2208 |
+
valid specialization can be generated. If no valid specialization can be
|
| 2209 |
+
generated for a template, and that template is not instantiated, the
|
| 2210 |
+
template is ill-formed, no diagnostic required. If every valid
|
| 2211 |
+
specialization of a variadic template requires an empty template
|
| 2212 |
+
parameter pack, the template is ill-formed, no diagnostic required. If a
|
| 2213 |
+
type used in a non-dependent name is incomplete at the point at which a
|
|
|
|
| 2214 |
template is defined but is complete at the point at which an
|
| 2215 |
instantiation is done, and if the completeness of that type affects
|
| 2216 |
whether or not the program is well-formed or affects the semantics of
|
| 2217 |
the program, the program is ill-formed; no diagnostic is required. If a
|
| 2218 |
template is instantiated, errors will be diagnosed according to the
|
|
|
|
| 2303 |
// by two calls of f(E)
|
| 2304 |
g('a'); // will cause three calls of f(char)
|
| 2305 |
}
|
| 2306 |
```
|
| 2307 |
|
| 2308 |
+
For purposes of name lookup, default arguments and
|
| 2309 |
+
*exception-specification*s of function templates and default arguments
|
| 2310 |
+
and *exception-specification*s of member functions of class templates
|
| 2311 |
+
are considered definitions ([[temp.decls]]).
|
| 2312 |
|
| 2313 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 2314 |
|
| 2315 |
Like normal (non-template) classes, class templates have an
|
| 2316 |
injected-class-name (Clause [[class]]). The injected-class-name can be
|
|
|
|
| 2472 |
arguments) and this determines the context for name lookup for certain
|
| 2473 |
names. Expressions may be *type-dependent* (on the type of a template
|
| 2474 |
parameter) or *value-dependent* (on the value of a non-type template
|
| 2475 |
parameter). In an expression of the form:
|
| 2476 |
|
| 2477 |
+
where the *postfix-expression* is an *unqualified-id*, the
|
| 2478 |
+
*unqualified-id* denotes a *dependent name* if
|
| 2479 |
|
| 2480 |
- any of the expressions in the *expression-list* is a pack expansion (
|
| 2481 |
[[temp.variadic]]),
|
| 2482 |
- any of the expressions in the *expression-list* is a type-dependent
|
| 2483 |
expression ([[temp.dep.expr]]), or
|
| 2484 |
+
- if the *unqualified-id* is a *template-id* in which any of the
|
| 2485 |
+
template arguments depends on a template parameter.
|
| 2486 |
|
| 2487 |
If an operand of an operator is a type-dependent expression, the
|
| 2488 |
operator also denotes a dependent name. Such names are unbound and are
|
| 2489 |
looked up at the point of the template instantiation ([[temp.point]])
|
| 2490 |
in both the context of the template definition and the context of the
|
|
|
|
| 2616 |
```
|
| 2617 |
|
| 2618 |
A name is a *member of the current instantiation* if it is
|
| 2619 |
|
| 2620 |
- An unqualified name that, when looked up, refers to at least one
|
| 2621 |
+
member of a class that is the current instantiation or a non-dependent
|
| 2622 |
+
base class thereof. This can only occur when looking up a name in a
|
| 2623 |
+
scope enclosed by the definition of a class template.
|
| 2624 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2625 |
current instantiation and that, when looked up, refers to at least one
|
| 2626 |
+
member of a class that is the current instantiation or a non-dependent
|
| 2627 |
+
base class thereof. if no such member is found, and the current
|
| 2628 |
+
instantiation has any dependent base classes, then the *qualified-id*
|
| 2629 |
+
is a member of an unknown specialization; see below.
|
| 2630 |
- An *id-expression* denoting the member in a class member access
|
| 2631 |
expression ([[expr.ref]]) for which the type of the object expression
|
| 2632 |
is the current instantiation, and the *id-expression*, when looked
|
| 2633 |
+
up ([[basic.lookup.classref]]), refers to at least one member of a
|
| 2634 |
+
class that is the current instantiation or a non-dependent base class
|
| 2635 |
+
thereof. if no such member is found, and the current instantiation has
|
| 2636 |
+
any dependent base classes, then the *id-expression* is a member of an
|
| 2637 |
+
unknown specialization; see below.
|
| 2638 |
|
| 2639 |
``` cpp
|
| 2640 |
template <class T> class A {
|
| 2641 |
static const int i = 5;
|
| 2642 |
int n1[i]; // i refers to a member of the current instantiation
|
|
|
|
| 2648 |
template <class T> int A<T>::f() {
|
| 2649 |
return i; // i refers to a member of the current instantiation
|
| 2650 |
}
|
| 2651 |
```
|
| 2652 |
|
| 2653 |
+
A name is a *dependent member of the current instantiation* if it is a
|
| 2654 |
+
member of the current instantiation that, when looked up, refers to at
|
| 2655 |
+
least one member of a class that is the current instantiation.
|
| 2656 |
+
|
| 2657 |
A name is a *member of an unknown specialization* if it is
|
| 2658 |
|
| 2659 |
- A *qualified-id* in which the *nested-name-specifier* names a
|
| 2660 |
dependent type that is not the current instantiation.
|
| 2661 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2662 |
current instantiation, the current instantiation has at least one
|
| 2663 |
dependent base class, and name lookup of the *qualified-id* does not
|
| 2664 |
+
find any member of a class that is the current instantiation or a
|
| 2665 |
+
non-dependent base class thereof.
|
| 2666 |
- An *id-expression* denoting the member in a class member access
|
| 2667 |
expression ([[expr.ref]]) in which either
|
| 2668 |
- the type of the object expression is the current instantiation, the
|
| 2669 |
current instantiation has at least one dependent base class, and
|
| 2670 |
+
name lookup of the *id-expression* does not find a member of a class
|
| 2671 |
+
that is the current instantiation or a non-dependent base class
|
| 2672 |
+
thereof; or
|
| 2673 |
- the type of the object expression is dependent and is not the
|
| 2674 |
current instantiation.
|
| 2675 |
|
| 2676 |
If a *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2677 |
current instantiation is not a member of the current instantiation or a
|
|
|
|
| 2709 |
|
| 2710 |
A type is dependent if it is
|
| 2711 |
|
| 2712 |
- a template parameter,
|
| 2713 |
- a member of an unknown specialization,
|
| 2714 |
+
- a nested class or enumeration that is a dependent member of the
|
| 2715 |
+
current instantiation,
|
| 2716 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 2717 |
- a compound type constructed from any dependent type,
|
| 2718 |
- an array type constructed from any dependent type or whose size is
|
| 2719 |
specified by a constant expression that is value-dependent,
|
| 2720 |
- a *simple-template-id* in which either the template name is a template
|
|
|
|
| 2740 |
|
| 2741 |
An *id-expression* is type-dependent if it contains
|
| 2742 |
|
| 2743 |
- an *identifier* associated by name lookup with one or more
|
| 2744 |
declarations declared with a dependent type,
|
| 2745 |
+
- an *identifier* associated by name lookup with one or more
|
| 2746 |
+
declarations of member functions of the current instantiation declared
|
| 2747 |
+
with a return type that contains a placeholder type (
|
| 2748 |
+
[[dcl.spec.auto]]),
|
| 2749 |
- a *template-id* that is dependent,
|
| 2750 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 2751 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 2752 |
an unknown specialization;
|
| 2753 |
|
| 2754 |
+
or if it names a dependent member of the current instantiation that is a
|
| 2755 |
+
static data member of type “array of unknown bound of `T`” for some
|
| 2756 |
+
`T` ([[temp.static]]). Expressions of the following forms are
|
| 2757 |
+
type-dependent only if the type specified by the *type-id*,
|
| 2758 |
+
*simple-type-specifier* or *new-type-id* is dependent, even if any
|
| 2759 |
+
subexpression is type-dependent:
|
| 2760 |
|
| 2761 |
Expressions of the following forms are never type-dependent (because the
|
| 2762 |
type of the expression cannot be dependent):
|
| 2763 |
|
| 2764 |
For the standard library macro `offsetof`, see [[support.types]].
|
|
|
|
| 2778 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 2779 |
|
| 2780 |
Except as described below, a constant expression is value-dependent if
|
| 2781 |
any subexpression is value-dependent.
|
| 2782 |
|
| 2783 |
+
An *id-expression* is value-dependent if:
|
| 2784 |
|
| 2785 |
+
- it is a name declared with a dependent type,
|
| 2786 |
+
- it is the name of a non-type template parameter,
|
| 2787 |
+
- it names a member of an unknown specialization,
|
| 2788 |
+
- it names a static data member that is a dependent member of the
|
| 2789 |
+
current instantiation and is not initialized in a *member-declarator*,
|
| 2790 |
+
- it names a static member function that is a dependent member of the
|
| 2791 |
+
current instantiation, or
|
| 2792 |
+
- it is a constant with literal type and is initialized with an
|
| 2793 |
+
expression that is value-dependent.
|
| 2794 |
|
| 2795 |
Expressions of the following form are value-dependent if the
|
| 2796 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 2797 |
dependent:
|
| 2798 |
|
|
|
|
| 2802 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 2803 |
*cast-expression* is value-dependent:
|
| 2804 |
|
| 2805 |
Expressions of the following form are value-dependent:
|
| 2806 |
|
| 2807 |
+
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 2808 |
+
names a dependent member of the current instantiation is
|
| 2809 |
+
value-dependent.
|
| 2810 |
|
| 2811 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 2812 |
|
| 2813 |
A type *template-argument* is dependent if the type it specifies is
|
| 2814 |
dependent.
|
|
|
|
| 2876 |
in a way which uses the definition of a default argument of that
|
| 2877 |
function template or member function, the point of instantiation of the
|
| 2878 |
default argument is the point of instantiation of the function template
|
| 2879 |
or member function specialization.
|
| 2880 |
|
| 2881 |
+
For an *exception-specification* of a function template specialization
|
| 2882 |
+
or specialization of a member function of a class template, if the
|
| 2883 |
+
*exception-specification* is implicitly instantiated because it is
|
| 2884 |
+
needed by another template specialization and the context that requires
|
| 2885 |
+
it depends on a template parameter, the point of instantiation of the
|
| 2886 |
+
*exception-specification* is the point of instantiation of the
|
| 2887 |
+
specialization that requires it. Otherwise, the point of instantiation
|
| 2888 |
+
for such an *exception-specification* immediately follows the namespace
|
| 2889 |
+
scope declaration or definition that requires the
|
| 2890 |
+
*exception-specification*.
|
| 2891 |
+
|
| 2892 |
For a class template specialization, a class member template
|
| 2893 |
specialization, or a specialization for a class member of a class
|
| 2894 |
template, if the specialization is implicitly instantiated because it is
|
| 2895 |
referenced from within another template specialization, if the context
|
| 2896 |
from which the specialization is referenced depends on a template
|
|
|
|
| 2927 |
specialization different meanings according to the one definition rule (
|
| 2928 |
[[basic.def.odr]]), the program is ill-formed, no diagnostic required.
|
| 2929 |
|
| 2930 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 2931 |
|
| 2932 |
+
For a function call where the *postfix-expression* is a dependent name,
|
| 2933 |
+
the candidate functions are found using the usual lookup rules (
|
| 2934 |
+
[[basic.lookup.unqual]], [[basic.lookup.argdep]]) except that:
|
|
|
|
| 2935 |
|
| 2936 |
- For the part of the lookup using unqualified name lookup (
|
| 2937 |
+
[[basic.lookup.unqual]]), only function declarations from the template
|
|
|
|
| 2938 |
definition context are found.
|
| 2939 |
- For the part of the lookup using associated namespaces (
|
| 2940 |
[[basic.lookup.argdep]]), only function declarations found in either
|
| 2941 |
the template definition context or the template instantiation context
|
| 2942 |
are found.
|
| 2943 |
|
| 2944 |
+
If the call would be ill-formed or would find a better match had the
|
| 2945 |
+
lookup within the associated namespaces considered all the function
|
| 2946 |
+
declarations with external linkage introduced in those namespaces in all
|
| 2947 |
+
translation units, not just considering those declarations found in the
|
| 2948 |
+
template definition and template instantiation contexts, then the
|
| 2949 |
+
program has undefined behavior.
|
|
|
|
| 2950 |
|
| 2951 |
### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
|
| 2952 |
|
| 2953 |
Friend classes or functions can be declared within a class template.
|
| 2954 |
When a template is instantiated, the names of its friends are treated as
|
|
|
|
| 3064 |
[[temp.expl.spec]]), the class template specialization is implicitly
|
| 3065 |
instantiated when the specialization is referenced in a context that
|
| 3066 |
requires a completely-defined object type or when the completeness of
|
| 3067 |
the class type affects the semantics of the program. The implicit
|
| 3068 |
instantiation of a class template specialization causes the implicit
|
| 3069 |
+
instantiation of the declarations, but not of the definitions, default
|
| 3070 |
+
arguments, or *exception-specification*s of the class member functions,
|
| 3071 |
+
member classes, scoped member enumerations, static data members and
|
| 3072 |
+
member templates; and it causes the implicit instantiation of the
|
| 3073 |
+
definitions of unscoped member enumerations and member anonymous unions.
|
| 3074 |
+
However, for the purpose of determining whether an instantiated
|
| 3075 |
+
redeclaration of a member is valid according to [[class.mem]], a
|
| 3076 |
+
declaration that corresponds to a definition in the template is
|
| 3077 |
+
considered to be a definition.
|
| 3078 |
|
| 3079 |
``` cpp
|
| 3080 |
template<class T, class U>
|
| 3081 |
struct Outer {
|
| 3082 |
template<class X, class Y> struct Inner;
|
|
|
|
| 3131 |
```
|
| 3132 |
|
| 3133 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 3134 |
`Z<char>::f()` to be implicitly instantiated.
|
| 3135 |
|
| 3136 |
+
Unless a variable template specialization has been explicitly
|
| 3137 |
+
instantiated or explicitly specialized, the variable template
|
| 3138 |
+
specialization is implicitly instantiated when the specialization is
|
| 3139 |
+
used. A default template argument for a variable template is implicitly
|
| 3140 |
+
instantiated when the variable template is referenced in a context that
|
| 3141 |
+
requires the value of the default argument.
|
| 3142 |
+
|
| 3143 |
A class template specialization is implicitly instantiated if the class
|
| 3144 |
type is used in a context that requires a completely-defined object type
|
| 3145 |
or if the completeness of the class type might affect the semantics of
|
| 3146 |
the program. In particular, if the semantics of an expression depend on
|
| 3147 |
the member or base class lists of a class template specialization, the
|
|
|
|
| 3201 |
If a function template or a member function template specialization is
|
| 3202 |
used in a way that involves overload resolution, a declaration of the
|
| 3203 |
specialization is implicitly instantiated ([[temp.over]]).
|
| 3204 |
|
| 3205 |
An implementation shall not implicitly instantiate a function template,
|
| 3206 |
+
a variable template, a member template, a non-virtual member function, a
|
| 3207 |
+
member class, or a static data member of a class template that does not
|
| 3208 |
+
require instantiation. It is unspecified whether or not an
|
| 3209 |
+
implementation implicitly instantiates a virtual member function of a
|
| 3210 |
+
class template if the virtual member function would not otherwise be
|
| 3211 |
+
instantiated. The use of a template specialization in a default argument
|
| 3212 |
+
shall not cause the template to be implicitly instantiated except that a
|
| 3213 |
+
class template may be instantiated where its complete type is needed to
|
| 3214 |
+
determine the correctness of the default argument. The use of a default
|
| 3215 |
+
argument in a function call causes specializations in the default
|
| 3216 |
+
argument to be implicitly instantiated.
|
| 3217 |
|
| 3218 |
+
Implicitly instantiated class, function, and variable template
|
| 3219 |
+
specializations are placed in the namespace where the template is
|
| 3220 |
+
defined. Implicitly instantiated specializations for members of a class
|
| 3221 |
+
template are placed in the namespace where the enclosing class template
|
| 3222 |
+
is defined. Implicitly instantiated member templates are placed in the
|
| 3223 |
+
namespace where the enclosing class or class template is defined.
|
| 3224 |
|
| 3225 |
``` cpp
|
| 3226 |
namespace N {
|
| 3227 |
template<class T> class List {
|
| 3228 |
public:
|
|
|
|
| 3249 |
argument to be used, the dependent names are looked up, the semantics
|
| 3250 |
constraints are checked, and the instantiation of any template used in
|
| 3251 |
the default argument is done as if the default argument had been an
|
| 3252 |
initializer used in a function template specialization with the same
|
| 3253 |
scope, the same template parameters and the same access as that of the
|
| 3254 |
+
function template `f` used at that point, except that the scope in which
|
| 3255 |
+
a closure type is declared ([[expr.prim.lambda]]) – and therefore its
|
| 3256 |
+
associated namespaces – remain as determined from the context of the
|
| 3257 |
+
definition for the default argument. This analysis is called *default
|
| 3258 |
+
argument instantiation*. The instantiated default argument is then used
|
| 3259 |
+
as the argument of `f`.
|
| 3260 |
|
| 3261 |
Each default argument is instantiated independently.
|
| 3262 |
|
| 3263 |
``` cpp
|
| 3264 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
|
|
|
| 3272 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 3273 |
f(a); // ill-formed; ydef is not declared
|
| 3274 |
}
|
| 3275 |
```
|
| 3276 |
|
| 3277 |
+
The *exception-specification* of a function template specialization is
|
| 3278 |
+
not instantiated along with the function declaration; it is instantiated
|
| 3279 |
+
when needed ([[except.spec]]). If such an *exception-specification* is
|
| 3280 |
+
needed but has not yet been instantiated, the dependent names are looked
|
| 3281 |
+
up, the semantics constraints are checked, and the instantiation of any
|
| 3282 |
+
template used in the *exception-specification* is done as if it were
|
| 3283 |
+
being done as part of instantiating the declaration of the
|
| 3284 |
+
specialization at that point.
|
| 3285 |
+
|
| 3286 |
[[temp.point]] defines the point of instantiation of a template
|
| 3287 |
specialization.
|
| 3288 |
|
| 3289 |
There is an implementation-defined quantity that specifies the limit on
|
| 3290 |
the total depth of recursive instantiations, which could involve more
|
|
|
|
| 3300 |
};
|
| 3301 |
```
|
| 3302 |
|
| 3303 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 3304 |
|
| 3305 |
+
A class, function, variable, or member template specialization can be
|
| 3306 |
+
explicitly instantiated from its template. A member function, member
|
| 3307 |
+
class or static data member of a class template can be explicitly
|
| 3308 |
+
instantiated from the member definition associated with its class
|
| 3309 |
+
template. An explicit instantiation of a function template or member
|
| 3310 |
+
function of a class template shall not use the `inline` or `constexpr`
|
| 3311 |
+
specifiers.
|
| 3312 |
|
| 3313 |
The syntax for explicit instantiation is:
|
| 3314 |
|
| 3315 |
``` bnf
|
| 3316 |
explicit-instantiation:
|
|
|
|
| 3330 |
a *qualified-id*, in which case the *unqualified-id* of the
|
| 3331 |
*qualified-id* must be a *template-id*. If the explicit instantiation is
|
| 3332 |
for a member function, a member class or a static data member of a class
|
| 3333 |
template specialization, the name of the class template specialization
|
| 3334 |
in the *qualified-id* for the member name shall be a
|
| 3335 |
+
*simple-template-id*. If the explicit instantiation is for a variable,
|
| 3336 |
+
the *unqualified-id* in the declaration shall be a *template-id*. An
|
| 3337 |
+
explicit instantiation shall appear in an enclosing namespace of its
|
| 3338 |
+
template. If the name declared in the explicit instantiation is an
|
| 3339 |
+
unqualified name, the explicit instantiation shall appear in the
|
| 3340 |
+
namespace where its template is declared or, if that namespace is
|
| 3341 |
+
inline ([[namespace.def]]), any namespace from its enclosing namespace
|
| 3342 |
+
set. Regarding qualified names in declarators, see [[dcl.meaning]].
|
| 3343 |
|
| 3344 |
``` cpp
|
| 3345 |
template<class T> class Array { void mf(); };
|
| 3346 |
template class Array<char>;
|
| 3347 |
template void Array<int>::mf();
|
|
|
|
| 3353 |
template<class T> void f(T&) { }
|
| 3354 |
}
|
| 3355 |
template void N::f<int>(int&);
|
| 3356 |
```
|
| 3357 |
|
| 3358 |
+
A declaration of a function template, a variable template, a member
|
| 3359 |
+
function or static data member of a class template, or a member function
|
| 3360 |
+
template of a class or class template shall precede an explicit
|
| 3361 |
+
instantiation of that entity. A definition of a class template, a member
|
| 3362 |
+
class of a class template, or a member class template of a class or
|
| 3363 |
+
class template shall precede an explicit instantiation of that entity
|
| 3364 |
+
unless the explicit instantiation is preceded by an explicit
|
| 3365 |
+
specialization of the entity with the same template arguments. If the
|
| 3366 |
+
*declaration* of the explicit instantiation names an implicitly-declared
|
| 3367 |
+
special member function (Clause [[special]]), the program is
|
| 3368 |
+
ill-formed.
|
| 3369 |
|
| 3370 |
For a given set of template arguments, if an explicit instantiation of a
|
| 3371 |
template appears after a declaration of an explicit specialization for
|
| 3372 |
that template, the explicit instantiation has no effect. Otherwise, for
|
| 3373 |
an explicit instantiation definition the definition of a function
|
| 3374 |
+
template, a variable template, a member function template, or a member
|
| 3375 |
+
function or static data member of a class template shall be present in
|
| 3376 |
+
every translation unit in which it is explicitly instantiated.
|
| 3377 |
|
| 3378 |
+
An explicit instantiation of a class, function template, or variable
|
| 3379 |
+
template specialization is placed in the namespace in which the template
|
| 3380 |
+
is defined. An explicit instantiation for a member of a class template
|
| 3381 |
+
is placed in the namespace where the enclosing class template is
|
| 3382 |
+
defined. An explicit instantiation for a member template is placed in
|
| 3383 |
+
the namespace where the enclosing class or class template is defined.
|
| 3384 |
|
| 3385 |
``` cpp
|
| 3386 |
namespace N {
|
| 3387 |
template<class T> class Y { void mf() { } };
|
| 3388 |
}
|
|
|
|
| 3413 |
```
|
| 3414 |
|
| 3415 |
An explicit instantiation that names a class template specialization is
|
| 3416 |
also an explicit instantiation of the same kind (declaration or
|
| 3417 |
definition) of each of its members (not including members inherited from
|
| 3418 |
+
base classes and members that are templates) that has not been
|
| 3419 |
+
previously explicitly specialized in the translation unit containing the
|
| 3420 |
+
explicit instantiation, except as described below. In addition, it will
|
| 3421 |
+
typically be an explicit instantiation of certain
|
| 3422 |
+
implementation-dependent data about the class.
|
| 3423 |
|
| 3424 |
An explicit instantiation definition that names a class template
|
| 3425 |
specialization explicitly instantiates the class template specialization
|
| 3426 |
and is an explicit instantiation definition of only those members that
|
| 3427 |
have been defined at the point of instantiation.
|
| 3428 |
|
| 3429 |
+
Except for inline functions, declarations with types deduced from their
|
| 3430 |
+
initializer or return value ([[dcl.spec.auto]]), `const` variables of
|
| 3431 |
+
literal types, variables of reference types, and class template
|
| 3432 |
+
specializations, explicit instantiation declarations have the effect of
|
| 3433 |
+
suppressing the implicit instantiation of the entity to which they
|
| 3434 |
+
refer. The intent is that an inline function that is the subject of an
|
| 3435 |
+
explicit instantiation declaration will still be implicitly instantiated
|
| 3436 |
+
when odr-used ([[basic.def.odr]]) so that the body can be considered
|
| 3437 |
+
for inlining, but that no out-of-line copy of the inline function would
|
| 3438 |
+
be generated in the translation unit.
|
| 3439 |
|
| 3440 |
If an entity is the subject of both an explicit instantiation
|
| 3441 |
declaration and an explicit instantiation definition in the same
|
| 3442 |
translation unit, the definition shall follow the declaration. An entity
|
| 3443 |
that is the subject of an explicit instantiation declaration and that is
|
|
|
|
| 3473 |
|
| 3474 |
An explicit specialization of any of the following:
|
| 3475 |
|
| 3476 |
- function template
|
| 3477 |
- class template
|
| 3478 |
+
- variable template
|
| 3479 |
- member function of a class template
|
| 3480 |
- static data member of a class template
|
| 3481 |
- member class of a class template
|
| 3482 |
- member enumeration of a class template
|
| 3483 |
- member class template of a class or class template
|
|
|
|
| 3515 |
[[namespace.def]]), any namespace from its enclosing namespace set. Such
|
| 3516 |
a declaration may also be a definition. If the declaration is not a
|
| 3517 |
definition, the specialization may be defined later (
|
| 3518 |
[[namespace.memdef]]).
|
| 3519 |
|
| 3520 |
+
A declaration of a function template, class template, or variable
|
| 3521 |
+
template being explicitly specialized shall precede the declaration of
|
| 3522 |
+
the explicit specialization. A declaration, but not a definition of the
|
| 3523 |
+
template is required. The definition of a class or class template shall
|
| 3524 |
+
precede the declaration of an explicit specialization for a member
|
| 3525 |
+
template of the class or class template.
|
| 3526 |
|
| 3527 |
``` cpp
|
| 3528 |
template<> class X<int> { /* ... */ }; // error: X not a template
|
| 3529 |
|
| 3530 |
template<class T> class X;
|
| 3531 |
|
| 3532 |
template<> class X<char*> { /* ... */ }; // OK: X is a template
|
| 3533 |
```
|
| 3534 |
|
| 3535 |
A member function, a member function template, a member class, a member
|
| 3536 |
+
enumeration, a member class template, a static data member, or a static
|
| 3537 |
+
data member template of a class template may be explicitly specialized
|
| 3538 |
+
for a class specialization that is implicitly instantiated; in this
|
| 3539 |
+
case, the definition of the class template shall precede the explicit
|
| 3540 |
+
specialization for the member of the class template. If such an explicit
|
| 3541 |
+
specialization for the member of a class template names an
|
| 3542 |
+
implicitly-declared special member function (Clause [[special]]), the
|
| 3543 |
+
program is ill-formed.
|
| 3544 |
|
| 3545 |
A member of an explicitly specialized class is not implicitly
|
| 3546 |
instantiated from the member declaration of the class template; instead,
|
| 3547 |
the member of the class template specialization shall itself be
|
| 3548 |
explicitly defined if its definition is required. In this case, the
|
|
|
|
| 3634 |
};
|
| 3635 |
template<> enum A<int>::E : int { eint }; // OK
|
| 3636 |
template<> enum class A<int>::S : int { sint }; // OK
|
| 3637 |
template<class T> enum A<T>::E : T { eT };
|
| 3638 |
template<class T> enum class A<T>::S : T { sT };
|
| 3639 |
+
template<> enum A<char>::E : char { echar }; // ill-formed, A<char>::E was instantiated
|
| 3640 |
// when A<char> was instantiated
|
| 3641 |
+
template<> enum class A<char>::S : char { schar }; // OK
|
| 3642 |
```
|
| 3643 |
|
| 3644 |
The placement of explicit specialization declarations for function
|
| 3645 |
+
templates, class templates, variable templates, member functions of
|
| 3646 |
+
class templates, static data members of class templates, member classes
|
| 3647 |
+
of class templates, member enumerations of class templates, member class
|
| 3648 |
+
templates of class templates, member function templates of class
|
| 3649 |
+
templates, static data member templates of class templates, member
|
| 3650 |
functions of member templates of class templates, member functions of
|
| 3651 |
+
member templates of non-template classes, static data member templates
|
| 3652 |
+
of non-template classes, member function templates of member classes of
|
| 3653 |
+
class templates, etc., and the placement of partial specialization
|
| 3654 |
+
declarations of class templates, variable templates, member class
|
| 3655 |
+
templates of non-template classes, static data member templates of
|
| 3656 |
+
non-template classes, member class templates of class templates, etc.,
|
| 3657 |
+
can affect whether a program is well-formed according to the relative
|
| 3658 |
+
positioning of the explicit specialization declarations and their points
|
| 3659 |
+
of instantiation in the translation unit as specified above and below.
|
| 3660 |
+
When writing a specialization, be careful about its location; or to make
|
| 3661 |
+
it compile will be such a trial as to kindle its self-immolation.
|
| 3662 |
|
| 3663 |
A template explicit specialization is in the scope of the namespace in
|
| 3664 |
which the template was defined.
|
| 3665 |
|
| 3666 |
``` cpp
|
|
|
|
| 3673 |
template<> class Y<double>; // forward declare intent to
|
| 3674 |
// specialize for double
|
| 3675 |
}
|
| 3676 |
|
| 3677 |
template<> class N::Y<double> { /* ... */ }; // OK: specialization
|
| 3678 |
+
// in enclosing namespace
|
| 3679 |
+
template<> class N::Y<short> { /* ... */ }; // OK: specialization
|
| 3680 |
+
// in enclosing namespace
|
| 3681 |
```
|
| 3682 |
|
| 3683 |
A *simple-template-id* that names a class template explicit
|
| 3684 |
specialization that has been declared but not defined can be used
|
| 3685 |
exactly like the names of other incompletely-defined classes (
|
|
|
|
| 3720 |
|
| 3721 |
template<> inline void f<>(int) { /* ... */ } // OK: inline
|
| 3722 |
template<> int g<>(int) { /* ... */ } // OK: not inline
|
| 3723 |
```
|
| 3724 |
|
| 3725 |
+
An explicit specialization of a static data member of a template or an
|
| 3726 |
+
explicit specialization of a static data member template is a definition
|
| 3727 |
+
if the declaration includes an initializer; otherwise, it is a
|
| 3728 |
+
declaration. The definition of a static data member of a template that
|
| 3729 |
requires default initialization must use a *braced-init-list*:
|
| 3730 |
|
| 3731 |
``` cpp
|
| 3732 |
template<> X Q<int>::x; // declaration
|
| 3733 |
template<> X Q<int>::x (); // error: declares a function
|
|
|
|
| 3810 |
template <class Y> template <>
|
| 3811 |
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
|
| 3812 |
// its enclosing class template A is not
|
| 3813 |
```
|
| 3814 |
|
| 3815 |
+
A specialization of a member function template, member class template,
|
| 3816 |
+
or static data member template of a non-specialized class template is
|
| 3817 |
+
itself a template.
|
| 3818 |
|
| 3819 |
An explicit specialization declaration shall not be a friend
|
| 3820 |
declaration.
|
| 3821 |
|
| 3822 |
Default function arguments shall not be specified in a declaration or a
|
|
|
|
| 3922 |
// int (*)(bool), Z is deduced to an empty sequence
|
| 3923 |
}
|
| 3924 |
```
|
| 3925 |
|
| 3926 |
An empty template argument list can be used to indicate that a given use
|
| 3927 |
+
refers to a specialization of a function template even when a
|
| 3928 |
+
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 3929 |
+
used. For example:
|
| 3930 |
|
| 3931 |
``` cpp
|
| 3932 |
template <class T> int f(T); // #1
|
| 3933 |
int f(int); // #2
|
| 3934 |
int k = f(1); // uses #2
|
|
|
|
| 4097 |
`f<int>(1)` and `f<const int>(1)` call distinct functions even though
|
| 4098 |
both of the functions called have the same function type.
|
| 4099 |
|
| 4100 |
The resulting substituted and adjusted function type is used as the type
|
| 4101 |
of the function template for template argument deduction. If a template
|
| 4102 |
+
argument has not been deduced and its corresponding template parameter
|
| 4103 |
+
has a default argument, the template argument is determined by
|
| 4104 |
+
substituting the template arguments determined for preceding template
|
| 4105 |
+
parameters into the default argument. If the substitution results in an
|
| 4106 |
+
invalid type, as described above, type deduction fails.
|
| 4107 |
|
| 4108 |
``` cpp
|
| 4109 |
template <class T, class U = double>
|
| 4110 |
void f(T t = 0, U u = 0);
|
| 4111 |
|
|
|
|
| 4138 |
the function type and in template parameter declarations. The
|
| 4139 |
expressions include not only constant expressions such as those that
|
| 4140 |
appear in array bounds or as nontype template arguments but also general
|
| 4141 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 4142 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 4143 |
+
substitution proceeds in lexical order and stops when a condition that
|
| 4144 |
+
causes deduction to fail is encountered. The equivalent substitution in
|
| 4145 |
+
exception specifications is done only when the *exception-specification*
|
| 4146 |
+
is instantiated, at which point a program is ill-formed if the
|
| 4147 |
+
substitution results in an invalid type or expression.
|
| 4148 |
+
|
| 4149 |
+
``` cpp
|
| 4150 |
+
template <class T> struct A { using X = typename T::X; };
|
| 4151 |
+
template <class T> typename T::X f(typename A<T>::X);
|
| 4152 |
+
template <class T> void f(...) { }
|
| 4153 |
+
template <class T> auto g(typename A<T>::X) -> typename T::X;
|
| 4154 |
+
template <class T> void g(...) { }
|
| 4155 |
+
|
| 4156 |
+
void h() {
|
| 4157 |
+
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 4158 |
+
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 4159 |
+
}
|
| 4160 |
+
```
|
| 4161 |
|
| 4162 |
If a substitution results in an invalid type or expression, type
|
| 4163 |
deduction fails. An invalid type or expression is one that would be
|
| 4164 |
+
ill-formed, with a diagnostic required, if written using the substituted
|
| 4165 |
+
arguments. If no diagnostic is required, the program is still
|
| 4166 |
+
ill-formed. Access checking is done as part of the substitution process.
|
| 4167 |
+
Only invalid types and expressions in the immediate context of the
|
| 4168 |
+
function type and its template parameter types can result in a deduction
|
| 4169 |
+
failure. The evaluation of the substituted types and expressions can
|
| 4170 |
+
result in side effects such as the instantiation of class template
|
| 4171 |
+
specializations and/or function template specializations, the generation
|
| 4172 |
+
of implicitly-defined functions, etc. Such side effects are not in the
|
| 4173 |
“immediate context” and can result in the program being ill-formed.
|
| 4174 |
|
| 4175 |
``` cpp
|
| 4176 |
struct X { };
|
| 4177 |
struct Y {
|
|
|
|
| 4301 |
For a function parameter pack that occurs at the end of the
|
| 4302 |
*parameter-declaration-list*, the type `A` of each remaining argument of
|
| 4303 |
the call is compared with the type `P` of the *declarator-id* of the
|
| 4304 |
function parameter pack. Each comparison deduces template arguments for
|
| 4305 |
subsequent positions in the template parameter packs expanded by the
|
| 4306 |
+
function parameter pack. When a function parameter pack appears in a
|
| 4307 |
+
non-deduced context ([[temp.deduct.type]]), the type of that parameter
|
| 4308 |
+
pack is never deduced.
|
| 4309 |
|
| 4310 |
``` cpp
|
| 4311 |
template<class ... Types> void f(Types& ...);
|
| 4312 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 4313 |
+
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 4314 |
|
| 4315 |
void h(int x, float& y) {
|
| 4316 |
const int z = x;
|
| 4317 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 4318 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 4319 |
+
g1(x, y, z); // error: Types is not deduced
|
| 4320 |
+
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 4321 |
+
|
| 4322 |
}
|
| 4323 |
```
|
| 4324 |
|
| 4325 |
If `P` is not a reference type:
|
| 4326 |
|
|
|
|
| 4426 |
the address of an overloaded function ([[over.over]]). The function
|
| 4427 |
template’s function type and the specified type are used as the types of
|
| 4428 |
`P` and `A`, and the deduction is done as described in
|
| 4429 |
[[temp.deduct.type]].
|
| 4430 |
|
| 4431 |
+
A placeholder type ([[dcl.spec.auto]]) in the return type of a function
|
| 4432 |
+
template is a non-deduced context. If template argument deduction
|
| 4433 |
+
succeeds for such a function, the return type is determined from
|
| 4434 |
+
instantiation of the function body.
|
| 4435 |
+
|
| 4436 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 4437 |
|
| 4438 |
Template argument deduction is done by comparing the return type of the
|
| 4439 |
+
conversion function template (call it `P`) with the type that is
|
| 4440 |
+
required as the result of the conversion (call it `A`; see
|
| 4441 |
+
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
|
| 4442 |
+
determination of that type) as described in [[temp.deduct.type]].
|
| 4443 |
|
| 4444 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 4445 |
of `P` for type deduction and for any further references to or
|
| 4446 |
transformations of `P` in the remainder of this section.
|
| 4447 |
|
|
|
|
| 4511 |
The types used to determine the ordering depend on the context in which
|
| 4512 |
the partial ordering is done:
|
| 4513 |
|
| 4514 |
- In the context of a function call, the types used are those function
|
| 4515 |
parameter types for which the function call has arguments.[^7]
|
| 4516 |
+
- In the context of a call to a conversion function, the return types of
|
| 4517 |
the conversion function templates are used.
|
| 4518 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 4519 |
function type is used.
|
| 4520 |
|
| 4521 |
Each type nominated above from the parameter template and the
|
|
|
|
| 4659 |
|
| 4660 |
The non-deduced contexts are:
|
| 4661 |
|
| 4662 |
- The *nested-name-specifier* of a type that was specified using a
|
| 4663 |
*qualified-id*.
|
| 4664 |
+
- The *expression* of a *decltype-specifier*.
|
| 4665 |
- A non-type template argument or an array bound in which a
|
| 4666 |
subexpression references a template parameter.
|
| 4667 |
- A template parameter used in the parameter type of a function
|
| 4668 |
parameter that has a default argument that is being used in the call
|
| 4669 |
for which argument deduction is being done.
|
|
|
|
| 4683 |
``` cpp
|
| 4684 |
template<class T> void g(T);
|
| 4685 |
g({1,2,3}); // error: no argument deduced for T
|
| 4686 |
```
|
| 4687 |
- A function parameter pack that does not occur at the end of the
|
| 4688 |
+
*parameter-declaration-list*.
|
| 4689 |
|
| 4690 |
When a type name is specified in a way that includes a non-deduced
|
| 4691 |
context, all of the types that comprise that type name are also
|
| 4692 |
non-deduced. However, a compound type can include both deduced and
|
| 4693 |
non-deduced types. If a type is specified as `A<T>::B<T2>`, both `T` and
|
|
|
|
| 4955 |
// A<int>::X
|
| 4956 |
// i is explicitly specified to be 77, b.ym must be convertible
|
| 4957 |
// to B<77>::Y
|
| 4958 |
```
|
| 4959 |
|
| 4960 |
+
If `P` has a form that contains `<i>`, and if the type of the
|
| 4961 |
+
corresponding value of `A` differs from the type of `i`, deduction
|
| 4962 |
+
fails. If `P` has a form that contains `[i]`, and if the type of `i` is
|
| 4963 |
+
not an integral type, deduction fails.[^8]
|
|
|
|
|
|
|
|
|
|
| 4964 |
|
| 4965 |
``` cpp
|
| 4966 |
template<int i> class A { /* ... */ };
|
| 4967 |
template<short s> void f(A<s>);
|
| 4968 |
void k1() {
|
|
|
|
| 5190 |
[dcl.fct]: dcl.md#dcl.fct
|
| 5191 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5192 |
[dcl.init]: dcl.md#dcl.init
|
| 5193 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5194 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 5195 |
+
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 5196 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 5197 |
[except.spec]: except.md#except.spec
|
| 5198 |
[expr.const]: expr.md#expr.const
|
| 5199 |
[expr.new]: expr.md#expr.new
|
| 5200 |
[expr.prim.lambda]: expr.md#expr.prim.lambda
|