- tmp/tmpw8u0tv68/{from.md → to.md} +439 -161
tmp/tmpw8u0tv68/{from.md → to.md}
RENAMED
|
@@ -2,37 +2,45 @@
|
|
| 2 |
|
| 3 |
A *template-id*, that is, the *template-name* followed by a
|
| 4 |
*template-argument-list* shall not be specified in the declaration of a
|
| 5 |
primary template declaration.
|
| 6 |
|
|
|
|
|
|
|
| 7 |
``` cpp
|
| 8 |
template<class T1, class T2, int I> class A<T1, T2, I> { }; // error
|
| 9 |
template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
|
| 10 |
```
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
For purposes of name lookup and instantiation, default arguments and
|
| 16 |
-
*
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 24 |
possible to partially or explicitly specialize an alias template.
|
| 25 |
|
| 26 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 27 |
|
| 28 |
-
A class
|
| 29 |
-
set of related types.
|
| 30 |
-
common definition for list of `int`, list of `float`, and list of
|
| 31 |
-
pointers to `Shape`s.
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
``` cpp
|
| 36 |
template<class T> class Array {
|
| 37 |
T* v;
|
| 38 |
int sz;
|
|
@@ -41,14 +49,16 @@ public:
|
|
| 41 |
T& operator[](int);
|
| 42 |
T& elem(int i) { return v[i]; }
|
| 43 |
};
|
| 44 |
```
|
| 45 |
|
| 46 |
-
The prefix `template
|
| 47 |
-
declared and that a *type-name* `T`
|
| 48 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 49 |
|
|
|
|
|
|
|
| 50 |
When a member function, a member class, a member enumeration, a static
|
| 51 |
data member or a member template of a class template is defined outside
|
| 52 |
of the class template definition, the member definition is defined as a
|
| 53 |
template definition in which the *template-parameter*s are those of the
|
| 54 |
class template. The names of the template parameters used in the
|
|
@@ -57,10 +67,12 @@ names used in the class template definition. The template argument list
|
|
| 57 |
following the class template name in the member definition shall name
|
| 58 |
the parameters in the same order as the one used in the template
|
| 59 |
parameter list of the member. Each template parameter pack shall be
|
| 60 |
expanded with an ellipsis in the template argument list.
|
| 61 |
|
|
|
|
|
|
|
| 62 |
``` cpp
|
| 63 |
template<class T1, class T2> struct A {
|
| 64 |
void f1();
|
| 65 |
void f2();
|
| 66 |
};
|
|
@@ -77,20 +89,24 @@ template<class ... Types> struct B {
|
|
| 77 |
|
| 78 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 79 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 80 |
```
|
| 81 |
|
|
|
|
|
|
|
| 82 |
In a redeclaration, partial specialization, explicit specialization or
|
| 83 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 84 |
in kind with the original class template declaration (
|
| 85 |
[[dcl.type.elab]]).
|
| 86 |
|
| 87 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 88 |
|
| 89 |
A member function of a class template may be defined outside of the
|
| 90 |
class template definition in which it is declared.
|
| 91 |
|
|
|
|
|
|
|
| 92 |
``` cpp
|
| 93 |
template<class T> class Array {
|
| 94 |
T* v;
|
| 95 |
int sz;
|
| 96 |
public:
|
|
@@ -108,46 +124,60 @@ template<class T> T& Array<T>::operator[](int i) {
|
|
| 108 |
if (i<0 || sz<=i) error("Array: range error");
|
| 109 |
return v[i];
|
| 110 |
}
|
| 111 |
```
|
| 112 |
|
|
|
|
|
|
|
| 113 |
The *template-argument*s for a member function of a class template are
|
| 114 |
determined by the *template-argument*s of the type of the object for
|
| 115 |
-
which the member function is called.
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
``` cpp
|
| 120 |
Array<int> v1(20);
|
| 121 |
Array<dcomplex> v2(30);
|
| 122 |
|
| 123 |
v1[3] = 7; // Array<int>::operator[]()
|
| 124 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 125 |
```
|
| 126 |
|
|
|
|
|
|
|
| 127 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 128 |
|
| 129 |
A member class of a class template may be defined outside the class
|
| 130 |
-
template definition in which it is declared.
|
| 131 |
-
|
| 132 |
-
[
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
``` cpp
|
| 135 |
template<class T> struct A {
|
| 136 |
class B;
|
| 137 |
};
|
| 138 |
A<int>::B* b1; // OK: requires A to be defined but not A::B
|
| 139 |
template<class T> class A<T>::B { };
|
| 140 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 141 |
```
|
| 142 |
|
|
|
|
|
|
|
| 143 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 144 |
|
| 145 |
A definition for a static data member or static data member template may
|
| 146 |
be provided in a namespace scope enclosing the definition of the static
|
| 147 |
member’s class template.
|
| 148 |
|
|
|
|
|
|
|
| 149 |
``` cpp
|
| 150 |
template<class T> class X {
|
| 151 |
static T s;
|
| 152 |
};
|
| 153 |
template<class T> T X<T>::s = 0;
|
|
@@ -159,64 +189,80 @@ struct limits {
|
|
| 159 |
|
| 160 |
template<class T>
|
| 161 |
const T limits::min = { }; // definition
|
| 162 |
```
|
| 163 |
|
|
|
|
|
|
|
| 164 |
An explicit specialization of a static data member declared as an array
|
| 165 |
of unknown bound can have a different bound from its definition, if any.
|
| 166 |
|
|
|
|
|
|
|
| 167 |
``` cpp
|
| 168 |
template <class T> struct A {
|
| 169 |
static int i[];
|
| 170 |
};
|
| 171 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 172 |
template <> int A<int>::i[] = { 1 }; // OK: 1 element
|
| 173 |
```
|
| 174 |
|
|
|
|
|
|
|
| 175 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
| 176 |
|
| 177 |
An enumeration member of a class template may be defined outside the
|
| 178 |
class template definition.
|
| 179 |
|
|
|
|
|
|
|
| 180 |
``` cpp
|
| 181 |
template<class T> struct A {
|
| 182 |
enum E : T;
|
| 183 |
};
|
| 184 |
A<int> a;
|
| 185 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 186 |
A<int>::E e = A<int>::e1;
|
| 187 |
```
|
| 188 |
|
|
|
|
|
|
|
| 189 |
### Member templates <a id="temp.mem">[[temp.mem]]</a>
|
| 190 |
|
| 191 |
A template can be declared within a class or class template; such a
|
| 192 |
template is called a member template. A member template can be defined
|
| 193 |
within or outside its class definition or class template definition. A
|
| 194 |
member template of a class template that is defined outside of its class
|
| 195 |
template definition shall be specified with the *template-parameter*s of
|
| 196 |
the class template followed by the *template-parameter*s of the member
|
| 197 |
template.
|
| 198 |
|
|
|
|
|
|
|
| 199 |
``` cpp
|
| 200 |
template<class T> struct string {
|
| 201 |
template<class T2> int compare(const T2&);
|
| 202 |
-
template<class T2> string(const string<T2>& s) {
|
| 203 |
};
|
| 204 |
|
| 205 |
template<class T> template<class T2> int string<T>::compare(const T2& s) {
|
| 206 |
}
|
| 207 |
```
|
| 208 |
|
|
|
|
|
|
|
| 209 |
A local class of non-closure type shall not have member templates.
|
| 210 |
Access control rules (Clause [[class.access]]) apply to member template
|
| 211 |
names. A destructor shall not be a member template. A non-template
|
| 212 |
member function ([[dcl.fct]]) with a given name and type and a member
|
| 213 |
function template of the same name, which could be used to generate a
|
| 214 |
specialization of the same type, can both be declared in a class. When
|
| 215 |
both exist, a use of that name and type refers to the non-template
|
| 216 |
member unless an explicit template argument list is supplied.
|
| 217 |
|
|
|
|
|
|
|
| 218 |
``` cpp
|
| 219 |
template <class T> struct A {
|
| 220 |
void f(int);
|
| 221 |
template <class T2> void f(T2);
|
| 222 |
};
|
|
@@ -230,38 +276,49 @@ int main() {
|
|
| 230 |
ac.f('c'); // template
|
| 231 |
ac.f<>(1); // template
|
| 232 |
}
|
| 233 |
```
|
| 234 |
|
|
|
|
|
|
|
| 235 |
A member function template shall not be virtual.
|
| 236 |
|
|
|
|
|
|
|
| 237 |
``` cpp
|
| 238 |
template <class T> struct AA {
|
| 239 |
template <class C> virtual void g(C); // error
|
| 240 |
virtual void f(); // OK
|
| 241 |
};
|
| 242 |
```
|
| 243 |
|
|
|
|
|
|
|
| 244 |
A specialization of a member function template does not override a
|
| 245 |
virtual function from a base class.
|
| 246 |
|
|
|
|
|
|
|
| 247 |
``` cpp
|
| 248 |
class B {
|
| 249 |
virtual void f(int);
|
| 250 |
};
|
| 251 |
|
| 252 |
class D : public B {
|
| 253 |
template <class T> void f(T); // does not override B::f(int)
|
| 254 |
-
void f(int i) { f<>(i); } // overriding function that calls
|
| 255 |
-
// the template instantiation
|
| 256 |
};
|
| 257 |
```
|
| 258 |
|
|
|
|
|
|
|
| 259 |
A specialization of a conversion function template is referenced in the
|
| 260 |
same way as a non-template conversion function that converts to the same
|
| 261 |
type.
|
| 262 |
|
|
|
|
|
|
|
| 263 |
``` cpp
|
| 264 |
struct A {
|
| 265 |
template <class T> operator T*();
|
| 266 |
};
|
| 267 |
template <class T> A::operator T*(){ return 0; }
|
|
@@ -269,20 +326,21 @@ template <> A::operator char*(){ return 0; } // specialization
|
|
| 269 |
template A::operator void*(); // explicit instantiation
|
| 270 |
|
| 271 |
int main() {
|
| 272 |
A a;
|
| 273 |
int* ip;
|
| 274 |
-
ip = a.operator int*(); // explicit call to template operator
|
| 275 |
-
// A::operator int*()
|
| 276 |
}
|
| 277 |
```
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
| 282 |
function name, there is no way to provide an explicit template argument
|
| 283 |
-
list for these function templates.
|
| 284 |
|
| 285 |
A specialization of a conversion function template is not found by name
|
| 286 |
lookup. Instead, any conversion function templates visible in the
|
| 287 |
context of the use are considered. For each such operator, if argument
|
| 288 |
deduction succeeds ([[temp.deduct.conv]]), the resulting specialization
|
|
@@ -299,30 +357,38 @@ non-template conversion functions.
|
|
| 299 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 300 |
|
| 301 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 302 |
more template arguments.
|
| 303 |
|
|
|
|
|
|
|
| 304 |
``` cpp
|
| 305 |
template<class ... Types> struct Tuple { };
|
| 306 |
|
| 307 |
Tuple<> t0; // Types contains no arguments
|
| 308 |
Tuple<int> t1; // Types contains one argument: int
|
| 309 |
Tuple<int, float> t2; // Types contains two arguments: int and float
|
| 310 |
Tuple<0> error; // error: 0 is not a type
|
| 311 |
```
|
| 312 |
|
|
|
|
|
|
|
| 313 |
A *function parameter pack* is a function parameter that accepts zero or
|
| 314 |
more function arguments.
|
| 315 |
|
|
|
|
|
|
|
| 316 |
``` cpp
|
| 317 |
template<class ... Types> void f(Types ... args);
|
| 318 |
|
| 319 |
f(); // OK: args contains no arguments
|
| 320 |
f(1); // OK: args contains one argument: int
|
| 321 |
f(2, 1.0); // OK: args contains two arguments: int and double
|
| 322 |
```
|
| 323 |
|
|
|
|
|
|
|
| 324 |
A *parameter pack* is either a template parameter pack or a function
|
| 325 |
parameter pack.
|
| 326 |
|
| 327 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 328 |
instantiation of which produces zero or more instantiations of the
|
|
@@ -330,10 +396,12 @@ pattern in a list (described below). The form of the pattern depends on
|
|
| 330 |
the context in which the expansion occurs. Pack expansions can occur in
|
| 331 |
the following contexts:
|
| 332 |
|
| 333 |
- In a function parameter pack ([[dcl.fct]]); the pattern is the
|
| 334 |
*parameter-declaration* without the ellipsis.
|
|
|
|
|
|
|
| 335 |
- In a template parameter pack that is a pack expansion (
|
| 336 |
[[temp.param]]):
|
| 337 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 338 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 339 |
- if the template parameter pack is a *type-parameter* with a
|
|
@@ -346,43 +414,49 @@ the following contexts:
|
|
| 346 |
- In a *mem-initializer-list* ([[class.base.init]]) for a
|
| 347 |
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 348 |
pattern is the *mem-initializer*.
|
| 349 |
- In a *template-argument-list* ([[temp.arg]]); the pattern is a
|
| 350 |
*template-argument*.
|
| 351 |
-
- In a *dynamic-exception-specification* ([[except.spec]]); the pattern
|
| 352 |
-
is a *type-id*.
|
| 353 |
- In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
|
| 354 |
*attribute*.
|
| 355 |
- In an *alignment-specifier* ([[dcl.align]]); the pattern is the
|
| 356 |
*alignment-specifier* without the ellipsis.
|
| 357 |
- In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
|
| 358 |
*capture*.
|
| 359 |
- In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
|
| 360 |
*identifier*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
For the purpose of determining whether a parameter pack satisfies a rule
|
| 363 |
regarding entities other than parameter packs, the parameter pack is
|
| 364 |
considered to be the entity that would result from an instantiation of
|
| 365 |
the pattern in which it appears.
|
| 366 |
|
| 367 |
-
``` cpp
|
| 368 |
-
template<class ... Types> void f(Types ... rest);
|
| 369 |
-
template<class ... Types> void g(Types ... rest) {
|
| 370 |
-
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 371 |
-
}
|
| 372 |
-
```
|
| 373 |
-
|
| 374 |
A parameter pack whose name appears within the pattern of a pack
|
| 375 |
expansion is expanded by that pack expansion. An appearance of the name
|
| 376 |
of a parameter pack is only expanded by the innermost enclosing pack
|
| 377 |
expansion. The pattern of a pack expansion shall name one or more
|
| 378 |
parameter packs that are not expanded by a nested pack expansion; such
|
| 379 |
-
parameter packs are called *unexpanded
|
| 380 |
All of the parameter packs expanded by a pack expansion shall have the
|
| 381 |
same number of arguments specified. An appearance of a name of a
|
| 382 |
parameter pack that is not expanded is ill-formed.
|
| 383 |
|
|
|
|
|
|
|
| 384 |
``` cpp
|
| 385 |
template<typename...> struct Tuple {};
|
| 386 |
template<typename T1, typename T2> struct Pair {};
|
| 387 |
|
| 388 |
template<class ... Args1> struct zip {
|
|
@@ -399,38 +473,46 @@ typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
|
| 399 |
template<class ... Args>
|
| 400 |
void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
|
| 401 |
f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
|
| 402 |
f(5 ...); // error: pattern does not contain any parameter packs
|
| 403 |
f(args); // error: parameter pack ``args'' is not expanded
|
| 404 |
-
f(h(args ...) + args ...);
|
| 405 |
-
|
| 406 |
}
|
| 407 |
```
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
| 415 |
instantiation, is interpreted as follows:
|
| 416 |
|
| 417 |
- if the pack is a template parameter pack, the element is a template
|
| 418 |
parameter ([[temp.param]]) of the corresponding kind (type or
|
| 419 |
non-type) designating the type or value from the template argument;
|
| 420 |
otherwise,
|
| 421 |
- if the pack is a function parameter pack, the element is an
|
| 422 |
*id-expression* designating the function parameter that resulted from
|
| 423 |
the instantiation of the pattern where the pack is declared.
|
| 424 |
|
| 425 |
-
All of the Eᵢ become elements in the enclosing list.
|
| 426 |
-
|
| 427 |
-
*
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
``` cpp
|
| 434 |
template<class... T> struct X : T... { };
|
| 435 |
template<class... T> void f(T... values) {
|
| 436 |
X<T...> x(values...);
|
|
@@ -438,14 +520,60 @@ template<class... T> void f(T... values) {
|
|
| 438 |
|
| 439 |
template void f<>(); // OK: X<> has no base classes
|
| 440 |
// x is a variable of type X<> that is value-initialized
|
| 441 |
```
|
| 442 |
|
|
|
|
|
|
|
| 443 |
The instantiation of a `sizeof...` expression ([[expr.sizeof]])
|
| 444 |
produces an integral constant containing the number of elements in the
|
| 445 |
parameter pack it expands.
|
| 446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 448 |
|
| 449 |
A friend of a class or class template can be a function template or
|
| 450 |
class template, a specialization of a function template or class
|
| 451 |
template, or a non-template function or class. For a friend function
|
|
@@ -462,10 +590,12 @@ declaration that is not a template declaration:
|
|
| 462 |
declaration refers to the deduced specialization of that function
|
| 463 |
template ([[temp.deduct.decl]]), otherwise,
|
| 464 |
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 465 |
non-template function.
|
| 466 |
|
|
|
|
|
|
|
| 467 |
``` cpp
|
| 468 |
template<class T> class task;
|
| 469 |
template<class T> task<T>* preempt(task<T>*);
|
| 470 |
|
| 471 |
template<class T> class task {
|
|
@@ -490,54 +620,61 @@ function template `preempt` as a friend; and each specialization of the
|
|
| 490 |
`task` class template has all specializations of the function template
|
| 491 |
`func` as friends. Similarly, each specialization of the `task` class
|
| 492 |
template has the class template specialization `task<int>` as a friend,
|
| 493 |
and has all specializations of the class template `frd` as friends.
|
| 494 |
|
|
|
|
|
|
|
| 495 |
A friend template may be declared within a class or class template. A
|
| 496 |
friend function template may be defined within a class or class
|
| 497 |
template, but a friend class template may not be defined in a class or
|
| 498 |
class template. In these cases, all specializations of the friend class
|
| 499 |
or friend function template are friends of the class or class template
|
| 500 |
granting friendship.
|
| 501 |
|
|
|
|
|
|
|
| 502 |
``` cpp
|
| 503 |
class A {
|
| 504 |
template<class T> friend class B; // OK
|
| 505 |
-
template<class T> friend void f(T){
|
| 506 |
};
|
| 507 |
```
|
| 508 |
|
|
|
|
|
|
|
| 509 |
A template friend declaration specifies that all specializations of that
|
| 510 |
template, whether they are implicitly instantiated ([[temp.inst]]),
|
| 511 |
partially specialized ([[temp.class.spec]]) or explicitly specialized (
|
| 512 |
[[temp.expl.spec]]), are friends of the class containing the template
|
| 513 |
friend declaration.
|
| 514 |
|
|
|
|
|
|
|
| 515 |
``` cpp
|
| 516 |
class X {
|
| 517 |
template<class T> friend struct A;
|
| 518 |
class Y { };
|
| 519 |
};
|
| 520 |
|
| 521 |
template<class T> struct A { X::Y ab; }; // OK
|
| 522 |
template<class T> struct A<T*> { X::Y ab; }; // OK
|
| 523 |
```
|
| 524 |
|
| 525 |
-
|
| 526 |
-
template, the function is instantiated when the function is odr-used (
|
| 527 |
-
[[basic.def.odr]]). The same restrictions on multiple declarations and
|
| 528 |
-
definitions that apply to non-template function declarations and
|
| 529 |
-
definitions also apply to these implicit definitions.
|
| 530 |
|
| 531 |
A member of a class template may be declared to be a friend of a
|
| 532 |
non-template class. In this case, the corresponding member of every
|
| 533 |
-
specialization of the class template
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
the
|
| 538 |
-
|
|
|
|
|
|
|
|
|
|
| 539 |
|
| 540 |
``` cpp
|
| 541 |
template<class T> struct A {
|
| 542 |
struct B { };
|
| 543 |
void f();
|
|
@@ -561,31 +698,37 @@ class C {
|
|
| 561 |
template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
|
| 562 |
// because A<int>::D is not a specialization of A<T>::D
|
| 563 |
};
|
| 564 |
```
|
| 565 |
|
| 566 |
-
|
| 567 |
-
|
|
|
|
|
|
|
| 568 |
|
| 569 |
A friend template shall not be declared in a local class.
|
| 570 |
|
| 571 |
Friend declarations shall not declare partial specializations.
|
| 572 |
|
|
|
|
|
|
|
| 573 |
``` cpp
|
| 574 |
template<class T> class A { };
|
| 575 |
class X {
|
| 576 |
template<class T> friend class A<T*>; // error
|
| 577 |
};
|
| 578 |
```
|
| 579 |
|
|
|
|
|
|
|
| 580 |
When a friend declaration refers to a specialization of a function
|
| 581 |
template, the function parameter declarations shall not include default
|
| 582 |
arguments, nor shall the inline specifier be used in such a declaration.
|
| 583 |
|
| 584 |
### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
|
| 585 |
|
| 586 |
-
A *primary
|
| 587 |
template name is an identifier. A template declaration in which the
|
| 588 |
class template name is a *simple-template-id* is a *partial
|
| 589 |
specialization* of the class template named in the *simple-template-id*.
|
| 590 |
A partial specialization of a class template provides an alternative
|
| 591 |
definition of the template that is used instead of the primary
|
|
@@ -600,62 +743,81 @@ required.
|
|
| 600 |
|
| 601 |
Each class template partial specialization is a distinct template and
|
| 602 |
definitions shall be provided for the members of a template partial
|
| 603 |
specialization ([[temp.class.spec.mfunc]]).
|
| 604 |
|
|
|
|
|
|
|
| 605 |
``` cpp
|
| 606 |
-
template<class T1, class T2, int I> class A { };
|
| 607 |
-
template<class T, int I> class A<T, T*, I> { };
|
| 608 |
-
template<class T1, class T2, int I> class A<T1*, T2, I> { };
|
| 609 |
-
template<class T> class A<int, T*, 5> { };
|
| 610 |
-
template<class T1, class T2, int I> class A<T1, T2*, I> { };
|
| 611 |
```
|
| 612 |
|
| 613 |
The first declaration declares the primary (unspecialized) class
|
| 614 |
template. The second and subsequent declarations declare partial
|
| 615 |
specializations of the primary template.
|
| 616 |
|
|
|
|
|
|
|
| 617 |
The template parameters are specified in the angle bracket enclosed list
|
| 618 |
that immediately follows the keyword `template`. For partial
|
| 619 |
specializations, the template argument list is explicitly written
|
| 620 |
immediately following the class template name. For primary templates,
|
| 621 |
this list is implicitly described by the template parameter list.
|
| 622 |
Specifically, the order of the template arguments is the sequence in
|
| 623 |
-
which they appear in the template parameter list.
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
The template argument list shall not be specified in the primary
|
| 626 |
template declaration. For example,
|
| 627 |
|
| 628 |
``` cpp
|
| 629 |
-
template<class T1, class T2, int I>
|
|
|
|
| 630 |
```
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
``` cpp
|
| 637 |
template<class T> struct A {
|
| 638 |
struct C {
|
| 639 |
template<class T2> struct B { };
|
|
|
|
| 640 |
};
|
| 641 |
};
|
| 642 |
|
| 643 |
// partial specialization of A<T>::C::B<T2>
|
| 644 |
template<class T> template<class T2>
|
| 645 |
-
struct A<T>::C::B<T2*> { };
|
| 646 |
|
| 647 |
-
A<short>::C::B<int*> absip;
|
| 648 |
```
|
| 649 |
|
|
|
|
|
|
|
| 650 |
Partial specialization declarations themselves are not found by name
|
| 651 |
lookup. Rather, when the primary template name is used, any
|
| 652 |
previously-declared partial specializations of the primary template are
|
| 653 |
also considered. One consequence is that a *using-declaration* which
|
| 654 |
refers to a class template does not restrict the set of partial
|
| 655 |
specializations which may be found through the *using-declaration*.
|
| 656 |
|
|
|
|
|
|
|
| 657 |
``` cpp
|
| 658 |
namespace N {
|
| 659 |
template<class T1, class T2> class A { }; // primary template
|
| 660 |
}
|
| 661 |
|
|
@@ -663,43 +825,36 @@ using N::A; // refers to the primary template
|
|
| 663 |
|
| 664 |
namespace N {
|
| 665 |
template<class T> class A<T, T*> { }; // partial specialization
|
| 666 |
}
|
| 667 |
|
| 668 |
-
A<int,int*> a;
|
| 669 |
-
|
| 670 |
```
|
| 671 |
|
|
|
|
|
|
|
| 672 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 673 |
parameter. All other non-type arguments are specialized.
|
| 674 |
|
| 675 |
Within the argument list of a class template partial specialization, the
|
| 676 |
following restrictions apply:
|
| 677 |
|
| 678 |
-
- A partially specialized non-type argument expression shall not involve
|
| 679 |
-
a template parameter of the partial specialization except when the
|
| 680 |
-
argument expression is a simple *identifier*.
|
| 681 |
-
``` cpp
|
| 682 |
-
template <int I, int J> struct A {};
|
| 683 |
-
template <int I> struct A<I+5, I*2> {}; // error
|
| 684 |
-
|
| 685 |
-
template <int I, int J> struct B {};
|
| 686 |
-
template <int I> struct B<I, I> {}; // OK
|
| 687 |
-
```
|
| 688 |
- The type of a template parameter corresponding to a specialized
|
| 689 |
non-type argument shall not be dependent on a parameter of the
|
| 690 |
specialization.
|
|
|
|
| 691 |
``` cpp
|
| 692 |
template <class T, T t> struct C {};
|
| 693 |
template <class T> struct C<T, 1>; // error
|
| 694 |
|
| 695 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 696 |
int array[5];
|
| 697 |
template< int X > class A<X,&array> { }; // error
|
| 698 |
```
|
| 699 |
-
|
| 700 |
-
|
| 701 |
- The specialization shall be more specialized than the primary
|
| 702 |
template ([[temp.class.order]]).
|
| 703 |
- The template parameter list of a specialization shall not contain
|
| 704 |
default template argument values.[^4]
|
| 705 |
- An argument shall not contain an unexpanded parameter pack. If an
|
|
@@ -728,57 +883,93 @@ argument lists of the partial specializations.
|
|
| 728 |
|
| 729 |
A partial specialization matches a given actual template argument list
|
| 730 |
if the template arguments of the partial specialization can be deduced
|
| 731 |
from the actual template argument list ([[temp.deduct]]).
|
| 732 |
|
|
|
|
|
|
|
| 733 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
A<int, int, 1> a1; // uses #1
|
| 735 |
A<int, int*, 1> a2; // uses #2, T is int, I is 1
|
| 736 |
A<int, char*, 5> a3; // uses #4, T is char
|
| 737 |
A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
|
| 738 |
A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
|
| 739 |
```
|
| 740 |
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
|
| 745 |
In a type name that refers to a class template specialization, (e.g.,
|
| 746 |
`A<int, int, 1>`) the argument list shall match the template parameter
|
| 747 |
list of the primary template. The template arguments of a specialization
|
| 748 |
are deduced from the arguments of the primary template.
|
| 749 |
|
| 750 |
#### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
|
| 751 |
|
| 752 |
-
For two class template partial specializations, the first is
|
| 753 |
-
specialized
|
| 754 |
-
function templates, the first function template is
|
| 755 |
-
|
| 756 |
-
|
| 757 |
|
| 758 |
-
- the
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
the
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
|
|
|
| 766 |
|
| 767 |
``` cpp
|
| 768 |
template<int I, int J, class T> class X { };
|
| 769 |
template<int I, int J> class X<I, J, int> { }; // #1
|
| 770 |
template<int I> class X<I, I, int> { }; // #2
|
| 771 |
|
| 772 |
-
template<int
|
| 773 |
-
template<int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
```
|
| 775 |
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
|
| 781 |
#### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
|
| 782 |
|
| 783 |
The template parameter list of a member of a class template partial
|
| 784 |
specialization shall match the template parameter list of the class
|
|
@@ -793,16 +984,19 @@ definitions of members of the primary template are never used as
|
|
| 793 |
definitions for members of a class template partial specialization. An
|
| 794 |
explicit specialization of a member of a class template partial
|
| 795 |
specialization is declared in the same way as an explicit specialization
|
| 796 |
of the primary template.
|
| 797 |
|
|
|
|
|
|
|
| 798 |
``` cpp
|
| 799 |
-
// primary template
|
| 800 |
template<class T, int I> struct A {
|
| 801 |
void f();
|
| 802 |
};
|
| 803 |
|
|
|
|
| 804 |
template<class T, int I> void A<T,I>::f() { }
|
| 805 |
|
| 806 |
// class template partial specialization
|
| 807 |
template<class T> struct A<T,2> {
|
| 808 |
void f();
|
|
@@ -818,19 +1012,18 @@ template<> void A<char,2>::h() { }
|
|
| 818 |
|
| 819 |
int main() {
|
| 820 |
A<char,0> a0;
|
| 821 |
A<char,2> a2;
|
| 822 |
a0.f(); // OK, uses definition of primary template's member
|
| 823 |
-
a2.g();
|
| 824 |
-
|
| 825 |
-
a2.
|
| 826 |
-
// explicit specialization's member
|
| 827 |
-
a2.f(); // ill-formed, no definition of f for A<T,2>
|
| 828 |
-
// the primary template is not used here
|
| 829 |
}
|
| 830 |
```
|
| 831 |
|
|
|
|
|
|
|
| 832 |
If a member template of a class template is partially specialized, the
|
| 833 |
member template partial specializations are member templates of the
|
| 834 |
enclosing class template; if the enclosing class template is
|
| 835 |
instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
|
| 836 |
every member template partial specialization is also instantiated as
|
|
@@ -842,10 +1035,12 @@ specialization of the enclosing class template. If a partial
|
|
| 842 |
specialization of the member template is explicitly specialized for a
|
| 843 |
given (implicit) specialization of the enclosing class template, the
|
| 844 |
primary member template and its other partial specializations are still
|
| 845 |
considered for this specialization of the enclosing class template.
|
| 846 |
|
|
|
|
|
|
|
| 847 |
``` cpp
|
| 848 |
template<class T> struct A {
|
| 849 |
template<class T2> struct B {}; // #1
|
| 850 |
template<class T2> struct B<T2*> {}; // #2
|
| 851 |
};
|
|
@@ -855,20 +1050,27 @@ template<> template<class T2> struct A<short>::B {}; // #3
|
|
| 855 |
A<char>::B<int*> abcip; // uses #2
|
| 856 |
A<short>::B<int*> absip; // uses #3
|
| 857 |
A<char>::B<int> abci; // uses #1
|
| 858 |
```
|
| 859 |
|
|
|
|
|
|
|
| 860 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 861 |
|
| 862 |
-
A function template defines an unbounded set of related functions.
|
| 863 |
-
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
``` cpp
|
| 866 |
template<class T> class Array { };
|
| 867 |
template<class T> void sort(Array<T>&);
|
| 868 |
```
|
| 869 |
|
|
|
|
|
|
|
| 870 |
A function template can be overloaded with other function templates and
|
| 871 |
with non-template functions ([[dcl.fct]]). A non-template function is
|
| 872 |
not related to a function template (i.e., it is never considered to be a
|
| 873 |
specialization), even if it has the same name and type as a potentially
|
| 874 |
generated function template specialization.[^5]
|
|
@@ -876,76 +1078,92 @@ generated function template specialization.[^5]
|
|
| 876 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 877 |
|
| 878 |
It is possible to overload function templates so that two different
|
| 879 |
function template specializations have the same type.
|
| 880 |
|
|
|
|
|
|
|
| 881 |
``` cpp
|
| 882 |
-
//
|
| 883 |
template<class T>
|
| 884 |
void f(T*);
|
| 885 |
void g(int* p) {
|
| 886 |
f(p); // calls f<int>(int*)
|
| 887 |
}
|
| 888 |
```
|
| 889 |
|
| 890 |
``` cpp
|
| 891 |
-
//
|
| 892 |
template<class T>
|
| 893 |
void f(T);
|
| 894 |
void h(int* p) {
|
| 895 |
f(p); // calls f<int*>(int*)
|
| 896 |
}
|
| 897 |
```
|
| 898 |
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 908 |
|
| 909 |
``` cpp
|
| 910 |
template<class T> void f();
|
| 911 |
template<int I> void f(); // OK: overloads the first template
|
| 912 |
// distinguishable with an explicit template argument list
|
| 913 |
```
|
| 914 |
|
|
|
|
|
|
|
| 915 |
When an expression that references a template parameter is used in the
|
| 916 |
function parameter list or the return type in the declaration of a
|
| 917 |
function template, the expression that references the template parameter
|
| 918 |
is part of the signature of the function template. This is necessary to
|
| 919 |
permit a declaration of a function template in one translation unit to
|
| 920 |
be linked with another declaration of the function template in another
|
| 921 |
translation unit and, conversely, to ensure that function templates that
|
| 922 |
are intended to be distinct are not linked with one another.
|
| 923 |
|
|
|
|
|
|
|
| 924 |
``` cpp
|
| 925 |
template <int I, int J> A<I+J> f(A<I>, A<J>); // #1
|
| 926 |
template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
| 927 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 928 |
```
|
| 929 |
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
|
|
|
|
|
|
| 934 |
|
| 935 |
Two expressions involving template parameters are considered
|
| 936 |
*equivalent* if two function definitions containing the expressions
|
| 937 |
-
would satisfy the one
|
| 938 |
the tokens used to name the template parameters may differ as long as a
|
| 939 |
token used to name a template parameter in one expression is replaced by
|
| 940 |
another token that names the same template parameter in the other
|
| 941 |
expression. For determining whether two dependent names ([[temp.dep]])
|
| 942 |
are equivalent, only the name itself is considered, not the result of
|
| 943 |
name lookup in the context of the template. If multiple declarations of
|
| 944 |
the same function template differ in the result of this name lookup, the
|
| 945 |
result for the first declaration is used.
|
| 946 |
|
|
|
|
|
|
|
| 947 |
``` cpp
|
| 948 |
template <int I, int J> void f(A<I+J>); // #1
|
| 949 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 950 |
|
| 951 |
template <class T> decltype(g(T())) h();
|
|
@@ -954,10 +1172,12 @@ template <class T> decltype(g(T())) h() // redeclaration of h() uses the
|
|
| 954 |
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 955 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 956 |
// was not in scope at the first declaration of h()
|
| 957 |
```
|
| 958 |
|
|
|
|
|
|
|
| 959 |
Two expressions involving template parameters that are not equivalent
|
| 960 |
are *functionally equivalent* if, for any given set of template
|
| 961 |
arguments, the evaluation of the expression results in the same value.
|
| 962 |
|
| 963 |
Two function templates are *equivalent* if they are declared in the same
|
|
@@ -968,11 +1188,13 @@ parameters. Two function templates are *functionally equivalent* if they
|
|
| 968 |
are equivalent except that one or more expressions that involve template
|
| 969 |
parameters in the return types and parameter lists are functionally
|
| 970 |
equivalent using the rules described above to compare expressions
|
| 971 |
involving template parameters. If a program contains declarations of
|
| 972 |
function templates that are functionally equivalent but not equivalent,
|
| 973 |
-
the program is ill-formed
|
|
|
|
|
|
|
| 974 |
|
| 975 |
This rule guarantees that equivalent declarations will be linked with
|
| 976 |
one another, while not requiring implementations to use heroic efforts
|
| 977 |
to guarantee that functionally equivalent declarations will be treated
|
| 978 |
as distinct. For example, the last two declarations are functionally
|
|
@@ -990,10 +1212,12 @@ template <int I> void f(A<I>, A<I+11>);
|
|
| 990 |
// Ill-formed, no diagnostic required
|
| 991 |
template <int I> void f(A<I>, A<I+10>);
|
| 992 |
template <int I> void f(A<I>, A<I+1+2+3+4>);
|
| 993 |
```
|
| 994 |
|
|
|
|
|
|
|
| 995 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 996 |
|
| 997 |
If a function template is overloaded, the use of a function template
|
| 998 |
specialization might be ambiguous because template argument deduction (
|
| 999 |
[[temp.deduct]]) may associate the function template specialization with
|
|
@@ -1021,20 +1245,29 @@ specialized template is the one chosen by the partial ordering process.
|
|
| 1021 |
|
| 1022 |
To produce the transformed template, for each type, non-type, or
|
| 1023 |
template template parameter (including template parameter packs (
|
| 1024 |
[[temp.variadic]]) thereof) synthesize a unique type, value, or class
|
| 1025 |
template respectively and substitute it for each occurrence of that
|
| 1026 |
-
parameter in the function type of the template.
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
of
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1036 |
|
| 1037 |
``` cpp
|
| 1038 |
struct A { };
|
| 1039 |
template<class T> struct B {
|
| 1040 |
template<class R> int operator*(R&); // #1
|
|
@@ -1050,14 +1283,18 @@ int main() {
|
|
| 1050 |
B<A> b;
|
| 1051 |
b * a; // calls #1a
|
| 1052 |
}
|
| 1053 |
```
|
| 1054 |
|
|
|
|
|
|
|
| 1055 |
Using the transformed function template’s function type, perform type
|
| 1056 |
deduction against the other template as described in
|
| 1057 |
[[temp.deduct.partial]].
|
| 1058 |
|
|
|
|
|
|
|
| 1059 |
``` cpp
|
| 1060 |
template<class T> struct A { A(); };
|
| 1061 |
|
| 1062 |
template<class T> void f(T);
|
| 1063 |
template<class T> void f(T*);
|
|
@@ -1071,23 +1308,29 @@ template<class T> void h(A<T>&);
|
|
| 1071 |
|
| 1072 |
void m() {
|
| 1073 |
const int* p;
|
| 1074 |
f(p); // f(const T*) is more specialized than f(T) or f(T*)
|
| 1075 |
float x;
|
| 1076 |
-
g(x); //
|
| 1077 |
A<int> z;
|
| 1078 |
h(z); // overload resolution selects h(A<T>&)
|
| 1079 |
const A<int> z2;
|
| 1080 |
h(z2); // h(const T&) is called because h(A<T>&) is not callable
|
| 1081 |
}
|
| 1082 |
```
|
| 1083 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1084 |
Since partial ordering in a call context considers only parameters for
|
| 1085 |
which there are explicit call arguments, some parameters are ignored
|
| 1086 |
(namely, function parameter packs, parameters with default arguments,
|
| 1087 |
and ellipsis parameters).
|
| 1088 |
|
|
|
|
|
|
|
| 1089 |
``` cpp
|
| 1090 |
template<class T> void f(T); // #1
|
| 1091 |
template<class T> void f(T*, int=1); // #2
|
| 1092 |
template<class T> void g(T); // #3
|
| 1093 |
template<class T> void g(T*, ...); // #4
|
|
@@ -1099,10 +1342,14 @@ int main() {
|
|
| 1099 |
f(ip); // calls #2
|
| 1100 |
g(ip); // calls #4
|
| 1101 |
}
|
| 1102 |
```
|
| 1103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1104 |
``` cpp
|
| 1105 |
template<class T, class U> struct A { };
|
| 1106 |
|
| 1107 |
template<class T, class U> void f(U, A<U, T>* p = 0); // #1
|
| 1108 |
template< class U> void f(U, A<U, U>* p = 0); // #2
|
|
@@ -1114,10 +1361,14 @@ void h() {
|
|
| 1114 |
f<int>(42); // error: ambiguous
|
| 1115 |
g(42); // error: ambiguous
|
| 1116 |
}
|
| 1117 |
```
|
| 1118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1119 |
``` cpp
|
| 1120 |
template<class T, class... U> void f(T, U...); // #1
|
| 1121 |
template<class T > void f(T); // #2
|
| 1122 |
template<class T, class... U> void g(T*, U...); // #3
|
| 1123 |
template<class T > void g(T); // #4
|
|
@@ -1126,34 +1377,42 @@ void h(int i) {
|
|
| 1126 |
f(&i); // error: ambiguous
|
| 1127 |
g(&i); // OK: calls #3
|
| 1128 |
}
|
| 1129 |
```
|
| 1130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1131 |
### Alias templates <a id="temp.alias">[[temp.alias]]</a>
|
| 1132 |
|
| 1133 |
A *template-declaration* in which the *declaration* is an
|
| 1134 |
*alias-declaration* (Clause [[dcl.dcl]]) declares the *identifier* to
|
| 1135 |
-
be
|
| 1136 |
types. The name of the alias template is a *template-name*.
|
| 1137 |
|
| 1138 |
When a *template-id* refers to the specialization of an alias template,
|
| 1139 |
it is equivalent to the associated type obtained by substitution of its
|
| 1140 |
*template-argument*s for the *template-parameter*s in the *type-id* of
|
| 1141 |
-
the alias template.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1142 |
|
| 1143 |
``` cpp
|
| 1144 |
-
template<class T> struct Alloc {
|
| 1145 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 1146 |
Vec<int> v; // same as vector<int, Alloc<int>{> v;}
|
| 1147 |
|
| 1148 |
template<class T>
|
| 1149 |
void process(Vec<T>& v)
|
| 1150 |
-
{
|
| 1151 |
|
| 1152 |
template<class T>
|
| 1153 |
void process(vector<T, Alloc<T>>& w)
|
| 1154 |
-
{
|
| 1155 |
|
| 1156 |
template<template<class> class TT>
|
| 1157 |
void f(TT<int>);
|
| 1158 |
|
| 1159 |
f(v); // error: Vec not deduced
|
|
@@ -1161,19 +1420,38 @@ f(v); // error: Vec not deduced
|
|
| 1161 |
template<template<class,class> class TT>
|
| 1162 |
void g(TT<int, Alloc<int>>);
|
| 1163 |
g(v); // OK: TT = vector
|
| 1164 |
```
|
| 1165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1166 |
The *type-id* in an alias template declaration shall not refer to the
|
| 1167 |
alias template being declared. The type produced by an alias template
|
| 1168 |
specialization shall not directly or indirectly make use of that
|
| 1169 |
specialization.
|
| 1170 |
|
|
|
|
|
|
|
| 1171 |
``` cpp
|
| 1172 |
template <class T> struct A;
|
| 1173 |
template <class T> using B = typename A<T>::U;
|
| 1174 |
template <class T> struct A {
|
| 1175 |
typedef B<T> U;
|
| 1176 |
};
|
| 1177 |
B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
|
| 1178 |
```
|
| 1179 |
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
A *template-id*, that is, the *template-name* followed by a
|
| 4 |
*template-argument-list* shall not be specified in the declaration of a
|
| 5 |
primary template declaration.
|
| 6 |
|
| 7 |
+
[*Example 1*:
|
| 8 |
+
|
| 9 |
``` cpp
|
| 10 |
template<class T1, class T2, int I> class A<T1, T2, I> { }; // error
|
| 11 |
template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
|
| 12 |
```
|
| 13 |
|
| 14 |
+
— *end example*]
|
| 15 |
+
|
| 16 |
+
[*Note 1*: However, this syntax is allowed in class template partial
|
| 17 |
+
specializations ([[temp.class.spec]]). — *end note*]
|
| 18 |
|
| 19 |
For purposes of name lookup and instantiation, default arguments and
|
| 20 |
+
*noexcept-specifier*s of function templates and default arguments and
|
| 21 |
+
*noexcept-specifier*s of member functions of class templates are
|
| 22 |
+
considered definitions; each default argument or *noexcept-specifier* is
|
| 23 |
+
a separate definition which is unrelated to the function template
|
| 24 |
+
definition or to any other default arguments or *noexcept-specifier*s.
|
| 25 |
+
For the purpose of instantiation, the substatements of a constexpr if
|
| 26 |
+
statement ([[stmt.if]]) are considered definitions.
|
| 27 |
|
| 28 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 29 |
possible to partially or explicitly specialize an alias template.
|
| 30 |
|
| 31 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 32 |
|
| 33 |
+
A *class template* defines the layout and operations for an unbounded
|
| 34 |
+
set of related types.
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
[*Example 1*:
|
| 37 |
+
|
| 38 |
+
A single class template `List` might provide an unbounded set of class
|
| 39 |
+
definitions: one class `List<T>` for every type `T`, each describing a
|
| 40 |
+
linked list of elements of type `T`. Similarly, a class template `Array`
|
| 41 |
+
describing a contiguous, dynamic array might be defined like this:
|
| 42 |
|
| 43 |
``` cpp
|
| 44 |
template<class T> class Array {
|
| 45 |
T* v;
|
| 46 |
int sz;
|
|
|
|
| 49 |
T& operator[](int);
|
| 50 |
T& elem(int i) { return v[i]; }
|
| 51 |
};
|
| 52 |
```
|
| 53 |
|
| 54 |
+
The prefix `template<class T>` specifies that a template is being
|
| 55 |
+
declared and that a *type-name* `T` may be used in the declaration. In
|
| 56 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 57 |
|
| 58 |
+
— *end example*]
|
| 59 |
+
|
| 60 |
When a member function, a member class, a member enumeration, a static
|
| 61 |
data member or a member template of a class template is defined outside
|
| 62 |
of the class template definition, the member definition is defined as a
|
| 63 |
template definition in which the *template-parameter*s are those of the
|
| 64 |
class template. The names of the template parameters used in the
|
|
|
|
| 67 |
following the class template name in the member definition shall name
|
| 68 |
the parameters in the same order as the one used in the template
|
| 69 |
parameter list of the member. Each template parameter pack shall be
|
| 70 |
expanded with an ellipsis in the template argument list.
|
| 71 |
|
| 72 |
+
[*Example 2*:
|
| 73 |
+
|
| 74 |
``` cpp
|
| 75 |
template<class T1, class T2> struct A {
|
| 76 |
void f1();
|
| 77 |
void f2();
|
| 78 |
};
|
|
|
|
| 89 |
|
| 90 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 91 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 92 |
```
|
| 93 |
|
| 94 |
+
— *end example*]
|
| 95 |
+
|
| 96 |
In a redeclaration, partial specialization, explicit specialization or
|
| 97 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 98 |
in kind with the original class template declaration (
|
| 99 |
[[dcl.type.elab]]).
|
| 100 |
|
| 101 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 102 |
|
| 103 |
A member function of a class template may be defined outside of the
|
| 104 |
class template definition in which it is declared.
|
| 105 |
|
| 106 |
+
[*Example 1*:
|
| 107 |
+
|
| 108 |
``` cpp
|
| 109 |
template<class T> class Array {
|
| 110 |
T* v;
|
| 111 |
int sz;
|
| 112 |
public:
|
|
|
|
| 124 |
if (i<0 || sz<=i) error("Array: range error");
|
| 125 |
return v[i];
|
| 126 |
}
|
| 127 |
```
|
| 128 |
|
| 129 |
+
— *end example*]
|
| 130 |
+
|
| 131 |
The *template-argument*s for a member function of a class template are
|
| 132 |
determined by the *template-argument*s of the type of the object for
|
| 133 |
+
which the member function is called.
|
| 134 |
+
|
| 135 |
+
[*Example 2*:
|
| 136 |
+
|
| 137 |
+
The *template-argument* for `Array<T>::operator[]()` will be determined
|
| 138 |
+
by the `Array` to which the subscripting operation is applied.
|
| 139 |
|
| 140 |
``` cpp
|
| 141 |
Array<int> v1(20);
|
| 142 |
Array<dcomplex> v2(30);
|
| 143 |
|
| 144 |
v1[3] = 7; // Array<int>::operator[]()
|
| 145 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 146 |
```
|
| 147 |
|
| 148 |
+
— *end example*]
|
| 149 |
+
|
| 150 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 151 |
|
| 152 |
A member class of a class template may be defined outside the class
|
| 153 |
+
template definition in which it is declared.
|
| 154 |
+
|
| 155 |
+
[*Note 1*:
|
| 156 |
+
|
| 157 |
+
The member class must be defined before its first use that requires an
|
| 158 |
+
instantiation ([[temp.inst]]). For example,
|
| 159 |
|
| 160 |
``` cpp
|
| 161 |
template<class T> struct A {
|
| 162 |
class B;
|
| 163 |
};
|
| 164 |
A<int>::B* b1; // OK: requires A to be defined but not A::B
|
| 165 |
template<class T> class A<T>::B { };
|
| 166 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 167 |
```
|
| 168 |
|
| 169 |
+
— *end note*]
|
| 170 |
+
|
| 171 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 172 |
|
| 173 |
A definition for a static data member or static data member template may
|
| 174 |
be provided in a namespace scope enclosing the definition of the static
|
| 175 |
member’s class template.
|
| 176 |
|
| 177 |
+
[*Example 1*:
|
| 178 |
+
|
| 179 |
``` cpp
|
| 180 |
template<class T> class X {
|
| 181 |
static T s;
|
| 182 |
};
|
| 183 |
template<class T> T X<T>::s = 0;
|
|
|
|
| 189 |
|
| 190 |
template<class T>
|
| 191 |
const T limits::min = { }; // definition
|
| 192 |
```
|
| 193 |
|
| 194 |
+
— *end example*]
|
| 195 |
+
|
| 196 |
An explicit specialization of a static data member declared as an array
|
| 197 |
of unknown bound can have a different bound from its definition, if any.
|
| 198 |
|
| 199 |
+
[*Example 2*:
|
| 200 |
+
|
| 201 |
``` cpp
|
| 202 |
template <class T> struct A {
|
| 203 |
static int i[];
|
| 204 |
};
|
| 205 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 206 |
template <> int A<int>::i[] = { 1 }; // OK: 1 element
|
| 207 |
```
|
| 208 |
|
| 209 |
+
— *end example*]
|
| 210 |
+
|
| 211 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
| 212 |
|
| 213 |
An enumeration member of a class template may be defined outside the
|
| 214 |
class template definition.
|
| 215 |
|
| 216 |
+
[*Example 1*:
|
| 217 |
+
|
| 218 |
``` cpp
|
| 219 |
template<class T> struct A {
|
| 220 |
enum E : T;
|
| 221 |
};
|
| 222 |
A<int> a;
|
| 223 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 224 |
A<int>::E e = A<int>::e1;
|
| 225 |
```
|
| 226 |
|
| 227 |
+
— *end example*]
|
| 228 |
+
|
| 229 |
### Member templates <a id="temp.mem">[[temp.mem]]</a>
|
| 230 |
|
| 231 |
A template can be declared within a class or class template; such a
|
| 232 |
template is called a member template. A member template can be defined
|
| 233 |
within or outside its class definition or class template definition. A
|
| 234 |
member template of a class template that is defined outside of its class
|
| 235 |
template definition shall be specified with the *template-parameter*s of
|
| 236 |
the class template followed by the *template-parameter*s of the member
|
| 237 |
template.
|
| 238 |
|
| 239 |
+
[*Example 1*:
|
| 240 |
+
|
| 241 |
``` cpp
|
| 242 |
template<class T> struct string {
|
| 243 |
template<class T2> int compare(const T2&);
|
| 244 |
+
template<class T2> string(const string<T2>& s) { ... }
|
| 245 |
};
|
| 246 |
|
| 247 |
template<class T> template<class T2> int string<T>::compare(const T2& s) {
|
| 248 |
}
|
| 249 |
```
|
| 250 |
|
| 251 |
+
— *end example*]
|
| 252 |
+
|
| 253 |
A local class of non-closure type shall not have member templates.
|
| 254 |
Access control rules (Clause [[class.access]]) apply to member template
|
| 255 |
names. A destructor shall not be a member template. A non-template
|
| 256 |
member function ([[dcl.fct]]) with a given name and type and a member
|
| 257 |
function template of the same name, which could be used to generate a
|
| 258 |
specialization of the same type, can both be declared in a class. When
|
| 259 |
both exist, a use of that name and type refers to the non-template
|
| 260 |
member unless an explicit template argument list is supplied.
|
| 261 |
|
| 262 |
+
[*Example 2*:
|
| 263 |
+
|
| 264 |
``` cpp
|
| 265 |
template <class T> struct A {
|
| 266 |
void f(int);
|
| 267 |
template <class T2> void f(T2);
|
| 268 |
};
|
|
|
|
| 276 |
ac.f('c'); // template
|
| 277 |
ac.f<>(1); // template
|
| 278 |
}
|
| 279 |
```
|
| 280 |
|
| 281 |
+
— *end example*]
|
| 282 |
+
|
| 283 |
A member function template shall not be virtual.
|
| 284 |
|
| 285 |
+
[*Example 3*:
|
| 286 |
+
|
| 287 |
``` cpp
|
| 288 |
template <class T> struct AA {
|
| 289 |
template <class C> virtual void g(C); // error
|
| 290 |
virtual void f(); // OK
|
| 291 |
};
|
| 292 |
```
|
| 293 |
|
| 294 |
+
— *end example*]
|
| 295 |
+
|
| 296 |
A specialization of a member function template does not override a
|
| 297 |
virtual function from a base class.
|
| 298 |
|
| 299 |
+
[*Example 4*:
|
| 300 |
+
|
| 301 |
``` cpp
|
| 302 |
class B {
|
| 303 |
virtual void f(int);
|
| 304 |
};
|
| 305 |
|
| 306 |
class D : public B {
|
| 307 |
template <class T> void f(T); // does not override B::f(int)
|
| 308 |
+
void f(int i) { f<>(i); } // overriding function that calls the template instantiation
|
|
|
|
| 309 |
};
|
| 310 |
```
|
| 311 |
|
| 312 |
+
— *end example*]
|
| 313 |
+
|
| 314 |
A specialization of a conversion function template is referenced in the
|
| 315 |
same way as a non-template conversion function that converts to the same
|
| 316 |
type.
|
| 317 |
|
| 318 |
+
[*Example 5*:
|
| 319 |
+
|
| 320 |
``` cpp
|
| 321 |
struct A {
|
| 322 |
template <class T> operator T*();
|
| 323 |
};
|
| 324 |
template <class T> A::operator T*(){ return 0; }
|
|
|
|
| 326 |
template A::operator void*(); // explicit instantiation
|
| 327 |
|
| 328 |
int main() {
|
| 329 |
A a;
|
| 330 |
int* ip;
|
| 331 |
+
ip = a.operator int*(); // explicit call to template operator A::operator int*()
|
|
|
|
| 332 |
}
|
| 333 |
```
|
| 334 |
|
| 335 |
+
— *end example*]
|
| 336 |
+
|
| 337 |
+
[*Note 1*: Because the explicit template argument list follows the
|
| 338 |
+
function template name, and because conversion member function templates
|
| 339 |
+
and constructor member function templates are called without using a
|
| 340 |
function name, there is no way to provide an explicit template argument
|
| 341 |
+
list for these function templates. — *end note*]
|
| 342 |
|
| 343 |
A specialization of a conversion function template is not found by name
|
| 344 |
lookup. Instead, any conversion function templates visible in the
|
| 345 |
context of the use are considered. For each such operator, if argument
|
| 346 |
deduction succeeds ([[temp.deduct.conv]]), the resulting specialization
|
|
|
|
| 357 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 358 |
|
| 359 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 360 |
more template arguments.
|
| 361 |
|
| 362 |
+
[*Example 1*:
|
| 363 |
+
|
| 364 |
``` cpp
|
| 365 |
template<class ... Types> struct Tuple { };
|
| 366 |
|
| 367 |
Tuple<> t0; // Types contains no arguments
|
| 368 |
Tuple<int> t1; // Types contains one argument: int
|
| 369 |
Tuple<int, float> t2; // Types contains two arguments: int and float
|
| 370 |
Tuple<0> error; // error: 0 is not a type
|
| 371 |
```
|
| 372 |
|
| 373 |
+
— *end example*]
|
| 374 |
+
|
| 375 |
A *function parameter pack* is a function parameter that accepts zero or
|
| 376 |
more function arguments.
|
| 377 |
|
| 378 |
+
[*Example 2*:
|
| 379 |
+
|
| 380 |
``` cpp
|
| 381 |
template<class ... Types> void f(Types ... args);
|
| 382 |
|
| 383 |
f(); // OK: args contains no arguments
|
| 384 |
f(1); // OK: args contains one argument: int
|
| 385 |
f(2, 1.0); // OK: args contains two arguments: int and double
|
| 386 |
```
|
| 387 |
|
| 388 |
+
— *end example*]
|
| 389 |
+
|
| 390 |
A *parameter pack* is either a template parameter pack or a function
|
| 391 |
parameter pack.
|
| 392 |
|
| 393 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 394 |
instantiation of which produces zero or more instantiations of the
|
|
|
|
| 396 |
the context in which the expansion occurs. Pack expansions can occur in
|
| 397 |
the following contexts:
|
| 398 |
|
| 399 |
- In a function parameter pack ([[dcl.fct]]); the pattern is the
|
| 400 |
*parameter-declaration* without the ellipsis.
|
| 401 |
+
- In a *using-declaration* ([[namespace.udecl]]); the pattern is a
|
| 402 |
+
*using-declarator*.
|
| 403 |
- In a template parameter pack that is a pack expansion (
|
| 404 |
[[temp.param]]):
|
| 405 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 406 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 407 |
- if the template parameter pack is a *type-parameter* with a
|
|
|
|
| 414 |
- In a *mem-initializer-list* ([[class.base.init]]) for a
|
| 415 |
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 416 |
pattern is the *mem-initializer*.
|
| 417 |
- In a *template-argument-list* ([[temp.arg]]); the pattern is a
|
| 418 |
*template-argument*.
|
|
|
|
|
|
|
| 419 |
- In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
|
| 420 |
*attribute*.
|
| 421 |
- In an *alignment-specifier* ([[dcl.align]]); the pattern is the
|
| 422 |
*alignment-specifier* without the ellipsis.
|
| 423 |
- In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
|
| 424 |
*capture*.
|
| 425 |
- In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
|
| 426 |
*identifier*.
|
| 427 |
+
- In a *fold-expression* ([[expr.prim.fold]]); the pattern is the
|
| 428 |
+
*cast-expression* that contains an unexpanded parameter pack.
|
| 429 |
+
|
| 430 |
+
[*Example 3*:
|
| 431 |
+
|
| 432 |
+
``` cpp
|
| 433 |
+
template<class ... Types> void f(Types ... rest);
|
| 434 |
+
template<class ... Types> void g(Types ... rest) {
|
| 435 |
+
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 436 |
+
}
|
| 437 |
+
```
|
| 438 |
+
|
| 439 |
+
— *end example*]
|
| 440 |
|
| 441 |
For the purpose of determining whether a parameter pack satisfies a rule
|
| 442 |
regarding entities other than parameter packs, the parameter pack is
|
| 443 |
considered to be the entity that would result from an instantiation of
|
| 444 |
the pattern in which it appears.
|
| 445 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
A parameter pack whose name appears within the pattern of a pack
|
| 447 |
expansion is expanded by that pack expansion. An appearance of the name
|
| 448 |
of a parameter pack is only expanded by the innermost enclosing pack
|
| 449 |
expansion. The pattern of a pack expansion shall name one or more
|
| 450 |
parameter packs that are not expanded by a nested pack expansion; such
|
| 451 |
+
parameter packs are called *unexpanded parameter packs* in the pattern.
|
| 452 |
All of the parameter packs expanded by a pack expansion shall have the
|
| 453 |
same number of arguments specified. An appearance of a name of a
|
| 454 |
parameter pack that is not expanded is ill-formed.
|
| 455 |
|
| 456 |
+
[*Example 4*:
|
| 457 |
+
|
| 458 |
``` cpp
|
| 459 |
template<typename...> struct Tuple {};
|
| 460 |
template<typename T1, typename T2> struct Pair {};
|
| 461 |
|
| 462 |
template<class ... Args1> struct zip {
|
|
|
|
| 473 |
template<class ... Args>
|
| 474 |
void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
|
| 475 |
f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
|
| 476 |
f(5 ...); // error: pattern does not contain any parameter packs
|
| 477 |
f(args); // error: parameter pack ``args'' is not expanded
|
| 478 |
+
f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
|
| 479 |
+
// second ``args'' expanded within f
|
| 480 |
}
|
| 481 |
```
|
| 482 |
|
| 483 |
+
— *end example*]
|
| 484 |
+
|
| 485 |
+
The instantiation of a pack expansion that is neither a `sizeof...`
|
| 486 |
+
expression nor a *fold-expression* produces a list
|
| 487 |
+
$\mathtt{E}_1, \mathtt{E}_2, \dotsc, \mathtt{E}_N$, where N is the
|
| 488 |
+
number of elements in the pack expansion parameters. Each Eᵢ is
|
| 489 |
+
generated by instantiating the pattern and replacing each pack expansion
|
| 490 |
+
parameter with its ith element. Such an element, in the context of the
|
| 491 |
instantiation, is interpreted as follows:
|
| 492 |
|
| 493 |
- if the pack is a template parameter pack, the element is a template
|
| 494 |
parameter ([[temp.param]]) of the corresponding kind (type or
|
| 495 |
non-type) designating the type or value from the template argument;
|
| 496 |
otherwise,
|
| 497 |
- if the pack is a function parameter pack, the element is an
|
| 498 |
*id-expression* designating the function parameter that resulted from
|
| 499 |
the instantiation of the pattern where the pack is declared.
|
| 500 |
|
| 501 |
+
All of the Eᵢ become elements in the enclosing list.
|
| 502 |
+
|
| 503 |
+
[*Note 1*: The variety of list varies with the context:
|
| 504 |
+
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 505 |
+
etc. — *end note*]
|
| 506 |
+
|
| 507 |
+
When N is zero, the instantiation of the expansion produces an empty
|
| 508 |
+
list. Such an instantiation does not alter the syntactic interpretation
|
| 509 |
+
of the enclosing construct, even in cases where omitting the list
|
| 510 |
+
entirely would otherwise be ill-formed or would result in an ambiguity
|
| 511 |
+
in the grammar.
|
| 512 |
+
|
| 513 |
+
[*Example 5*:
|
| 514 |
|
| 515 |
``` cpp
|
| 516 |
template<class... T> struct X : T... { };
|
| 517 |
template<class... T> void f(T... values) {
|
| 518 |
X<T...> x(values...);
|
|
|
|
| 520 |
|
| 521 |
template void f<>(); // OK: X<> has no base classes
|
| 522 |
// x is a variable of type X<> that is value-initialized
|
| 523 |
```
|
| 524 |
|
| 525 |
+
— *end example*]
|
| 526 |
+
|
| 527 |
The instantiation of a `sizeof...` expression ([[expr.sizeof]])
|
| 528 |
produces an integral constant containing the number of elements in the
|
| 529 |
parameter pack it expands.
|
| 530 |
|
| 531 |
+
The instantiation of a *fold-expression* produces:
|
| 532 |
+
|
| 533 |
+
- `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
|
| 534 |
+
- E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
|
| 535 |
+
unary right fold,
|
| 536 |
+
- `(((`E *op* E₁`)` *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a
|
| 537 |
+
binary left fold, and
|
| 538 |
+
- E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* `(`$\mathtt{E}_{N}$ *op*
|
| 539 |
+
E`)))` for a binary right fold.
|
| 540 |
+
|
| 541 |
+
In each case, *op* is the *fold-operator*, N is the number of elements
|
| 542 |
+
in the pack expansion parameters, and each Eᵢ is generated by
|
| 543 |
+
instantiating the pattern and replacing each pack expansion parameter
|
| 544 |
+
with its ith element. For a binary fold-expression, E is generated by
|
| 545 |
+
instantiating the *cast-expression* that did not contain an unexpanded
|
| 546 |
+
parameter pack.
|
| 547 |
+
|
| 548 |
+
[*Example 6*:
|
| 549 |
+
|
| 550 |
+
``` cpp
|
| 551 |
+
template<typename ...Args>
|
| 552 |
+
bool all(Args ...args) { return (... && args); }
|
| 553 |
+
|
| 554 |
+
bool b = all(true, true, true, false);
|
| 555 |
+
```
|
| 556 |
+
|
| 557 |
+
Within the instantiation of `all`, the returned expression expands to
|
| 558 |
+
`((true && true) && true) && false`, which evaluates to `false`.
|
| 559 |
+
|
| 560 |
+
— *end example*]
|
| 561 |
+
|
| 562 |
+
If N is zero for a unary fold-expression, the value of the expression is
|
| 563 |
+
shown in Table [[tab:fold.empty]]; if the operator is not listed in
|
| 564 |
+
Table [[tab:fold.empty]], the instantiation is ill-formed.
|
| 565 |
+
|
| 566 |
+
**Table: Value of folding empty sequences** <a id="tab:fold.empty">[tab:fold.empty]</a>
|
| 567 |
+
|
| 568 |
+
| Operator | Value when parameter pack is empty |
|
| 569 |
+
| -------- | ---------------------------------- |
|
| 570 |
+
| `&&` | `true` |
|
| 571 |
+
| `||` | `false` |
|
| 572 |
+
| `,` | `void()` |
|
| 573 |
+
|
| 574 |
+
|
| 575 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 576 |
|
| 577 |
A friend of a class or class template can be a function template or
|
| 578 |
class template, a specialization of a function template or class
|
| 579 |
template, or a non-template function or class. For a friend function
|
|
|
|
| 590 |
declaration refers to the deduced specialization of that function
|
| 591 |
template ([[temp.deduct.decl]]), otherwise,
|
| 592 |
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 593 |
non-template function.
|
| 594 |
|
| 595 |
+
[*Example 1*:
|
| 596 |
+
|
| 597 |
``` cpp
|
| 598 |
template<class T> class task;
|
| 599 |
template<class T> task<T>* preempt(task<T>*);
|
| 600 |
|
| 601 |
template<class T> class task {
|
|
|
|
| 620 |
`task` class template has all specializations of the function template
|
| 621 |
`func` as friends. Similarly, each specialization of the `task` class
|
| 622 |
template has the class template specialization `task<int>` as a friend,
|
| 623 |
and has all specializations of the class template `frd` as friends.
|
| 624 |
|
| 625 |
+
— *end example*]
|
| 626 |
+
|
| 627 |
A friend template may be declared within a class or class template. A
|
| 628 |
friend function template may be defined within a class or class
|
| 629 |
template, but a friend class template may not be defined in a class or
|
| 630 |
class template. In these cases, all specializations of the friend class
|
| 631 |
or friend function template are friends of the class or class template
|
| 632 |
granting friendship.
|
| 633 |
|
| 634 |
+
[*Example 2*:
|
| 635 |
+
|
| 636 |
``` cpp
|
| 637 |
class A {
|
| 638 |
template<class T> friend class B; // OK
|
| 639 |
+
template<class T> friend void f(T){ ... } // OK
|
| 640 |
};
|
| 641 |
```
|
| 642 |
|
| 643 |
+
— *end example*]
|
| 644 |
+
|
| 645 |
A template friend declaration specifies that all specializations of that
|
| 646 |
template, whether they are implicitly instantiated ([[temp.inst]]),
|
| 647 |
partially specialized ([[temp.class.spec]]) or explicitly specialized (
|
| 648 |
[[temp.expl.spec]]), are friends of the class containing the template
|
| 649 |
friend declaration.
|
| 650 |
|
| 651 |
+
[*Example 3*:
|
| 652 |
+
|
| 653 |
``` cpp
|
| 654 |
class X {
|
| 655 |
template<class T> friend struct A;
|
| 656 |
class Y { };
|
| 657 |
};
|
| 658 |
|
| 659 |
template<class T> struct A { X::Y ab; }; // OK
|
| 660 |
template<class T> struct A<T*> { X::Y ab; }; // OK
|
| 661 |
```
|
| 662 |
|
| 663 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
A member of a class template may be declared to be a friend of a
|
| 666 |
non-template class. In this case, the corresponding member of every
|
| 667 |
+
specialization of the primary class template and class template partial
|
| 668 |
+
specializations thereof is a friend of the class granting friendship.
|
| 669 |
+
For explicit specializations and specializations of partial
|
| 670 |
+
specializations, the corresponding member is the member (if any) that
|
| 671 |
+
has the same name, kind (type, function, class template, or function
|
| 672 |
+
template), template parameters, and signature as the member of the class
|
| 673 |
+
template instantiation that would otherwise have been generated.
|
| 674 |
+
|
| 675 |
+
[*Example 4*:
|
| 676 |
|
| 677 |
``` cpp
|
| 678 |
template<class T> struct A {
|
| 679 |
struct B { };
|
| 680 |
void f();
|
|
|
|
| 698 |
template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
|
| 699 |
// because A<int>::D is not a specialization of A<T>::D
|
| 700 |
};
|
| 701 |
```
|
| 702 |
|
| 703 |
+
— *end example*]
|
| 704 |
+
|
| 705 |
+
[*Note 1*: A friend declaration may first declare a member of an
|
| 706 |
+
enclosing namespace scope ([[temp.inject]]). — *end note*]
|
| 707 |
|
| 708 |
A friend template shall not be declared in a local class.
|
| 709 |
|
| 710 |
Friend declarations shall not declare partial specializations.
|
| 711 |
|
| 712 |
+
[*Example 5*:
|
| 713 |
+
|
| 714 |
``` cpp
|
| 715 |
template<class T> class A { };
|
| 716 |
class X {
|
| 717 |
template<class T> friend class A<T*>; // error
|
| 718 |
};
|
| 719 |
```
|
| 720 |
|
| 721 |
+
— *end example*]
|
| 722 |
+
|
| 723 |
When a friend declaration refers to a specialization of a function
|
| 724 |
template, the function parameter declarations shall not include default
|
| 725 |
arguments, nor shall the inline specifier be used in such a declaration.
|
| 726 |
|
| 727 |
### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
|
| 728 |
|
| 729 |
+
A *primary class template* declaration is one in which the class
|
| 730 |
template name is an identifier. A template declaration in which the
|
| 731 |
class template name is a *simple-template-id* is a *partial
|
| 732 |
specialization* of the class template named in the *simple-template-id*.
|
| 733 |
A partial specialization of a class template provides an alternative
|
| 734 |
definition of the template that is used instead of the primary
|
|
|
|
| 743 |
|
| 744 |
Each class template partial specialization is a distinct template and
|
| 745 |
definitions shall be provided for the members of a template partial
|
| 746 |
specialization ([[temp.class.spec.mfunc]]).
|
| 747 |
|
| 748 |
+
[*Example 1*:
|
| 749 |
+
|
| 750 |
``` cpp
|
| 751 |
+
template<class T1, class T2, int I> class A { };
|
| 752 |
+
template<class T, int I> class A<T, T*, I> { };
|
| 753 |
+
template<class T1, class T2, int I> class A<T1*, T2, I> { };
|
| 754 |
+
template<class T> class A<int, T*, 5> { };
|
| 755 |
+
template<class T1, class T2, int I> class A<T1, T2*, I> { };
|
| 756 |
```
|
| 757 |
|
| 758 |
The first declaration declares the primary (unspecialized) class
|
| 759 |
template. The second and subsequent declarations declare partial
|
| 760 |
specializations of the primary template.
|
| 761 |
|
| 762 |
+
— *end example*]
|
| 763 |
+
|
| 764 |
The template parameters are specified in the angle bracket enclosed list
|
| 765 |
that immediately follows the keyword `template`. For partial
|
| 766 |
specializations, the template argument list is explicitly written
|
| 767 |
immediately following the class template name. For primary templates,
|
| 768 |
this list is implicitly described by the template parameter list.
|
| 769 |
Specifically, the order of the template arguments is the sequence in
|
| 770 |
+
which they appear in the template parameter list.
|
| 771 |
+
|
| 772 |
+
[*Example 2*: The template argument list for the primary template in
|
| 773 |
+
the example above is `<T1,` `T2,` `I>`. — *end example*]
|
| 774 |
+
|
| 775 |
+
[*Note 1*:
|
| 776 |
+
|
| 777 |
The template argument list shall not be specified in the primary
|
| 778 |
template declaration. For example,
|
| 779 |
|
| 780 |
``` cpp
|
| 781 |
+
template<class T1, class T2, int I>
|
| 782 |
+
class A<T1, T2, I> { }; // error
|
| 783 |
```
|
| 784 |
|
| 785 |
+
— *end note*]
|
| 786 |
+
|
| 787 |
+
A class template partial specialization may be declared in any scope in
|
| 788 |
+
which the corresponding primary template may be defined (
|
| 789 |
+
[[namespace.memdef]], [[class.mem]], [[temp.mem]]).
|
| 790 |
+
|
| 791 |
+
[*Example 3*:
|
| 792 |
|
| 793 |
``` cpp
|
| 794 |
template<class T> struct A {
|
| 795 |
struct C {
|
| 796 |
template<class T2> struct B { };
|
| 797 |
+
template<class T2> struct B<T2**> { }; // partial specialization #1
|
| 798 |
};
|
| 799 |
};
|
| 800 |
|
| 801 |
// partial specialization of A<T>::C::B<T2>
|
| 802 |
template<class T> template<class T2>
|
| 803 |
+
struct A<T>::C::B<T2*> { }; // #2
|
| 804 |
|
| 805 |
+
A<short>::C::B<int*> absip; // uses partial specialization #2
|
| 806 |
```
|
| 807 |
|
| 808 |
+
— *end example*]
|
| 809 |
+
|
| 810 |
Partial specialization declarations themselves are not found by name
|
| 811 |
lookup. Rather, when the primary template name is used, any
|
| 812 |
previously-declared partial specializations of the primary template are
|
| 813 |
also considered. One consequence is that a *using-declaration* which
|
| 814 |
refers to a class template does not restrict the set of partial
|
| 815 |
specializations which may be found through the *using-declaration*.
|
| 816 |
|
| 817 |
+
[*Example 4*:
|
| 818 |
+
|
| 819 |
``` cpp
|
| 820 |
namespace N {
|
| 821 |
template<class T1, class T2> class A { }; // primary template
|
| 822 |
}
|
| 823 |
|
|
|
|
| 825 |
|
| 826 |
namespace N {
|
| 827 |
template<class T> class A<T, T*> { }; // partial specialization
|
| 828 |
}
|
| 829 |
|
| 830 |
+
A<int,int*> a; // uses the partial specialization, which is found through the using-declaration
|
| 831 |
+
// which refers to the primary template
|
| 832 |
```
|
| 833 |
|
| 834 |
+
— *end example*]
|
| 835 |
+
|
| 836 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 837 |
parameter. All other non-type arguments are specialized.
|
| 838 |
|
| 839 |
Within the argument list of a class template partial specialization, the
|
| 840 |
following restrictions apply:
|
| 841 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
- The type of a template parameter corresponding to a specialized
|
| 843 |
non-type argument shall not be dependent on a parameter of the
|
| 844 |
specialization.
|
| 845 |
+
\[*Example 5*:
|
| 846 |
``` cpp
|
| 847 |
template <class T, T t> struct C {};
|
| 848 |
template <class T> struct C<T, 1>; // error
|
| 849 |
|
| 850 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 851 |
int array[5];
|
| 852 |
template< int X > class A<X,&array> { }; // error
|
| 853 |
```
|
| 854 |
+
|
| 855 |
+
— *end example*]
|
| 856 |
- The specialization shall be more specialized than the primary
|
| 857 |
template ([[temp.class.order]]).
|
| 858 |
- The template parameter list of a specialization shall not contain
|
| 859 |
default template argument values.[^4]
|
| 860 |
- An argument shall not contain an unexpanded parameter pack. If an
|
|
|
|
| 883 |
|
| 884 |
A partial specialization matches a given actual template argument list
|
| 885 |
if the template arguments of the partial specialization can be deduced
|
| 886 |
from the actual template argument list ([[temp.deduct]]).
|
| 887 |
|
| 888 |
+
[*Example 1*:
|
| 889 |
+
|
| 890 |
``` cpp
|
| 891 |
+
template<class T1, class T2, int I> class A { }; // #1
|
| 892 |
+
template<class T, int I> class A<T, T*, I> { }; // #2
|
| 893 |
+
template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3
|
| 894 |
+
template<class T> class A<int, T*, 5> { }; // #4
|
| 895 |
+
template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5
|
| 896 |
+
|
| 897 |
A<int, int, 1> a1; // uses #1
|
| 898 |
A<int, int*, 1> a2; // uses #2, T is int, I is 1
|
| 899 |
A<int, char*, 5> a3; // uses #4, T is char
|
| 900 |
A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
|
| 901 |
A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
|
| 902 |
```
|
| 903 |
|
| 904 |
+
— *end example*]
|
| 905 |
+
|
| 906 |
+
If the template arguments of a partial specialization cannot be deduced
|
| 907 |
+
because of the structure of its *template-parameter-list* and the
|
| 908 |
+
*template-id*, the program is ill-formed.
|
| 909 |
+
|
| 910 |
+
[*Example 2*:
|
| 911 |
+
|
| 912 |
+
``` cpp
|
| 913 |
+
template <int I, int J> struct A {};
|
| 914 |
+
template <int I> struct A<I+5, I*2> {}; // error
|
| 915 |
+
|
| 916 |
+
template <int I> struct A<I, I> {}; // OK
|
| 917 |
+
|
| 918 |
+
template <int I, int J, int K> struct B {};
|
| 919 |
+
template <int I> struct B<I, I*2, 2> {}; // OK
|
| 920 |
+
```
|
| 921 |
+
|
| 922 |
+
— *end example*]
|
| 923 |
|
| 924 |
In a type name that refers to a class template specialization, (e.g.,
|
| 925 |
`A<int, int, 1>`) the argument list shall match the template parameter
|
| 926 |
list of the primary template. The template arguments of a specialization
|
| 927 |
are deduced from the arguments of the primary template.
|
| 928 |
|
| 929 |
#### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
|
| 930 |
|
| 931 |
+
For two class template partial specializations, the first is *more
|
| 932 |
+
specialized* than the second if, given the following rewrite to two
|
| 933 |
+
function templates, the first function template is more specialized than
|
| 934 |
+
the second according to the ordering rules for function templates (
|
| 935 |
+
[[temp.func.order]]):
|
| 936 |
|
| 937 |
+
- Each of the two function templates has the same template parameters as
|
| 938 |
+
the corresponding partial specialization.
|
| 939 |
+
- Each function template has a single function parameter whose type is a
|
| 940 |
+
class template specialization where the template arguments are the
|
| 941 |
+
corresponding template parameters from the function template for each
|
| 942 |
+
template argument in the *template-argument-list* of the
|
| 943 |
+
*simple-template-id* of the partial specialization.
|
| 944 |
+
|
| 945 |
+
[*Example 1*:
|
| 946 |
|
| 947 |
``` cpp
|
| 948 |
template<int I, int J, class T> class X { };
|
| 949 |
template<int I, int J> class X<I, J, int> { }; // #1
|
| 950 |
template<int I> class X<I, I, int> { }; // #2
|
| 951 |
|
| 952 |
+
template<int I0, int J0> void f(X<I0, J0, int>); // A
|
| 953 |
+
template<int I0> void f(X<I0, I0, int>); // B
|
| 954 |
+
|
| 955 |
+
template <auto v> class Y { };
|
| 956 |
+
template <auto* p> class Y<p> { }; // #3
|
| 957 |
+
template <auto** pp> class Y<pp> { }; // #4
|
| 958 |
+
|
| 959 |
+
template <auto* p0> void g(Y<p0>); // C
|
| 960 |
+
template <auto** pp0> void g(Y<pp0>); // D
|
| 961 |
```
|
| 962 |
|
| 963 |
+
According to the ordering rules for function templates, the function
|
| 964 |
+
template *B* is more specialized than the function template *A* and the
|
| 965 |
+
function template *D* is more specialized than the function template
|
| 966 |
+
*C*. Therefore, the partial specialization \#2 is more specialized than
|
| 967 |
+
the partial specialization \#1 and the partial specialization \#4 is
|
| 968 |
+
more specialized than the partial specialization \#3.
|
| 969 |
+
|
| 970 |
+
— *end example*]
|
| 971 |
|
| 972 |
#### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
|
| 973 |
|
| 974 |
The template parameter list of a member of a class template partial
|
| 975 |
specialization shall match the template parameter list of the class
|
|
|
|
| 984 |
definitions for members of a class template partial specialization. An
|
| 985 |
explicit specialization of a member of a class template partial
|
| 986 |
specialization is declared in the same way as an explicit specialization
|
| 987 |
of the primary template.
|
| 988 |
|
| 989 |
+
[*Example 1*:
|
| 990 |
+
|
| 991 |
``` cpp
|
| 992 |
+
// primary class template
|
| 993 |
template<class T, int I> struct A {
|
| 994 |
void f();
|
| 995 |
};
|
| 996 |
|
| 997 |
+
// member of primary class template
|
| 998 |
template<class T, int I> void A<T,I>::f() { }
|
| 999 |
|
| 1000 |
// class template partial specialization
|
| 1001 |
template<class T> struct A<T,2> {
|
| 1002 |
void f();
|
|
|
|
| 1012 |
|
| 1013 |
int main() {
|
| 1014 |
A<char,0> a0;
|
| 1015 |
A<char,2> a2;
|
| 1016 |
a0.f(); // OK, uses definition of primary template's member
|
| 1017 |
+
a2.g(); // OK, uses definition of partial specialization's member
|
| 1018 |
+
a2.h(); // OK, uses definition of explicit specialization's member
|
| 1019 |
+
a2.f(); // ill-formed, no definition of f for A<T,2>; the primary template is not used here
|
|
|
|
|
|
|
|
|
|
| 1020 |
}
|
| 1021 |
```
|
| 1022 |
|
| 1023 |
+
— *end example*]
|
| 1024 |
+
|
| 1025 |
If a member template of a class template is partially specialized, the
|
| 1026 |
member template partial specializations are member templates of the
|
| 1027 |
enclosing class template; if the enclosing class template is
|
| 1028 |
instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
|
| 1029 |
every member template partial specialization is also instantiated as
|
|
|
|
| 1035 |
specialization of the member template is explicitly specialized for a
|
| 1036 |
given (implicit) specialization of the enclosing class template, the
|
| 1037 |
primary member template and its other partial specializations are still
|
| 1038 |
considered for this specialization of the enclosing class template.
|
| 1039 |
|
| 1040 |
+
[*Example 2*:
|
| 1041 |
+
|
| 1042 |
``` cpp
|
| 1043 |
template<class T> struct A {
|
| 1044 |
template<class T2> struct B {}; // #1
|
| 1045 |
template<class T2> struct B<T2*> {}; // #2
|
| 1046 |
};
|
|
|
|
| 1050 |
A<char>::B<int*> abcip; // uses #2
|
| 1051 |
A<short>::B<int*> absip; // uses #3
|
| 1052 |
A<char>::B<int> abci; // uses #1
|
| 1053 |
```
|
| 1054 |
|
| 1055 |
+
— *end example*]
|
| 1056 |
+
|
| 1057 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 1058 |
|
| 1059 |
+
A function template defines an unbounded set of related functions.
|
| 1060 |
+
|
| 1061 |
+
[*Example 1*:
|
| 1062 |
+
|
| 1063 |
+
A family of sort functions might be declared like this:
|
| 1064 |
|
| 1065 |
``` cpp
|
| 1066 |
template<class T> class Array { };
|
| 1067 |
template<class T> void sort(Array<T>&);
|
| 1068 |
```
|
| 1069 |
|
| 1070 |
+
— *end example*]
|
| 1071 |
+
|
| 1072 |
A function template can be overloaded with other function templates and
|
| 1073 |
with non-template functions ([[dcl.fct]]). A non-template function is
|
| 1074 |
not related to a function template (i.e., it is never considered to be a
|
| 1075 |
specialization), even if it has the same name and type as a potentially
|
| 1076 |
generated function template specialization.[^5]
|
|
|
|
| 1078 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1079 |
|
| 1080 |
It is possible to overload function templates so that two different
|
| 1081 |
function template specializations have the same type.
|
| 1082 |
|
| 1083 |
+
[*Example 1*:
|
| 1084 |
+
|
| 1085 |
``` cpp
|
| 1086 |
+
// translation unit 1:
|
| 1087 |
template<class T>
|
| 1088 |
void f(T*);
|
| 1089 |
void g(int* p) {
|
| 1090 |
f(p); // calls f<int>(int*)
|
| 1091 |
}
|
| 1092 |
```
|
| 1093 |
|
| 1094 |
``` cpp
|
| 1095 |
+
// translation unit 2:
|
| 1096 |
template<class T>
|
| 1097 |
void f(T);
|
| 1098 |
void h(int* p) {
|
| 1099 |
f(p); // calls f<int*>(int*)
|
| 1100 |
}
|
| 1101 |
```
|
| 1102 |
|
| 1103 |
+
— *end example*]
|
| 1104 |
+
|
| 1105 |
+
Such specializations are distinct functions and do not violate the
|
| 1106 |
+
one-definition rule ([[basic.def.odr]]).
|
| 1107 |
+
|
| 1108 |
+
The signature of a function template is defined in Clause
|
| 1109 |
+
[[intro.defs]]. The names of the template parameters are significant
|
| 1110 |
+
only for establishing the relationship between the template parameters
|
| 1111 |
+
and the rest of the signature.
|
| 1112 |
+
|
| 1113 |
+
[*Note 1*:
|
| 1114 |
+
|
| 1115 |
+
Two distinct function templates may have identical function return types
|
| 1116 |
+
and function parameter lists, even if overload resolution alone cannot
|
| 1117 |
+
distinguish them.
|
| 1118 |
|
| 1119 |
``` cpp
|
| 1120 |
template<class T> void f();
|
| 1121 |
template<int I> void f(); // OK: overloads the first template
|
| 1122 |
// distinguishable with an explicit template argument list
|
| 1123 |
```
|
| 1124 |
|
| 1125 |
+
— *end note*]
|
| 1126 |
+
|
| 1127 |
When an expression that references a template parameter is used in the
|
| 1128 |
function parameter list or the return type in the declaration of a
|
| 1129 |
function template, the expression that references the template parameter
|
| 1130 |
is part of the signature of the function template. This is necessary to
|
| 1131 |
permit a declaration of a function template in one translation unit to
|
| 1132 |
be linked with another declaration of the function template in another
|
| 1133 |
translation unit and, conversely, to ensure that function templates that
|
| 1134 |
are intended to be distinct are not linked with one another.
|
| 1135 |
|
| 1136 |
+
[*Example 2*:
|
| 1137 |
+
|
| 1138 |
``` cpp
|
| 1139 |
template <int I, int J> A<I+J> f(A<I>, A<J>); // #1
|
| 1140 |
template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
| 1141 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 1142 |
```
|
| 1143 |
|
| 1144 |
+
— *end example*]
|
| 1145 |
+
|
| 1146 |
+
[*Note 2*: Most expressions that use template parameters use non-type
|
| 1147 |
+
template parameters, but it is possible for an expression to reference a
|
| 1148 |
+
type parameter. For example, a template type parameter can be used in
|
| 1149 |
+
the `sizeof` operator. — *end note*]
|
| 1150 |
|
| 1151 |
Two expressions involving template parameters are considered
|
| 1152 |
*equivalent* if two function definitions containing the expressions
|
| 1153 |
+
would satisfy the one-definition rule ([[basic.def.odr]]), except that
|
| 1154 |
the tokens used to name the template parameters may differ as long as a
|
| 1155 |
token used to name a template parameter in one expression is replaced by
|
| 1156 |
another token that names the same template parameter in the other
|
| 1157 |
expression. For determining whether two dependent names ([[temp.dep]])
|
| 1158 |
are equivalent, only the name itself is considered, not the result of
|
| 1159 |
name lookup in the context of the template. If multiple declarations of
|
| 1160 |
the same function template differ in the result of this name lookup, the
|
| 1161 |
result for the first declaration is used.
|
| 1162 |
|
| 1163 |
+
[*Example 3*:
|
| 1164 |
+
|
| 1165 |
``` cpp
|
| 1166 |
template <int I, int J> void f(A<I+J>); // #1
|
| 1167 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 1168 |
|
| 1169 |
template <class T> decltype(g(T())) h();
|
|
|
|
| 1172 |
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 1173 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 1174 |
// was not in scope at the first declaration of h()
|
| 1175 |
```
|
| 1176 |
|
| 1177 |
+
— *end example*]
|
| 1178 |
+
|
| 1179 |
Two expressions involving template parameters that are not equivalent
|
| 1180 |
are *functionally equivalent* if, for any given set of template
|
| 1181 |
arguments, the evaluation of the expression results in the same value.
|
| 1182 |
|
| 1183 |
Two function templates are *equivalent* if they are declared in the same
|
|
|
|
| 1188 |
are equivalent except that one or more expressions that involve template
|
| 1189 |
parameters in the return types and parameter lists are functionally
|
| 1190 |
equivalent using the rules described above to compare expressions
|
| 1191 |
involving template parameters. If a program contains declarations of
|
| 1192 |
function templates that are functionally equivalent but not equivalent,
|
| 1193 |
+
the program is ill-formed, no diagnostic required.
|
| 1194 |
+
|
| 1195 |
+
[*Note 3*:
|
| 1196 |
|
| 1197 |
This rule guarantees that equivalent declarations will be linked with
|
| 1198 |
one another, while not requiring implementations to use heroic efforts
|
| 1199 |
to guarantee that functionally equivalent declarations will be treated
|
| 1200 |
as distinct. For example, the last two declarations are functionally
|
|
|
|
| 1212 |
// Ill-formed, no diagnostic required
|
| 1213 |
template <int I> void f(A<I>, A<I+10>);
|
| 1214 |
template <int I> void f(A<I>, A<I+1+2+3+4>);
|
| 1215 |
```
|
| 1216 |
|
| 1217 |
+
— *end note*]
|
| 1218 |
+
|
| 1219 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 1220 |
|
| 1221 |
If a function template is overloaded, the use of a function template
|
| 1222 |
specialization might be ambiguous because template argument deduction (
|
| 1223 |
[[temp.deduct]]) may associate the function template specialization with
|
|
|
|
| 1245 |
|
| 1246 |
To produce the transformed template, for each type, non-type, or
|
| 1247 |
template template parameter (including template parameter packs (
|
| 1248 |
[[temp.variadic]]) thereof) synthesize a unique type, value, or class
|
| 1249 |
template respectively and substitute it for each occurrence of that
|
| 1250 |
+
parameter in the function type of the template.
|
| 1251 |
+
|
| 1252 |
+
[*Note 1*: The type replacing the placeholder in the type of the value
|
| 1253 |
+
synthesized for a non-type template parameter is also a unique
|
| 1254 |
+
synthesized type. — *end note*]
|
| 1255 |
+
|
| 1256 |
+
If only one of the function templates *M* is a non-static member of some
|
| 1257 |
+
class *A*, *M* is considered to have a new first parameter inserted in
|
| 1258 |
+
its function parameter list. Given cv as the cv-qualifiers of *M* (if
|
| 1259 |
+
any), the new parameter is of type “rvalue reference to cv *A*” if the
|
| 1260 |
+
optional *ref-qualifier* of *M* is `&&` or if *M* has no *ref-qualifier*
|
| 1261 |
+
and the first parameter of the other template has rvalue reference type.
|
| 1262 |
+
Otherwise, the new parameter is of type “lvalue reference to cv *A*”.
|
| 1263 |
+
|
| 1264 |
+
[*Note 2*: This allows a non-static member to be ordered with respect
|
| 1265 |
+
to a non-member function and for the results to be equivalent to the
|
| 1266 |
+
ordering of two equivalent non-members. — *end note*]
|
| 1267 |
+
|
| 1268 |
+
[*Example 1*:
|
| 1269 |
|
| 1270 |
``` cpp
|
| 1271 |
struct A { };
|
| 1272 |
template<class T> struct B {
|
| 1273 |
template<class R> int operator*(R&); // #1
|
|
|
|
| 1283 |
B<A> b;
|
| 1284 |
b * a; // calls #1a
|
| 1285 |
}
|
| 1286 |
```
|
| 1287 |
|
| 1288 |
+
— *end example*]
|
| 1289 |
+
|
| 1290 |
Using the transformed function template’s function type, perform type
|
| 1291 |
deduction against the other template as described in
|
| 1292 |
[[temp.deduct.partial]].
|
| 1293 |
|
| 1294 |
+
[*Example 2*:
|
| 1295 |
+
|
| 1296 |
``` cpp
|
| 1297 |
template<class T> struct A { A(); };
|
| 1298 |
|
| 1299 |
template<class T> void f(T);
|
| 1300 |
template<class T> void f(T*);
|
|
|
|
| 1308 |
|
| 1309 |
void m() {
|
| 1310 |
const int* p;
|
| 1311 |
f(p); // f(const T*) is more specialized than f(T) or f(T*)
|
| 1312 |
float x;
|
| 1313 |
+
g(x); // ambiguous: g(T) or g(T&)
|
| 1314 |
A<int> z;
|
| 1315 |
h(z); // overload resolution selects h(A<T>&)
|
| 1316 |
const A<int> z2;
|
| 1317 |
h(z2); // h(const T&) is called because h(A<T>&) is not callable
|
| 1318 |
}
|
| 1319 |
```
|
| 1320 |
|
| 1321 |
+
— *end example*]
|
| 1322 |
+
|
| 1323 |
+
[*Note 3*:
|
| 1324 |
+
|
| 1325 |
Since partial ordering in a call context considers only parameters for
|
| 1326 |
which there are explicit call arguments, some parameters are ignored
|
| 1327 |
(namely, function parameter packs, parameters with default arguments,
|
| 1328 |
and ellipsis parameters).
|
| 1329 |
|
| 1330 |
+
[*Example 3*:
|
| 1331 |
+
|
| 1332 |
``` cpp
|
| 1333 |
template<class T> void f(T); // #1
|
| 1334 |
template<class T> void f(T*, int=1); // #2
|
| 1335 |
template<class T> void g(T); // #3
|
| 1336 |
template<class T> void g(T*, ...); // #4
|
|
|
|
| 1342 |
f(ip); // calls #2
|
| 1343 |
g(ip); // calls #4
|
| 1344 |
}
|
| 1345 |
```
|
| 1346 |
|
| 1347 |
+
— *end example*]
|
| 1348 |
+
|
| 1349 |
+
[*Example 4*:
|
| 1350 |
+
|
| 1351 |
``` cpp
|
| 1352 |
template<class T, class U> struct A { };
|
| 1353 |
|
| 1354 |
template<class T, class U> void f(U, A<U, T>* p = 0); // #1
|
| 1355 |
template< class U> void f(U, A<U, U>* p = 0); // #2
|
|
|
|
| 1361 |
f<int>(42); // error: ambiguous
|
| 1362 |
g(42); // error: ambiguous
|
| 1363 |
}
|
| 1364 |
```
|
| 1365 |
|
| 1366 |
+
— *end example*]
|
| 1367 |
+
|
| 1368 |
+
[*Example 5*:
|
| 1369 |
+
|
| 1370 |
``` cpp
|
| 1371 |
template<class T, class... U> void f(T, U...); // #1
|
| 1372 |
template<class T > void f(T); // #2
|
| 1373 |
template<class T, class... U> void g(T*, U...); // #3
|
| 1374 |
template<class T > void g(T); // #4
|
|
|
|
| 1377 |
f(&i); // error: ambiguous
|
| 1378 |
g(&i); // OK: calls #3
|
| 1379 |
}
|
| 1380 |
```
|
| 1381 |
|
| 1382 |
+
— *end example*]
|
| 1383 |
+
|
| 1384 |
+
— *end note*]
|
| 1385 |
+
|
| 1386 |
### Alias templates <a id="temp.alias">[[temp.alias]]</a>
|
| 1387 |
|
| 1388 |
A *template-declaration* in which the *declaration* is an
|
| 1389 |
*alias-declaration* (Clause [[dcl.dcl]]) declares the *identifier* to
|
| 1390 |
+
be an *alias template*. An alias template is a name for a family of
|
| 1391 |
types. The name of the alias template is a *template-name*.
|
| 1392 |
|
| 1393 |
When a *template-id* refers to the specialization of an alias template,
|
| 1394 |
it is equivalent to the associated type obtained by substitution of its
|
| 1395 |
*template-argument*s for the *template-parameter*s in the *type-id* of
|
| 1396 |
+
the alias template.
|
| 1397 |
+
|
| 1398 |
+
[*Note 1*: An alias template name is never deduced. — *end note*]
|
| 1399 |
+
|
| 1400 |
+
[*Example 1*:
|
| 1401 |
|
| 1402 |
``` cpp
|
| 1403 |
+
template<class T> struct Alloc { ... };
|
| 1404 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 1405 |
Vec<int> v; // same as vector<int, Alloc<int>{> v;}
|
| 1406 |
|
| 1407 |
template<class T>
|
| 1408 |
void process(Vec<T>& v)
|
| 1409 |
+
{ ... }
|
| 1410 |
|
| 1411 |
template<class T>
|
| 1412 |
void process(vector<T, Alloc<T>>& w)
|
| 1413 |
+
{ ... } // error: redefinition
|
| 1414 |
|
| 1415 |
template<template<class> class TT>
|
| 1416 |
void f(TT<int>);
|
| 1417 |
|
| 1418 |
f(v); // error: Vec not deduced
|
|
|
|
| 1420 |
template<template<class,class> class TT>
|
| 1421 |
void g(TT<int, Alloc<int>>);
|
| 1422 |
g(v); // OK: TT = vector
|
| 1423 |
```
|
| 1424 |
|
| 1425 |
+
— *end example*]
|
| 1426 |
+
|
| 1427 |
+
However, if the *template-id* is dependent, subsequent template argument
|
| 1428 |
+
substitution still applies to the *template-id*.
|
| 1429 |
+
|
| 1430 |
+
[*Example 2*:
|
| 1431 |
+
|
| 1432 |
+
``` cpp
|
| 1433 |
+
template<typename...> using void_t = void;
|
| 1434 |
+
template<typename T> void_t<typename T::foo> f();
|
| 1435 |
+
f<int>(); // error, int does not have a nested type foo
|
| 1436 |
+
```
|
| 1437 |
+
|
| 1438 |
+
— *end example*]
|
| 1439 |
+
|
| 1440 |
The *type-id* in an alias template declaration shall not refer to the
|
| 1441 |
alias template being declared. The type produced by an alias template
|
| 1442 |
specialization shall not directly or indirectly make use of that
|
| 1443 |
specialization.
|
| 1444 |
|
| 1445 |
+
[*Example 3*:
|
| 1446 |
+
|
| 1447 |
``` cpp
|
| 1448 |
template <class T> struct A;
|
| 1449 |
template <class T> using B = typename A<T>::U;
|
| 1450 |
template <class T> struct A {
|
| 1451 |
typedef B<T> U;
|
| 1452 |
};
|
| 1453 |
B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
|
| 1454 |
```
|
| 1455 |
|
| 1456 |
+
— *end example*]
|
| 1457 |
+
|