- tmp/tmpa61sm3n1/{from.md → to.md} +359 -163
tmp/tmpa61sm3n1/{from.md → to.md}
RENAMED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 2 |
|
| 3 |
-
The act of instantiating a function, a class, a member of a
|
| 4 |
-
template or a member template is referred to as *template
|
| 5 |
instantiation*.
|
| 6 |
|
| 7 |
A function instantiated from a function template is called an
|
| 8 |
instantiated function. A class instantiated from a class template is
|
| 9 |
called an instantiated class. A member function, a member class, a
|
|
@@ -11,18 +11,22 @@ member enumeration, or a static data member of a class template
|
|
| 11 |
instantiated from the member definition of the class template is called,
|
| 12 |
respectively, an instantiated member function, member class, member
|
| 13 |
enumeration, or static data member. A member function instantiated from
|
| 14 |
a member function template is called an instantiated member function. A
|
| 15 |
member class instantiated from a member class template is called an
|
| 16 |
-
instantiated member class.
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
An explicit specialization may be declared for a function template, a
|
| 19 |
-
class template, a member of a class template or a
|
| 20 |
-
explicit specialization declaration is introduced by
|
| 21 |
-
explicit specialization declaration for a
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
specialization declaration for a function template or a member function
|
| 25 |
template, the name of the function or member function explicitly
|
| 26 |
specialized may be a *template-id*.
|
| 27 |
|
| 28 |
[*Example 1*:
|
|
@@ -47,27 +51,39 @@ template<> int B<>::x = 1; // specialize for T == int
|
|
| 47 |
```
|
| 48 |
|
| 49 |
— *end example*]
|
| 50 |
|
| 51 |
An instantiated template specialization can be either implicitly
|
| 52 |
-
instantiated
|
| 53 |
-
instantiated
|
| 54 |
-
function, or class member that is either instantiated
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
For a given template and a given set of *template-argument*s,
|
| 58 |
|
| 59 |
- an explicit instantiation definition shall appear at most once in a
|
| 60 |
program,
|
| 61 |
-
- an explicit specialization shall be defined at most once in a program
|
| 62 |
-
|
| 63 |
- both an explicit instantiation and a declaration of an explicit
|
| 64 |
specialization shall not appear in a program unless the explicit
|
| 65 |
instantiation follows a declaration of the explicit specialization.
|
| 66 |
|
| 67 |
An implementation is not required to diagnose a violation of this rule.
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
Each class template specialization instantiated from a template has its
|
| 70 |
own copy of any static members.
|
| 71 |
|
| 72 |
[*Example 2*:
|
| 73 |
|
|
@@ -86,35 +102,50 @@ has a static member `s` of type `int` and `X<char*>` has a static member
|
|
| 86 |
`s` of type `char*`.
|
| 87 |
|
| 88 |
— *end example*]
|
| 89 |
|
| 90 |
If a function declaration acquired its function type through a dependent
|
| 91 |
-
type
|
| 92 |
declarator, the program is ill-formed.
|
| 93 |
|
| 94 |
[*Example 3*:
|
| 95 |
|
| 96 |
``` cpp
|
| 97 |
template<class T> struct A {
|
| 98 |
static T t;
|
| 99 |
};
|
| 100 |
typedef int function();
|
| 101 |
-
A<function> a; //
|
| 102 |
```
|
| 103 |
|
| 104 |
— *end example*]
|
| 105 |
|
| 106 |
### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
[[temp.expl.spec]]
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
the member or base class lists of a class template specialization, the
|
| 117 |
class template specialization is implicitly generated. For instance,
|
| 118 |
deleting a pointer to class type depends on whether or not the class
|
| 119 |
declares a destructor, and a conversion between pointers to class type
|
| 120 |
depends on the inheritance relationship between the two classes
|
|
@@ -137,44 +168,65 @@ void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
|
| 137 |
```
|
| 138 |
|
| 139 |
— *end example*]
|
| 140 |
|
| 141 |
If a class template has been declared, but not defined, at the point of
|
| 142 |
-
instantiation
|
| 143 |
-
class type
|
| 144 |
|
| 145 |
[*Example 2*:
|
| 146 |
|
| 147 |
``` cpp
|
| 148 |
template<class T> class X;
|
| 149 |
X<char> ch; // error: incomplete type X<char>
|
| 150 |
```
|
| 151 |
|
| 152 |
— *end example*]
|
| 153 |
|
| 154 |
-
[*Note
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
declared. — *end note*]
|
| 163 |
|
| 164 |
-
The implicit instantiation of a class template specialization causes
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
template is considered to be a definition.
|
| 174 |
|
| 175 |
-
[*Example
|
| 176 |
|
| 177 |
``` cpp
|
| 178 |
template<class T, class U>
|
| 179 |
struct Outer {
|
| 180 |
template<class X, class Y> struct Inner;
|
|
@@ -195,38 +247,47 @@ same partial specialization.
|
|
| 195 |
``` cpp
|
| 196 |
template<typename T> struct Friendly {
|
| 197 |
template<typename U> friend int f(U) { return sizeof(T); }
|
| 198 |
};
|
| 199 |
Friendly<char> fc;
|
| 200 |
-
Friendly<float> ff; //
|
| 201 |
```
|
| 202 |
|
| 203 |
— *end example*]
|
| 204 |
|
| 205 |
-
Unless a member of a class template or a member template
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
| 209 |
particular, the initialization (and any associated side effects) of a
|
| 210 |
static data member does not occur unless the static data member is
|
| 211 |
itself used in a way that requires the definition of the static data
|
| 212 |
member to exist.
|
| 213 |
|
| 214 |
-
Unless a function template specialization
|
| 215 |
-
|
| 216 |
-
specialization is
|
| 217 |
-
|
| 218 |
-
function whose declaration was instantiated
|
| 219 |
-
definition is implicitly instantiated when it is
|
| 220 |
-
that requires a function definition to exist
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
| 226 |
|
| 227 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
``` cpp
|
| 230 |
template<class T> struct Z {
|
| 231 |
void f();
|
| 232 |
void g();
|
|
@@ -246,22 +307,46 @@ void h() {
|
|
| 246 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 247 |
`Z<char>::f()` to be implicitly instantiated.
|
| 248 |
|
| 249 |
— *end example*]
|
| 250 |
|
| 251 |
-
Unless a variable template specialization
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
determined without instantiating a class template definition, it is
|
| 260 |
unspecified whether that instantiation actually takes place.
|
| 261 |
|
| 262 |
-
[*Example
|
| 263 |
|
| 264 |
``` cpp
|
| 265 |
template <class T> struct S {
|
| 266 |
operator int();
|
| 267 |
};
|
|
@@ -278,34 +363,41 @@ void g(S<int>& sr) {
|
|
| 278 |
|
| 279 |
— *end example*]
|
| 280 |
|
| 281 |
If a function template or a member function template specialization is
|
| 282 |
used in a way that involves overload resolution, a declaration of the
|
| 283 |
-
specialization is implicitly instantiated
|
| 284 |
|
| 285 |
An implementation shall not implicitly instantiate a function template,
|
| 286 |
a variable template, a member template, a non-virtual member function, a
|
| 287 |
member class, a static data member of a class template, or a
|
| 288 |
-
substatement of a constexpr if statement
|
| 289 |
-
instantiation is required.
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
Implicitly instantiated class, function, and variable template
|
| 300 |
specializations are placed in the namespace where the template is
|
| 301 |
defined. Implicitly instantiated specializations for members of a class
|
| 302 |
template are placed in the namespace where the enclosing class template
|
| 303 |
is defined. Implicitly instantiated member templates are placed in the
|
| 304 |
namespace where the enclosing class or class template is defined.
|
| 305 |
|
| 306 |
-
[*Example
|
| 307 |
|
| 308 |
``` cpp
|
| 309 |
namespace N {
|
| 310 |
template<class T> class List {
|
| 311 |
public:
|
|
@@ -322,11 +414,11 @@ public:
|
|
| 322 |
void g(Map<const char*,int>& m) {
|
| 323 |
int i = m.get("Nicholas");
|
| 324 |
}
|
| 325 |
```
|
| 326 |
|
| 327 |
-
|
| 328 |
`List<int>::get()` in the namespace `N` rather than in the global
|
| 329 |
namespace.
|
| 330 |
|
| 331 |
— *end example*]
|
| 332 |
|
|
@@ -335,19 +427,19 @@ argument to be used, the dependent names are looked up, the semantics
|
|
| 335 |
constraints are checked, and the instantiation of any template used in
|
| 336 |
the default argument is done as if the default argument had been an
|
| 337 |
initializer used in a function template specialization with the same
|
| 338 |
scope, the same template parameters and the same access as that of the
|
| 339 |
function template `f` used at that point, except that the scope in which
|
| 340 |
-
a closure type is declared
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
|
| 346 |
Each default argument is instantiated independently.
|
| 347 |
|
| 348 |
-
[*Example
|
| 349 |
|
| 350 |
``` cpp
|
| 351 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 352 |
|
| 353 |
class A { };
|
|
@@ -355,34 +447,34 @@ class A { };
|
|
| 355 |
A zdef(A);
|
| 356 |
|
| 357 |
void g(A a, A b, A c) {
|
| 358 |
f(a, b, c); // no default argument instantiation
|
| 359 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 360 |
-
f(a); //
|
| 361 |
}
|
| 362 |
```
|
| 363 |
|
| 364 |
— *end example*]
|
| 365 |
|
| 366 |
The *noexcept-specifier* of a function template specialization is not
|
| 367 |
instantiated along with the function declaration; it is instantiated
|
| 368 |
-
when needed
|
| 369 |
-
|
| 370 |
-
|
| 371 |
template used in the *noexcept-specifier* is done as if it were being
|
| 372 |
done as part of instantiating the declaration of the specialization at
|
| 373 |
that point.
|
| 374 |
|
| 375 |
-
[*Note
|
| 376 |
template specialization. — *end note*]
|
| 377 |
|
| 378 |
There is an *implementation-defined* quantity that specifies the limit
|
| 379 |
-
on the total depth of recursive instantiations
|
| 380 |
could involve more than one template. The result of an infinite
|
| 381 |
recursion in instantiation is undefined.
|
| 382 |
|
| 383 |
-
[*Example
|
| 384 |
|
| 385 |
``` cpp
|
| 386 |
template<class T> class X {
|
| 387 |
X<T>* p; // OK
|
| 388 |
X<T*> a; // implicit generation of X<T> requires
|
|
@@ -391,53 +483,119 @@ template<class T> class X {
|
|
| 391 |
};
|
| 392 |
```
|
| 393 |
|
| 394 |
— *end example*]
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 397 |
|
| 398 |
A class, function, variable, or member template specialization can be
|
| 399 |
explicitly instantiated from its template. A member function, member
|
| 400 |
class or static data member of a class template can be explicitly
|
| 401 |
instantiated from the member definition associated with its class
|
| 402 |
-
template.
|
| 403 |
-
function of a class template shall not use the `inline` or `constexpr`
|
| 404 |
-
specifiers.
|
| 405 |
|
| 406 |
The syntax for explicit instantiation is:
|
| 407 |
|
| 408 |
``` bnf
|
| 409 |
explicit-instantiation:
|
| 410 |
-
|
| 411 |
```
|
| 412 |
|
| 413 |
There are two forms of explicit instantiation: an explicit instantiation
|
| 414 |
definition and an explicit instantiation declaration. An explicit
|
| 415 |
instantiation declaration begins with the `extern` keyword.
|
| 416 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
If the explicit instantiation is for a class or member class, the
|
| 418 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 419 |
-
*simple-template-id*
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
a
|
|
|
|
|
|
|
|
|
|
| 423 |
|
| 424 |
[*Note 1*: The declaration may declare a *qualified-id*, in which case
|
| 425 |
the *unqualified-id* of the *qualified-id* must be a
|
| 426 |
*template-id*. — *end note*]
|
| 427 |
|
| 428 |
If the explicit instantiation is for a member function, a member class
|
| 429 |
or a static data member of a class template specialization, the name of
|
| 430 |
the class template specialization in the *qualified-id* for the member
|
| 431 |
name shall be a *simple-template-id*. If the explicit instantiation is
|
| 432 |
-
for a variable, the *unqualified-id* in the
|
| 433 |
-
*template-id*. An explicit instantiation
|
| 434 |
-
namespace of its template. If the name
|
| 435 |
-
instantiation is an unqualified name, the
|
| 436 |
-
appear in the namespace where its template
|
| 437 |
-
namespace is inline
|
| 438 |
-
enclosing namespace set.
|
| 439 |
|
| 440 |
[*Note 2*: Regarding qualified names in declarators, see
|
| 441 |
[[dcl.meaning]]. — *end note*]
|
| 442 |
|
| 443 |
[*Example 1*:
|
|
@@ -465,17 +623,50 @@ instantiation of that entity. A definition of a class template, a member
|
|
| 465 |
class of a class template, or a member class template of a class or
|
| 466 |
class template shall precede an explicit instantiation of that entity
|
| 467 |
unless the explicit instantiation is preceded by an explicit
|
| 468 |
specialization of the entity with the same template arguments. If the
|
| 469 |
*declaration* of the explicit instantiation names an implicitly-declared
|
| 470 |
-
special member function
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
For a given set of template arguments, if an explicit instantiation of a
|
| 474 |
template appears after a declaration of an explicit specialization for
|
| 475 |
that template, the explicit instantiation has no effect. Otherwise, for
|
| 476 |
-
an explicit instantiation definition the definition of a function
|
| 477 |
template, a variable template, a member function template, or a member
|
| 478 |
function or static data member of a class template shall be present in
|
| 479 |
every translation unit in which it is explicitly instantiated.
|
| 480 |
|
| 481 |
An explicit instantiation of a class, function template, or variable
|
|
@@ -483,11 +674,11 @@ template specialization is placed in the namespace in which the template
|
|
| 483 |
is defined. An explicit instantiation for a member of a class template
|
| 484 |
is placed in the namespace where the enclosing class template is
|
| 485 |
defined. An explicit instantiation for a member template is placed in
|
| 486 |
the namespace where the enclosing class or class template is defined.
|
| 487 |
|
| 488 |
-
[*Example
|
| 489 |
|
| 490 |
``` cpp
|
| 491 |
namespace N {
|
| 492 |
template<class T> class Y { void mf() { } };
|
| 493 |
}
|
|
@@ -504,13 +695,13 @@ template void N::Y<double>::mf(); // OK: explicit instantiation in namespa
|
|
| 504 |
— *end example*]
|
| 505 |
|
| 506 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 507 |
instantiation of a function template specialization or of a member
|
| 508 |
function template specialization provided it can be deduced from the
|
| 509 |
-
type of a function parameter
|
| 510 |
|
| 511 |
-
[*Example
|
| 512 |
|
| 513 |
``` cpp
|
| 514 |
template<class T> class Array { ... };
|
| 515 |
template<class T> void sort(Array<T>& v) { ... }
|
| 516 |
|
|
@@ -518,70 +709,63 @@ template<class T> void sort(Array<T>& v) { ... }
|
|
| 518 |
template void sort<>(Array<int>&);
|
| 519 |
```
|
| 520 |
|
| 521 |
— *end example*]
|
| 522 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
An explicit instantiation that names a class template specialization is
|
| 524 |
also an explicit instantiation of the same kind (declaration or
|
| 525 |
definition) of each of its members (not including members inherited from
|
| 526 |
base classes and members that are templates) that has not been
|
| 527 |
previously explicitly specialized in the translation unit containing the
|
| 528 |
-
explicit instantiation,
|
|
|
|
|
|
|
|
|
|
| 529 |
|
| 530 |
-
[*Note
|
| 531 |
of certain implementation-dependent data about the class. — *end note*]
|
| 532 |
|
| 533 |
An explicit instantiation definition that names a class template
|
| 534 |
specialization explicitly instantiates the class template specialization
|
| 535 |
and is an explicit instantiation definition of only those members that
|
| 536 |
have been defined at the point of instantiation.
|
| 537 |
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
`const` variables of literal types, variables of reference types, and
|
| 541 |
-
class template specializations, explicit instantiation declarations have
|
| 542 |
-
the effect of suppressing the implicit instantiation of the entity to
|
| 543 |
-
which they refer.
|
| 544 |
-
|
| 545 |
-
[*Note 4*: The intent is that an inline function that is the subject of
|
| 546 |
-
an explicit instantiation declaration will still be implicitly
|
| 547 |
-
instantiated when odr-used ([[basic.def.odr]]) so that the body can be
|
| 548 |
-
considered for inlining, but that no out-of-line copy of the inline
|
| 549 |
-
function would be generated in the translation unit. — *end note*]
|
| 550 |
|
| 551 |
If an entity is the subject of both an explicit instantiation
|
| 552 |
declaration and an explicit instantiation definition in the same
|
| 553 |
translation unit, the definition shall follow the declaration. An entity
|
| 554 |
that is the subject of an explicit instantiation declaration and that is
|
| 555 |
-
also used in a way that would otherwise cause an implicit
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
|
| 560 |
-
[*Note
|
| 561 |
explicit instantiation declaration of such an entity has no other
|
| 562 |
normative effect. This is needed to ensure that if the address of an
|
| 563 |
inline function is taken in a translation unit in which the
|
| 564 |
implementation chose to suppress the out-of-line body, another
|
| 565 |
translation unit will supply the body. — *end note*]
|
| 566 |
|
| 567 |
An explicit instantiation declaration shall not name a specialization of
|
| 568 |
a template with internal linkage.
|
| 569 |
|
| 570 |
-
The usual access checking rules do not apply to names used to specify
|
| 571 |
-
explicit instantiations.
|
| 572 |
-
|
| 573 |
-
[*Note 6*: In particular, the template arguments and names used in the
|
| 574 |
-
function declarator (including parameter types, return types and
|
| 575 |
-
exception specifications) may be private types or objects which would
|
| 576 |
-
normally not be accessible and the template may be a member template or
|
| 577 |
-
member function which would not normally be accessible. — *end note*]
|
| 578 |
-
|
| 579 |
An explicit instantiation does not constitute a use of a default
|
| 580 |
argument, so default argument instantiation is not done.
|
| 581 |
|
| 582 |
-
[*Example
|
| 583 |
|
| 584 |
``` cpp
|
| 585 |
char* p = 0;
|
| 586 |
template<class T> T g(T x = &p) { return x; }
|
| 587 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
@@ -605,11 +789,11 @@ An explicit specialization of any of the following:
|
|
| 605 |
|
| 606 |
can be declared by a declaration introduced by `template<>`; that is:
|
| 607 |
|
| 608 |
``` bnf
|
| 609 |
explicit-specialization:
|
| 610 |
-
|
| 611 |
```
|
| 612 |
|
| 613 |
[*Example 1*:
|
| 614 |
|
| 615 |
``` cpp
|
|
@@ -630,10 +814,13 @@ specializations instantiated from the class template. Similarly,
|
|
| 630 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 631 |
generated from the template.
|
| 632 |
|
| 633 |
— *end example*]
|
| 634 |
|
|
|
|
|
|
|
|
|
|
| 635 |
An explicit specialization may be declared in any scope in which the
|
| 636 |
corresponding primary template may be defined ([[namespace.memdef]],
|
| 637 |
[[class.mem]], [[temp.mem]]).
|
| 638 |
|
| 639 |
A declaration of a function template, class template, or variable
|
|
@@ -664,12 +851,12 @@ enumeration, a member class template, a static data member, or a static
|
|
| 664 |
data member template of a class template may be explicitly specialized
|
| 665 |
for a class specialization that is implicitly instantiated; in this
|
| 666 |
case, the definition of the class template shall precede the explicit
|
| 667 |
specialization for the member of the class template. If such an explicit
|
| 668 |
specialization for the member of a class template names an
|
| 669 |
-
implicitly-declared special member function
|
| 670 |
-
|
| 671 |
|
| 672 |
A member of an explicitly specialized class is not implicitly
|
| 673 |
instantiated from the member declaration of the class template; instead,
|
| 674 |
the member of the class template specialization shall itself be
|
| 675 |
explicitly defined if its definition is required. In this case, the
|
|
@@ -763,11 +950,11 @@ template<class T> struct A {
|
|
| 763 |
};
|
| 764 |
template<> enum A<int>::E : int { eint }; // OK
|
| 765 |
template<> enum class A<int>::S : int { sint }; // OK
|
| 766 |
template<class T> enum A<T>::E : T { eT };
|
| 767 |
template<class T> enum class A<T>::S : T { sT };
|
| 768 |
-
template<> enum A<char>::E : char { echar }; //
|
| 769 |
// when A<char> was instantiated
|
| 770 |
template<> enum class A<char>::S : char { schar }; // OK
|
| 771 |
```
|
| 772 |
|
| 773 |
— *end example*]
|
|
@@ -811,12 +998,12 @@ template<> class N::Y<short> { ... }; // OK: specialization in enclosing nam
|
|
| 811 |
|
| 812 |
— *end example*]
|
| 813 |
|
| 814 |
A *simple-template-id* that names a class template explicit
|
| 815 |
specialization that has been declared but not defined can be used
|
| 816 |
-
exactly like the names of other incompletely-defined classes
|
| 817 |
-
[[basic.types]]
|
| 818 |
|
| 819 |
[*Example 6*:
|
| 820 |
|
| 821 |
``` cpp
|
| 822 |
template<class T> class X; // X is a class template
|
|
@@ -843,18 +1030,27 @@ template<class T> void sort(Array<T>& v);
|
|
| 843 |
template<> void sort(Array<int>&);
|
| 844 |
```
|
| 845 |
|
| 846 |
— *end example*]
|
| 847 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 848 |
A function with the same name as a template and a type that exactly
|
| 849 |
matches that of a template specialization is not an explicit
|
| 850 |
-
specialization
|
| 851 |
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
|
| 857 |
[*Example 8*:
|
| 858 |
|
| 859 |
``` cpp
|
| 860 |
template<class T> void f(T) { ... }
|
|
@@ -869,11 +1065,11 @@ template<> int g<>(int) { ... } // OK: not inline
|
|
| 869 |
An explicit specialization of a static data member of a template or an
|
| 870 |
explicit specialization of a static data member template is a definition
|
| 871 |
if the declaration includes an initializer; otherwise, it is a
|
| 872 |
declaration.
|
| 873 |
|
| 874 |
-
[*Note
|
| 875 |
|
| 876 |
The definition of a static data member of a template that requires
|
| 877 |
default-initialization must use a *braced-init-list*:
|
| 878 |
|
| 879 |
``` cpp
|
|
@@ -943,11 +1139,11 @@ template<> template<> void A<char>::B<char>::mf();
|
|
| 943 |
In an explicit specialization declaration for a member of a class
|
| 944 |
template or a member template that appears in namespace scope, the
|
| 945 |
member template and some of its enclosing class templates may remain
|
| 946 |
unspecialized, except that the declaration shall not explicitly
|
| 947 |
specialize a class member template if its enclosing class templates are
|
| 948 |
-
not explicitly specialized as well. In such explicit specialization
|
| 949 |
declaration, the keyword `template` followed by a
|
| 950 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 951 |
preceding the explicit specialization declaration of the member. The
|
| 952 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 953 |
shall be the same as those specified in the primary template definition.
|
|
@@ -966,11 +1162,11 @@ template <> template <class X>
|
|
| 966 |
template <class T> void mf1(T);
|
| 967 |
};
|
| 968 |
template <> template <> template<class T>
|
| 969 |
void A<int>::B<double>::mf1(T t) { }
|
| 970 |
template <class Y> template <>
|
| 971 |
-
void A<Y>::B<double>::mf2() { } //
|
| 972 |
// its enclosing class template A is not
|
| 973 |
```
|
| 974 |
|
| 975 |
— *end example*]
|
| 976 |
|
|
@@ -986,10 +1182,10 @@ definition for one of the following explicit specializations:
|
|
| 986 |
|
| 987 |
- the explicit specialization of a function template;
|
| 988 |
- the explicit specialization of a member function template;
|
| 989 |
- the explicit specialization of a member function of a class template
|
| 990 |
where the class template specialization to which the member function
|
| 991 |
-
specialization belongs is implicitly instantiated. \[*Note
|
| 992 |
function arguments may be specified in the declaration or definition
|
| 993 |
of a member function of a class template specialization that is
|
| 994 |
explicitly specialized. — *end note*]
|
| 995 |
|
|
|
|
| 1 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 2 |
|
| 3 |
+
The act of instantiating a function, a variable, a class, a member of a
|
| 4 |
+
class template, or a member template is referred to as *template
|
| 5 |
instantiation*.
|
| 6 |
|
| 7 |
A function instantiated from a function template is called an
|
| 8 |
instantiated function. A class instantiated from a class template is
|
| 9 |
called an instantiated class. A member function, a member class, a
|
|
|
|
| 11 |
instantiated from the member definition of the class template is called,
|
| 12 |
respectively, an instantiated member function, member class, member
|
| 13 |
enumeration, or static data member. A member function instantiated from
|
| 14 |
a member function template is called an instantiated member function. A
|
| 15 |
member class instantiated from a member class template is called an
|
| 16 |
+
instantiated member class. A variable instantiated from a variable
|
| 17 |
+
template is called an instantiated variable. A static data member
|
| 18 |
+
instantiated from a static data member template is called an
|
| 19 |
+
instantiated static data member.
|
| 20 |
|
| 21 |
An explicit specialization may be declared for a function template, a
|
| 22 |
+
variable template, a class template, a member of a class template, or a
|
| 23 |
+
member template. An explicit specialization declaration is introduced by
|
| 24 |
+
`template<>`. In an explicit specialization declaration for a variable
|
| 25 |
+
template, a class template, a member of a class template or a class
|
| 26 |
+
member template, the name of the variable or class that is explicitly
|
| 27 |
+
specialized shall be a *simple-template-id*. In the explicit
|
| 28 |
specialization declaration for a function template or a member function
|
| 29 |
template, the name of the function or member function explicitly
|
| 30 |
specialized may be a *template-id*.
|
| 31 |
|
| 32 |
[*Example 1*:
|
|
|
|
| 51 |
```
|
| 52 |
|
| 53 |
— *end example*]
|
| 54 |
|
| 55 |
An instantiated template specialization can be either implicitly
|
| 56 |
+
instantiated [[temp.inst]] for a given argument list or be explicitly
|
| 57 |
+
instantiated [[temp.explicit]]. A *specialization* is a class, variable,
|
| 58 |
+
function, or class member that is either instantiated [[temp.inst]] from
|
| 59 |
+
a templated entity or is an explicit specialization [[temp.expl.spec]]
|
| 60 |
+
of a templated entity.
|
| 61 |
|
| 62 |
For a given template and a given set of *template-argument*s,
|
| 63 |
|
| 64 |
- an explicit instantiation definition shall appear at most once in a
|
| 65 |
program,
|
| 66 |
+
- an explicit specialization shall be defined at most once in a program,
|
| 67 |
+
as specified in [[basic.def.odr]], and
|
| 68 |
- both an explicit instantiation and a declaration of an explicit
|
| 69 |
specialization shall not appear in a program unless the explicit
|
| 70 |
instantiation follows a declaration of the explicit specialization.
|
| 71 |
|
| 72 |
An implementation is not required to diagnose a violation of this rule.
|
| 73 |
|
| 74 |
+
The usual access checking rules do not apply to names in a declaration
|
| 75 |
+
of an explicit instantiation or explicit specialization, with the
|
| 76 |
+
exception of names appearing in a function body, default argument,
|
| 77 |
+
base-clause, member-specification, enumerator-list, or static data
|
| 78 |
+
member or variable template initializer.
|
| 79 |
+
|
| 80 |
+
[*Note 1*: In particular, the template arguments and names used in the
|
| 81 |
+
function declarator (including parameter types, return types and
|
| 82 |
+
exception specifications) may be private types or objects that would
|
| 83 |
+
normally not be accessible. — *end note*]
|
| 84 |
+
|
| 85 |
Each class template specialization instantiated from a template has its
|
| 86 |
own copy of any static members.
|
| 87 |
|
| 88 |
[*Example 2*:
|
| 89 |
|
|
|
|
| 102 |
`s` of type `char*`.
|
| 103 |
|
| 104 |
— *end example*]
|
| 105 |
|
| 106 |
If a function declaration acquired its function type through a dependent
|
| 107 |
+
type [[temp.dep.type]] without using the syntactic form of a function
|
| 108 |
declarator, the program is ill-formed.
|
| 109 |
|
| 110 |
[*Example 3*:
|
| 111 |
|
| 112 |
``` cpp
|
| 113 |
template<class T> struct A {
|
| 114 |
static T t;
|
| 115 |
};
|
| 116 |
typedef int function();
|
| 117 |
+
A<function> a; // error: would declare A<function>::t as a static member function
|
| 118 |
```
|
| 119 |
|
| 120 |
— *end example*]
|
| 121 |
|
| 122 |
### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
|
| 123 |
|
| 124 |
+
A template specialization E is a *declared specialization* if there is a
|
| 125 |
+
reachable explicit instantiation definition [[temp.explicit]] or
|
| 126 |
+
explicit specialization declaration [[temp.expl.spec]] for E, or if
|
| 127 |
+
there is a reachable explicit instantiation declaration for E and E is
|
| 128 |
+
not
|
| 129 |
+
|
| 130 |
+
- an inline function,
|
| 131 |
+
- declared with a type deduced from its initializer or return value
|
| 132 |
+
[[dcl.spec.auto]],
|
| 133 |
+
- a potentially-constant variable [[expr.const]], or
|
| 134 |
+
- a specialization of a templated class.
|
| 135 |
+
|
| 136 |
+
[*Note 1*: An implicit instantiation in an importing translation unit
|
| 137 |
+
cannot use names with internal linkage from an imported translation unit
|
| 138 |
+
[[basic.link]]. — *end note*]
|
| 139 |
+
|
| 140 |
+
Unless a class template specialization is a declared specialization, the
|
| 141 |
+
class template specialization is implicitly instantiated when the
|
| 142 |
+
specialization is referenced in a context that requires a
|
| 143 |
+
completely-defined object type or when the completeness of the class
|
| 144 |
+
type affects the semantics of the program.
|
| 145 |
+
|
| 146 |
+
[*Note 2*: In particular, if the semantics of an expression depend on
|
| 147 |
the member or base class lists of a class template specialization, the
|
| 148 |
class template specialization is implicitly generated. For instance,
|
| 149 |
deleting a pointer to class type depends on whether or not the class
|
| 150 |
declares a destructor, and a conversion between pointers to class type
|
| 151 |
depends on the inheritance relationship between the two classes
|
|
|
|
| 168 |
```
|
| 169 |
|
| 170 |
— *end example*]
|
| 171 |
|
| 172 |
If a class template has been declared, but not defined, at the point of
|
| 173 |
+
instantiation [[temp.point]], the instantiation yields an incomplete
|
| 174 |
+
class type [[basic.types]].
|
| 175 |
|
| 176 |
[*Example 2*:
|
| 177 |
|
| 178 |
``` cpp
|
| 179 |
template<class T> class X;
|
| 180 |
X<char> ch; // error: incomplete type X<char>
|
| 181 |
```
|
| 182 |
|
| 183 |
— *end example*]
|
| 184 |
|
| 185 |
+
[*Note 3*: Within a template declaration, a local class [[class.local]]
|
| 186 |
+
or enumeration and the members of a local class are never considered to
|
| 187 |
+
be entities that can be separately instantiated (this includes their
|
| 188 |
+
default arguments, *noexcept-specifier*s, and non-static data member
|
| 189 |
+
initializers, if any, but not their *type-constraint*s or
|
| 190 |
+
*requires-clause*s). As a result, the dependent names are looked up, the
|
| 191 |
+
semantic constraints are checked, and any templates used are
|
| 192 |
+
instantiated as part of the instantiation of the entity within which the
|
| 193 |
+
local class or enumeration is declared. — *end note*]
|
| 194 |
|
| 195 |
+
The implicit instantiation of a class template specialization causes
|
| 196 |
+
|
| 197 |
+
- the implicit instantiation of the declarations, but not of the
|
| 198 |
+
definitions, of the non-deleted class member functions, member
|
| 199 |
+
classes, scoped member enumerations, static data members, member
|
| 200 |
+
templates, and friends; and
|
| 201 |
+
- the implicit instantiation of the definitions of deleted member
|
| 202 |
+
functions, unscoped member enumerations, and member anonymous unions.
|
| 203 |
+
|
| 204 |
+
The implicit instantiation of a class template specialization does not
|
| 205 |
+
cause the implicit instantiation of default arguments or
|
| 206 |
+
*noexcept-specifier*s of the class member functions.
|
| 207 |
+
|
| 208 |
+
[*Example 3*:
|
| 209 |
+
|
| 210 |
+
``` cpp
|
| 211 |
+
template<class T>
|
| 212 |
+
struct C {
|
| 213 |
+
void f() { T x; }
|
| 214 |
+
void g() = delete;
|
| 215 |
+
};
|
| 216 |
+
C<void> c; // OK, definition of C<void>::f is not instantiated at this point
|
| 217 |
+
template<> void C<int>::g() { } // error: redefinition of C<int>::g
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
— *end example*]
|
| 221 |
+
|
| 222 |
+
However, for the purpose of determining whether an instantiated
|
| 223 |
+
redeclaration is valid according to [[basic.def.odr]] and
|
| 224 |
+
[[class.mem]], a declaration that corresponds to a definition in the
|
| 225 |
template is considered to be a definition.
|
| 226 |
|
| 227 |
+
[*Example 4*:
|
| 228 |
|
| 229 |
``` cpp
|
| 230 |
template<class T, class U>
|
| 231 |
struct Outer {
|
| 232 |
template<class X, class Y> struct Inner;
|
|
|
|
| 247 |
``` cpp
|
| 248 |
template<typename T> struct Friendly {
|
| 249 |
template<typename U> friend int f(U) { return sizeof(T); }
|
| 250 |
};
|
| 251 |
Friendly<char> fc;
|
| 252 |
+
Friendly<float> ff; // error: produces second definition of f(U)
|
| 253 |
```
|
| 254 |
|
| 255 |
— *end example*]
|
| 256 |
|
| 257 |
+
Unless a member of a class template or a member template is a declared
|
| 258 |
+
specialization, the specialization of the member is implicitly
|
| 259 |
+
instantiated when the specialization is referenced in a context that
|
| 260 |
+
requires the member definition to exist or if the existence of the
|
| 261 |
+
definition of the member affects the semantics of the program; in
|
| 262 |
particular, the initialization (and any associated side effects) of a
|
| 263 |
static data member does not occur unless the static data member is
|
| 264 |
itself used in a way that requires the definition of the static data
|
| 265 |
member to exist.
|
| 266 |
|
| 267 |
+
Unless a function template specialization is a declared specialization,
|
| 268 |
+
the function template specialization is implicitly instantiated when the
|
| 269 |
+
specialization is referenced in a context that requires a function
|
| 270 |
+
definition to exist or if the existence of the definition affects the
|
| 271 |
+
semantics of the program. A function whose declaration was instantiated
|
| 272 |
+
from a friend function definition is implicitly instantiated when it is
|
| 273 |
+
referenced in a context that requires a function definition to exist or
|
| 274 |
+
if the existence of the definition affects the semantics of the program.
|
| 275 |
+
Unless a call is to a function template explicit specialization or to a
|
| 276 |
+
member function of an explicitly specialized class template, a default
|
| 277 |
+
argument for a function template or a member function of a class
|
| 278 |
+
template is implicitly instantiated when the function is called in a
|
| 279 |
+
context that requires the value of the default argument.
|
| 280 |
|
| 281 |
+
[*Note 4*: An inline function that is the subject of an explicit
|
| 282 |
+
instantiation declaration is not a declared specialization; the intent
|
| 283 |
+
is that it still be implicitly instantiated when odr-used
|
| 284 |
+
[[basic.def.odr]] so that the body can be considered for inlining, but
|
| 285 |
+
that no out-of-line copy of it be generated in the translation
|
| 286 |
+
unit. — *end note*]
|
| 287 |
+
|
| 288 |
+
[*Example 5*:
|
| 289 |
|
| 290 |
``` cpp
|
| 291 |
template<class T> struct Z {
|
| 292 |
void f();
|
| 293 |
void g();
|
|
|
|
| 307 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 308 |
`Z<char>::f()` to be implicitly instantiated.
|
| 309 |
|
| 310 |
— *end example*]
|
| 311 |
|
| 312 |
+
Unless a variable template specialization is a declared specialization,
|
| 313 |
+
the variable template specialization is implicitly instantiated when it
|
| 314 |
+
is referenced in a context that requires a variable definition to exist
|
| 315 |
+
or if the existence of the definition affects the semantics of the
|
| 316 |
+
program. A default template argument for a variable template is
|
| 317 |
+
implicitly instantiated when the variable template is referenced in a
|
| 318 |
+
context that requires the value of the default argument.
|
| 319 |
+
|
| 320 |
+
The existence of a definition of a variable or function is considered to
|
| 321 |
+
affect the semantics of the program if the variable or function is
|
| 322 |
+
needed for constant evaluation by an expression [[expr.const]], even if
|
| 323 |
+
constant evaluation of the expression is not required or if constant
|
| 324 |
+
expression evaluation does not use the definition.
|
| 325 |
+
|
| 326 |
+
[*Example 6*:
|
| 327 |
+
|
| 328 |
+
``` cpp
|
| 329 |
+
template<typename T> constexpr int f() { return T::value; }
|
| 330 |
+
template<bool B, typename T> void g(decltype(B ? f<T>() : 0));
|
| 331 |
+
template<bool B, typename T> void g(...);
|
| 332 |
+
template<bool B, typename T> void h(decltype(int{B ? f<T>() : 0}));
|
| 333 |
+
template<bool B, typename T> void h(...);
|
| 334 |
+
void x() {
|
| 335 |
+
g<false, int>(0); // OK, B ? f<T>() :\ 0 is not potentially constant evaluated
|
| 336 |
+
h<false, int>(0); // error, instantiates f<int> even though B evaluates to false and
|
| 337 |
+
// list-initialization of int from int cannot be narrowing
|
| 338 |
+
}
|
| 339 |
+
```
|
| 340 |
+
|
| 341 |
+
— *end example*]
|
| 342 |
+
|
| 343 |
+
If the function selected by overload resolution [[over.match]] can be
|
| 344 |
determined without instantiating a class template definition, it is
|
| 345 |
unspecified whether that instantiation actually takes place.
|
| 346 |
|
| 347 |
+
[*Example 7*:
|
| 348 |
|
| 349 |
``` cpp
|
| 350 |
template <class T> struct S {
|
| 351 |
operator int();
|
| 352 |
};
|
|
|
|
| 363 |
|
| 364 |
— *end example*]
|
| 365 |
|
| 366 |
If a function template or a member function template specialization is
|
| 367 |
used in a way that involves overload resolution, a declaration of the
|
| 368 |
+
specialization is implicitly instantiated [[temp.over]].
|
| 369 |
|
| 370 |
An implementation shall not implicitly instantiate a function template,
|
| 371 |
a variable template, a member template, a non-virtual member function, a
|
| 372 |
member class, a static data member of a class template, or a
|
| 373 |
+
substatement of a constexpr if statement [[stmt.if]], unless such
|
| 374 |
+
instantiation is required.
|
| 375 |
+
|
| 376 |
+
[*Note 5*: The instantiation of a generic lambda does not require
|
| 377 |
+
instantiation of substatements of a constexpr if statement within its
|
| 378 |
+
*compound-statement* unless the call operator template is
|
| 379 |
+
instantiated. — *end note*]
|
| 380 |
+
|
| 381 |
+
It is unspecified whether or not an implementation implicitly
|
| 382 |
+
instantiates a virtual member function of a class template if the
|
| 383 |
+
virtual member function would not otherwise be instantiated. The use of
|
| 384 |
+
a template specialization in a default argument shall not cause the
|
| 385 |
+
template to be implicitly instantiated except that a class template may
|
| 386 |
+
be instantiated where its complete type is needed to determine the
|
| 387 |
+
correctness of the default argument. The use of a default argument in a
|
| 388 |
+
function call causes specializations in the default argument to be
|
| 389 |
+
implicitly instantiated.
|
| 390 |
|
| 391 |
Implicitly instantiated class, function, and variable template
|
| 392 |
specializations are placed in the namespace where the template is
|
| 393 |
defined. Implicitly instantiated specializations for members of a class
|
| 394 |
template are placed in the namespace where the enclosing class template
|
| 395 |
is defined. Implicitly instantiated member templates are placed in the
|
| 396 |
namespace where the enclosing class or class template is defined.
|
| 397 |
|
| 398 |
+
[*Example 8*:
|
| 399 |
|
| 400 |
``` cpp
|
| 401 |
namespace N {
|
| 402 |
template<class T> class List {
|
| 403 |
public:
|
|
|
|
| 414 |
void g(Map<const char*,int>& m) {
|
| 415 |
int i = m.get("Nicholas");
|
| 416 |
}
|
| 417 |
```
|
| 418 |
|
| 419 |
+
A call of `lt.get()` from `Map<const char*,int>::get()` would place
|
| 420 |
`List<int>::get()` in the namespace `N` rather than in the global
|
| 421 |
namespace.
|
| 422 |
|
| 423 |
— *end example*]
|
| 424 |
|
|
|
|
| 427 |
constraints are checked, and the instantiation of any template used in
|
| 428 |
the default argument is done as if the default argument had been an
|
| 429 |
initializer used in a function template specialization with the same
|
| 430 |
scope, the same template parameters and the same access as that of the
|
| 431 |
function template `f` used at that point, except that the scope in which
|
| 432 |
+
a closure type is declared [[expr.prim.lambda.closure]] – and therefore
|
| 433 |
+
its associated namespaces – remain as determined from the context of the
|
| 434 |
+
definition for the default argument. This analysis is called *default
|
| 435 |
+
argument instantiation*. The instantiated default argument is then used
|
| 436 |
+
as the argument of `f`.
|
| 437 |
|
| 438 |
Each default argument is instantiated independently.
|
| 439 |
|
| 440 |
+
[*Example 9*:
|
| 441 |
|
| 442 |
``` cpp
|
| 443 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 444 |
|
| 445 |
class A { };
|
|
|
|
| 447 |
A zdef(A);
|
| 448 |
|
| 449 |
void g(A a, A b, A c) {
|
| 450 |
f(a, b, c); // no default argument instantiation
|
| 451 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 452 |
+
f(a); // error: ydef is not declared
|
| 453 |
}
|
| 454 |
```
|
| 455 |
|
| 456 |
— *end example*]
|
| 457 |
|
| 458 |
The *noexcept-specifier* of a function template specialization is not
|
| 459 |
instantiated along with the function declaration; it is instantiated
|
| 460 |
+
when needed [[except.spec]]. If such an *noexcept-specifier* is needed
|
| 461 |
+
but has not yet been instantiated, the dependent names are looked up,
|
| 462 |
+
the semantics constraints are checked, and the instantiation of any
|
| 463 |
template used in the *noexcept-specifier* is done as if it were being
|
| 464 |
done as part of instantiating the declaration of the specialization at
|
| 465 |
that point.
|
| 466 |
|
| 467 |
+
[*Note 6*: [[temp.point]] defines the point of instantiation of a
|
| 468 |
template specialization. — *end note*]
|
| 469 |
|
| 470 |
There is an *implementation-defined* quantity that specifies the limit
|
| 471 |
+
on the total depth of recursive instantiations [[implimits]], which
|
| 472 |
could involve more than one template. The result of an infinite
|
| 473 |
recursion in instantiation is undefined.
|
| 474 |
|
| 475 |
+
[*Example 10*:
|
| 476 |
|
| 477 |
``` cpp
|
| 478 |
template<class T> class X {
|
| 479 |
X<T>* p; // OK
|
| 480 |
X<T*> a; // implicit generation of X<T> requires
|
|
|
|
| 483 |
};
|
| 484 |
```
|
| 485 |
|
| 486 |
— *end example*]
|
| 487 |
|
| 488 |
+
The *type-constraint*s and *requires-clause* of a template
|
| 489 |
+
specialization or member function are not instantiated along with the
|
| 490 |
+
specialization or function itself, even for a member function of a local
|
| 491 |
+
class; substitution into the atomic constraints formed from them is
|
| 492 |
+
instead performed as specified in [[temp.constr.decl]] and
|
| 493 |
+
[[temp.constr.atomic]] when determining whether the constraints are
|
| 494 |
+
satisfied or as specified in [[temp.constr.decl]] when comparing
|
| 495 |
+
declarations.
|
| 496 |
+
|
| 497 |
+
[*Note 7*: The satisfaction of constraints is determined during
|
| 498 |
+
template argument deduction [[temp.deduct]] and overload resolution
|
| 499 |
+
[[over.match]]. — *end note*]
|
| 500 |
+
|
| 501 |
+
[*Example 11*:
|
| 502 |
+
|
| 503 |
+
``` cpp
|
| 504 |
+
template<typename T> concept C = sizeof(T) > 2;
|
| 505 |
+
template<typename T> concept D = C<T> && sizeof(T) > 4;
|
| 506 |
+
|
| 507 |
+
template<typename T> struct S {
|
| 508 |
+
S() requires C<T> { } // #1
|
| 509 |
+
S() requires D<T> { } // #2
|
| 510 |
+
};
|
| 511 |
+
|
| 512 |
+
S<char> s1; // error: no matching constructor
|
| 513 |
+
S<char[8]> s2; // OK, calls #2
|
| 514 |
+
```
|
| 515 |
+
|
| 516 |
+
When `S<char>` is instantiated, both constructors are part of the
|
| 517 |
+
specialization. Their constraints are not satisfied, and they suppress
|
| 518 |
+
the implicit declaration of a default constructor for `S<char>`
|
| 519 |
+
[[class.default.ctor]], so there is no viable constructor for `s1`.
|
| 520 |
+
|
| 521 |
+
— *end example*]
|
| 522 |
+
|
| 523 |
+
[*Example 12*:
|
| 524 |
+
|
| 525 |
+
``` cpp
|
| 526 |
+
template<typename T> struct S1 {
|
| 527 |
+
template<typename U>
|
| 528 |
+
requires false
|
| 529 |
+
struct Inner1; // ill-formed, no diagnostic required
|
| 530 |
+
};
|
| 531 |
+
|
| 532 |
+
template<typename T> struct S2 {
|
| 533 |
+
template<typename U>
|
| 534 |
+
requires (sizeof(T[-(int)sizeof(T)]) > 1)
|
| 535 |
+
struct Inner2; // ill-formed, no diagnostic required
|
| 536 |
+
};
|
| 537 |
+
```
|
| 538 |
+
|
| 539 |
+
The class `S1<T>::Inner1` is ill-formed, no diagnostic required, because
|
| 540 |
+
it has no valid specializations. `S2` is ill-formed, no diagnostic
|
| 541 |
+
required, since no substitution into the constraints of its `Inner2`
|
| 542 |
+
template would result in a valid expression.
|
| 543 |
+
|
| 544 |
+
— *end example*]
|
| 545 |
+
|
| 546 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 547 |
|
| 548 |
A class, function, variable, or member template specialization can be
|
| 549 |
explicitly instantiated from its template. A member function, member
|
| 550 |
class or static data member of a class template can be explicitly
|
| 551 |
instantiated from the member definition associated with its class
|
| 552 |
+
template.
|
|
|
|
|
|
|
| 553 |
|
| 554 |
The syntax for explicit instantiation is:
|
| 555 |
|
| 556 |
``` bnf
|
| 557 |
explicit-instantiation:
|
| 558 |
+
externₒₚₜ template declaration
|
| 559 |
```
|
| 560 |
|
| 561 |
There are two forms of explicit instantiation: an explicit instantiation
|
| 562 |
definition and an explicit instantiation declaration. An explicit
|
| 563 |
instantiation declaration begins with the `extern` keyword.
|
| 564 |
|
| 565 |
+
An explicit instantiation shall not use a *storage-class-specifier*
|
| 566 |
+
[[dcl.stc]] other than `thread_local`. An explicit instantiation of a
|
| 567 |
+
function template, member function of a class template, or variable
|
| 568 |
+
template shall not use the `inline`, `constexpr`, or `consteval`
|
| 569 |
+
specifiers. No *attribute-specifier-seq* [[dcl.attr.grammar]] shall
|
| 570 |
+
appertain to an explicit instantiation.
|
| 571 |
+
|
| 572 |
If the explicit instantiation is for a class or member class, the
|
| 573 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 574 |
+
*simple-template-id*; otherwise, the *declaration* shall be a
|
| 575 |
+
*simple-declaration* whose *init-declarator-list* comprises a single
|
| 576 |
+
*init-declarator* that does not have an *initializer*. If the explicit
|
| 577 |
+
instantiation is for a function or member function, the *unqualified-id*
|
| 578 |
+
in the *declarator* shall be either a *template-id* or, where all
|
| 579 |
+
template arguments can be deduced, a *template-name* or
|
| 580 |
+
*operator-function-id*.
|
| 581 |
|
| 582 |
[*Note 1*: The declaration may declare a *qualified-id*, in which case
|
| 583 |
the *unqualified-id* of the *qualified-id* must be a
|
| 584 |
*template-id*. — *end note*]
|
| 585 |
|
| 586 |
If the explicit instantiation is for a member function, a member class
|
| 587 |
or a static data member of a class template specialization, the name of
|
| 588 |
the class template specialization in the *qualified-id* for the member
|
| 589 |
name shall be a *simple-template-id*. If the explicit instantiation is
|
| 590 |
+
for a variable template specialization, the *unqualified-id* in the
|
| 591 |
+
*declarator* shall be a *simple-template-id*. An explicit instantiation
|
| 592 |
+
shall appear in an enclosing namespace of its template. If the name
|
| 593 |
+
declared in the explicit instantiation is an unqualified name, the
|
| 594 |
+
explicit instantiation shall appear in the namespace where its template
|
| 595 |
+
is declared or, if that namespace is inline [[namespace.def]], any
|
| 596 |
+
namespace from its enclosing namespace set.
|
| 597 |
|
| 598 |
[*Note 2*: Regarding qualified names in declarators, see
|
| 599 |
[[dcl.meaning]]. — *end note*]
|
| 600 |
|
| 601 |
[*Example 1*:
|
|
|
|
| 623 |
class of a class template, or a member class template of a class or
|
| 624 |
class template shall precede an explicit instantiation of that entity
|
| 625 |
unless the explicit instantiation is preceded by an explicit
|
| 626 |
specialization of the entity with the same template arguments. If the
|
| 627 |
*declaration* of the explicit instantiation names an implicitly-declared
|
| 628 |
+
special member function [[special]], the program is ill-formed.
|
| 629 |
+
|
| 630 |
+
The *declaration* in an *explicit-instantiation* and the *declaration*
|
| 631 |
+
produced by the corresponding substitution into the templated function,
|
| 632 |
+
variable, or class are two declarations of the same entity.
|
| 633 |
+
|
| 634 |
+
[*Note 3*:
|
| 635 |
+
|
| 636 |
+
These declarations are required to have matching types as specified in
|
| 637 |
+
[[basic.link]], except as specified in [[except.spec]].
|
| 638 |
+
|
| 639 |
+
[*Example 2*:
|
| 640 |
+
|
| 641 |
+
``` cpp
|
| 642 |
+
template<typename T> T var = {};
|
| 643 |
+
template float var<float>; // OK, instantiated variable has type float
|
| 644 |
+
template int var<int[16]>[]; // OK, absence of major array bound is permitted
|
| 645 |
+
template int *var<int>; // error: instantiated variable has type int
|
| 646 |
+
|
| 647 |
+
template<typename T> auto av = T();
|
| 648 |
+
template int av<int>; // OK, variable with type int can be redeclared with type auto
|
| 649 |
+
|
| 650 |
+
template<typename T> auto f() {}
|
| 651 |
+
template void f<int>(); // error: function with deduced return type
|
| 652 |
+
// redeclared with non-deduced return type[dcl.spec.auto]
|
| 653 |
+
```
|
| 654 |
+
|
| 655 |
+
— *end example*]
|
| 656 |
+
|
| 657 |
+
— *end note*]
|
| 658 |
+
|
| 659 |
+
Despite its syntactic form, the *declaration* in an
|
| 660 |
+
*explicit-instantiation* for a variable is not itself a definition and
|
| 661 |
+
does not conflict with the definition instantiated by an explicit
|
| 662 |
+
instantiation definition for that variable.
|
| 663 |
|
| 664 |
For a given set of template arguments, if an explicit instantiation of a
|
| 665 |
template appears after a declaration of an explicit specialization for
|
| 666 |
that template, the explicit instantiation has no effect. Otherwise, for
|
| 667 |
+
an explicit instantiation definition, the definition of a function
|
| 668 |
template, a variable template, a member function template, or a member
|
| 669 |
function or static data member of a class template shall be present in
|
| 670 |
every translation unit in which it is explicitly instantiated.
|
| 671 |
|
| 672 |
An explicit instantiation of a class, function template, or variable
|
|
|
|
| 674 |
is defined. An explicit instantiation for a member of a class template
|
| 675 |
is placed in the namespace where the enclosing class template is
|
| 676 |
defined. An explicit instantiation for a member template is placed in
|
| 677 |
the namespace where the enclosing class or class template is defined.
|
| 678 |
|
| 679 |
+
[*Example 3*:
|
| 680 |
|
| 681 |
``` cpp
|
| 682 |
namespace N {
|
| 683 |
template<class T> class Y { void mf() { } };
|
| 684 |
}
|
|
|
|
| 695 |
— *end example*]
|
| 696 |
|
| 697 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 698 |
instantiation of a function template specialization or of a member
|
| 699 |
function template specialization provided it can be deduced from the
|
| 700 |
+
type of a function parameter [[temp.deduct]].
|
| 701 |
|
| 702 |
+
[*Example 4*:
|
| 703 |
|
| 704 |
``` cpp
|
| 705 |
template<class T> class Array { ... };
|
| 706 |
template<class T> void sort(Array<T>& v) { ... }
|
| 707 |
|
|
|
|
| 709 |
template void sort<>(Array<int>&);
|
| 710 |
```
|
| 711 |
|
| 712 |
— *end example*]
|
| 713 |
|
| 714 |
+
[*Note 4*: An explicit instantiation of a constrained template is
|
| 715 |
+
required to satisfy that template’s associated constraints
|
| 716 |
+
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 717 |
+
forming the template name of an explicit instantiation in which all
|
| 718 |
+
template arguments are specified [[temp.names]], or, for explicit
|
| 719 |
+
instantiations of function templates, during template argument deduction
|
| 720 |
+
[[temp.deduct.decl]] when one or more trailing template arguments are
|
| 721 |
+
left unspecified. — *end note*]
|
| 722 |
+
|
| 723 |
An explicit instantiation that names a class template specialization is
|
| 724 |
also an explicit instantiation of the same kind (declaration or
|
| 725 |
definition) of each of its members (not including members inherited from
|
| 726 |
base classes and members that are templates) that has not been
|
| 727 |
previously explicitly specialized in the translation unit containing the
|
| 728 |
+
explicit instantiation, provided that the associated constraints, if
|
| 729 |
+
any, of that member are satisfied by the template arguments of the
|
| 730 |
+
explicit instantiation ([[temp.constr.decl]], [[temp.constr.constr]]),
|
| 731 |
+
except as described below.
|
| 732 |
|
| 733 |
+
[*Note 5*: In addition, it will typically be an explicit instantiation
|
| 734 |
of certain implementation-dependent data about the class. — *end note*]
|
| 735 |
|
| 736 |
An explicit instantiation definition that names a class template
|
| 737 |
specialization explicitly instantiates the class template specialization
|
| 738 |
and is an explicit instantiation definition of only those members that
|
| 739 |
have been defined at the point of instantiation.
|
| 740 |
|
| 741 |
+
An explicit instantiation of a prospective destructor [[class.dtor]]
|
| 742 |
+
shall name the selected destructor of the class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
|
| 744 |
If an entity is the subject of both an explicit instantiation
|
| 745 |
declaration and an explicit instantiation definition in the same
|
| 746 |
translation unit, the definition shall follow the declaration. An entity
|
| 747 |
that is the subject of an explicit instantiation declaration and that is
|
| 748 |
+
also used in a way that would otherwise cause an implicit instantiation
|
| 749 |
+
[[temp.inst]] in the translation unit shall be the subject of an
|
| 750 |
+
explicit instantiation definition somewhere in the program; otherwise
|
| 751 |
+
the program is ill-formed, no diagnostic required.
|
| 752 |
|
| 753 |
+
[*Note 6*: This rule does apply to inline functions even though an
|
| 754 |
explicit instantiation declaration of such an entity has no other
|
| 755 |
normative effect. This is needed to ensure that if the address of an
|
| 756 |
inline function is taken in a translation unit in which the
|
| 757 |
implementation chose to suppress the out-of-line body, another
|
| 758 |
translation unit will supply the body. — *end note*]
|
| 759 |
|
| 760 |
An explicit instantiation declaration shall not name a specialization of
|
| 761 |
a template with internal linkage.
|
| 762 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
An explicit instantiation does not constitute a use of a default
|
| 764 |
argument, so default argument instantiation is not done.
|
| 765 |
|
| 766 |
+
[*Example 5*:
|
| 767 |
|
| 768 |
``` cpp
|
| 769 |
char* p = 0;
|
| 770 |
template<class T> T g(T x = &p) { return x; }
|
| 771 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
|
|
| 789 |
|
| 790 |
can be declared by a declaration introduced by `template<>`; that is:
|
| 791 |
|
| 792 |
``` bnf
|
| 793 |
explicit-specialization:
|
| 794 |
+
template '<' '>' declaration
|
| 795 |
```
|
| 796 |
|
| 797 |
[*Example 1*:
|
| 798 |
|
| 799 |
``` cpp
|
|
|
|
| 814 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 815 |
generated from the template.
|
| 816 |
|
| 817 |
— *end example*]
|
| 818 |
|
| 819 |
+
An explicit specialization shall not use a *storage-class-specifier*
|
| 820 |
+
[[dcl.stc]] other than `thread_local`.
|
| 821 |
+
|
| 822 |
An explicit specialization may be declared in any scope in which the
|
| 823 |
corresponding primary template may be defined ([[namespace.memdef]],
|
| 824 |
[[class.mem]], [[temp.mem]]).
|
| 825 |
|
| 826 |
A declaration of a function template, class template, or variable
|
|
|
|
| 851 |
data member template of a class template may be explicitly specialized
|
| 852 |
for a class specialization that is implicitly instantiated; in this
|
| 853 |
case, the definition of the class template shall precede the explicit
|
| 854 |
specialization for the member of the class template. If such an explicit
|
| 855 |
specialization for the member of a class template names an
|
| 856 |
+
implicitly-declared special member function [[special]], the program is
|
| 857 |
+
ill-formed.
|
| 858 |
|
| 859 |
A member of an explicitly specialized class is not implicitly
|
| 860 |
instantiated from the member declaration of the class template; instead,
|
| 861 |
the member of the class template specialization shall itself be
|
| 862 |
explicitly defined if its definition is required. In this case, the
|
|
|
|
| 950 |
};
|
| 951 |
template<> enum A<int>::E : int { eint }; // OK
|
| 952 |
template<> enum class A<int>::S : int { sint }; // OK
|
| 953 |
template<class T> enum A<T>::E : T { eT };
|
| 954 |
template<class T> enum class A<T>::S : T { sT };
|
| 955 |
+
template<> enum A<char>::E : char { echar }; // error: A<char>::E was instantiated
|
| 956 |
// when A<char> was instantiated
|
| 957 |
template<> enum class A<char>::S : char { schar }; // OK
|
| 958 |
```
|
| 959 |
|
| 960 |
— *end example*]
|
|
|
|
| 998 |
|
| 999 |
— *end example*]
|
| 1000 |
|
| 1001 |
A *simple-template-id* that names a class template explicit
|
| 1002 |
specialization that has been declared but not defined can be used
|
| 1003 |
+
exactly like the names of other incompletely-defined classes
|
| 1004 |
+
[[basic.types]].
|
| 1005 |
|
| 1006 |
[*Example 6*:
|
| 1007 |
|
| 1008 |
``` cpp
|
| 1009 |
template<class T> class X; // X is a class template
|
|
|
|
| 1030 |
template<> void sort(Array<int>&);
|
| 1031 |
```
|
| 1032 |
|
| 1033 |
— *end example*]
|
| 1034 |
|
| 1035 |
+
[*Note 2*: An explicit specialization of a constrained template is
|
| 1036 |
+
required to satisfy that template’s associated constraints
|
| 1037 |
+
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 1038 |
+
forming the template name of an explicit specialization in which all
|
| 1039 |
+
template arguments are specified [[temp.names]], or, for explicit
|
| 1040 |
+
specializations of function templates, during template argument
|
| 1041 |
+
deduction [[temp.deduct.decl]] when one or more trailing template
|
| 1042 |
+
arguments are left unspecified. — *end note*]
|
| 1043 |
+
|
| 1044 |
A function with the same name as a template and a type that exactly
|
| 1045 |
matches that of a template specialization is not an explicit
|
| 1046 |
+
specialization [[temp.fct]].
|
| 1047 |
|
| 1048 |
+
Whether an explicit specialization of a function or variable template is
|
| 1049 |
+
inline, constexpr, or an immediate function is determined by the
|
| 1050 |
+
explicit specialization and is independent of those properties of the
|
| 1051 |
+
template.
|
| 1052 |
|
| 1053 |
[*Example 8*:
|
| 1054 |
|
| 1055 |
``` cpp
|
| 1056 |
template<class T> void f(T) { ... }
|
|
|
|
| 1065 |
An explicit specialization of a static data member of a template or an
|
| 1066 |
explicit specialization of a static data member template is a definition
|
| 1067 |
if the declaration includes an initializer; otherwise, it is a
|
| 1068 |
declaration.
|
| 1069 |
|
| 1070 |
+
[*Note 3*:
|
| 1071 |
|
| 1072 |
The definition of a static data member of a template that requires
|
| 1073 |
default-initialization must use a *braced-init-list*:
|
| 1074 |
|
| 1075 |
``` cpp
|
|
|
|
| 1139 |
In an explicit specialization declaration for a member of a class
|
| 1140 |
template or a member template that appears in namespace scope, the
|
| 1141 |
member template and some of its enclosing class templates may remain
|
| 1142 |
unspecialized, except that the declaration shall not explicitly
|
| 1143 |
specialize a class member template if its enclosing class templates are
|
| 1144 |
+
not explicitly specialized as well. In such an explicit specialization
|
| 1145 |
declaration, the keyword `template` followed by a
|
| 1146 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 1147 |
preceding the explicit specialization declaration of the member. The
|
| 1148 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 1149 |
shall be the same as those specified in the primary template definition.
|
|
|
|
| 1162 |
template <class T> void mf1(T);
|
| 1163 |
};
|
| 1164 |
template <> template <> template<class T>
|
| 1165 |
void A<int>::B<double>::mf1(T t) { }
|
| 1166 |
template <class Y> template <>
|
| 1167 |
+
void A<Y>::B<double>::mf2() { } // error: B<double> is specialized but
|
| 1168 |
// its enclosing class template A is not
|
| 1169 |
```
|
| 1170 |
|
| 1171 |
— *end example*]
|
| 1172 |
|
|
|
|
| 1182 |
|
| 1183 |
- the explicit specialization of a function template;
|
| 1184 |
- the explicit specialization of a member function template;
|
| 1185 |
- the explicit specialization of a member function of a class template
|
| 1186 |
where the class template specialization to which the member function
|
| 1187 |
+
specialization belongs is implicitly instantiated. \[*Note 4*: Default
|
| 1188 |
function arguments may be specified in the declaration or definition
|
| 1189 |
of a member function of a class template specialization that is
|
| 1190 |
explicitly specialized. — *end note*]
|
| 1191 |
|