- tmp/tmpqh5rxty2/{from.md → to.md} +326 -186
tmp/tmpqh5rxty2/{from.md → to.md}
RENAMED
|
@@ -23,10 +23,12 @@ class template or a class member template, the name of the class that is
|
|
| 23 |
explicitly specialized shall be a *simple-template-id*. In the explicit
|
| 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 |
``` cpp
|
| 29 |
template<class T = int> struct A {
|
| 30 |
static int x;
|
| 31 |
};
|
| 32 |
template<class U> void g(U) { }
|
|
@@ -42,10 +44,12 @@ template<class T = int> struct B {
|
|
| 42 |
static int x;
|
| 43 |
};
|
| 44 |
template<> int B<>::x = 1; // specialize for T == int
|
| 45 |
```
|
| 46 |
|
|
|
|
|
|
|
| 47 |
An instantiated template specialization can be either implicitly
|
| 48 |
instantiated ([[temp.inst]]) for a given argument list or be explicitly
|
| 49 |
instantiated ([[temp.explicit]]). A specialization is a class,
|
| 50 |
function, or class member that is either instantiated or explicitly
|
| 51 |
specialized ([[temp.expl.spec]]).
|
|
@@ -63,10 +67,12 @@ For a given template and a given set of *template-argument*s,
|
|
| 63 |
An implementation is not required to diagnose a violation of this rule.
|
| 64 |
|
| 65 |
Each class template specialization instantiated from a template has its
|
| 66 |
own copy of any static members.
|
| 67 |
|
|
|
|
|
|
|
| 68 |
``` cpp
|
| 69 |
template<class T> class X {
|
| 70 |
static T s;
|
| 71 |
};
|
| 72 |
template<class T> T X<T>::s = 0;
|
|
@@ -77,28 +83,98 @@ X<char*> bb;
|
|
| 77 |
`X<int>`
|
| 78 |
|
| 79 |
has a static member `s` of type `int` and `X<char*>` has a static member
|
| 80 |
`s` of type `char*`.
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
|
| 83 |
|
| 84 |
Unless a class template specialization has been explicitly
|
| 85 |
instantiated ([[temp.explicit]]) or explicitly specialized (
|
| 86 |
[[temp.expl.spec]]), the class template specialization is implicitly
|
| 87 |
instantiated when the specialization is referenced in a context that
|
| 88 |
requires a completely-defined object type or when the completeness of
|
| 89 |
-
the class type affects the semantics of the program.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
``` cpp
|
| 102 |
template<class T, class U>
|
| 103 |
struct Outer {
|
| 104 |
template<class X, class Y> struct Inner;
|
|
@@ -114,28 +190,43 @@ Outer<int, int> outer; // error at #2
|
|
| 114 |
defined but noted as being associated with a definition in
|
| 115 |
`Outer<T, U>`.) \#2 is also a redeclaration of \#1a. It is noted as
|
| 116 |
associated with a definition, so it is an invalid redeclaration of the
|
| 117 |
same partial specialization.
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
Unless a member of a class template or a member template has been
|
| 120 |
explicitly instantiated or explicitly specialized, the specialization of
|
| 121 |
the member is implicitly instantiated when the specialization is
|
| 122 |
referenced in a context that requires the member definition to exist; in
|
| 123 |
-
particular, the initialization (and any associated side
|
| 124 |
static data member does not occur unless the static data member is
|
| 125 |
itself used in a way that requires the definition of the static data
|
| 126 |
member to exist.
|
| 127 |
|
| 128 |
Unless a function template specialization has been explicitly
|
| 129 |
instantiated or explicitly specialized, the function template
|
| 130 |
specialization is implicitly instantiated when the specialization is
|
| 131 |
-
referenced in a context that requires a function definition to exist.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
template
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
``` cpp
|
| 139 |
template<class T> struct Z {
|
| 140 |
void f();
|
| 141 |
void g();
|
|
@@ -153,47 +244,25 @@ void h() {
|
|
| 153 |
```
|
| 154 |
|
| 155 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 156 |
`Z<char>::f()` to be implicitly instantiated.
|
| 157 |
|
|
|
|
|
|
|
| 158 |
Unless a variable template specialization has been explicitly
|
| 159 |
instantiated or explicitly specialized, the variable template
|
| 160 |
specialization is implicitly instantiated when the specialization is
|
| 161 |
used. A default template argument for a variable template is implicitly
|
| 162 |
instantiated when the variable template is referenced in a context that
|
| 163 |
requires the value of the default argument.
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
or if the completeness of the class type might affect the semantics of
|
| 168 |
-
the program. In particular, if the semantics of an expression depend on
|
| 169 |
-
the member or base class lists of a class template specialization, the
|
| 170 |
-
class template specialization is implicitly generated. For instance,
|
| 171 |
-
deleting a pointer to class type depends on whether or not the class
|
| 172 |
-
declares a destructor, and conversion between pointer to class types
|
| 173 |
-
depends on the inheritance relationship between the two classes
|
| 174 |
-
involved.
|
| 175 |
-
|
| 176 |
-
``` cpp
|
| 177 |
-
template<class T> class B { /* ... */ };
|
| 178 |
-
template<class T> class D : public B<T> { /* ... */ };
|
| 179 |
-
|
| 180 |
-
void f(void*);
|
| 181 |
-
void f(B<int>*);
|
| 182 |
-
|
| 183 |
-
void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
| 184 |
-
f(p); // instantiation of D<int> required: call f(B<int>*)
|
| 185 |
-
B<char>* q = pp; // instantiation of D<char> required:
|
| 186 |
-
// convert D<char>* to B<char>*
|
| 187 |
-
delete ppp; // instantiation of D<double> required
|
| 188 |
-
}
|
| 189 |
-
```
|
| 190 |
-
|
| 191 |
-
If the overload resolution process can determine the correct function to
|
| 192 |
-
call without instantiating a class template definition, it is
|
| 193 |
unspecified whether that instantiation actually takes place.
|
| 194 |
|
|
|
|
|
|
|
| 195 |
``` cpp
|
| 196 |
template <class T> struct S {
|
| 197 |
operator int();
|
| 198 |
};
|
| 199 |
|
|
@@ -205,31 +274,21 @@ void g(S<int>& sr) {
|
|
| 205 |
f(sr); // instantiation of S<int> allowed but not required
|
| 206 |
// instantiation of S<float> allowed but not required
|
| 207 |
};
|
| 208 |
```
|
| 209 |
|
| 210 |
-
|
| 211 |
-
required and the template is declared but not defined, the program is
|
| 212 |
-
ill-formed.
|
| 213 |
-
|
| 214 |
-
``` cpp
|
| 215 |
-
template<class T> class X;
|
| 216 |
-
|
| 217 |
-
X<char> ch; // error: definition of X required
|
| 218 |
-
```
|
| 219 |
-
|
| 220 |
-
The implicit instantiation of a class template does not cause any static
|
| 221 |
-
data members of that class to be implicitly instantiated.
|
| 222 |
|
| 223 |
If a function template or a member function template specialization is
|
| 224 |
used in a way that involves overload resolution, a declaration of the
|
| 225 |
specialization is implicitly instantiated ([[temp.over]]).
|
| 226 |
|
| 227 |
An implementation shall not implicitly instantiate a function template,
|
| 228 |
a variable template, a member template, a non-virtual member function, a
|
| 229 |
-
member class,
|
| 230 |
-
|
|
|
|
| 231 |
implementation implicitly instantiates a virtual member function of a
|
| 232 |
class template if the virtual member function would not otherwise be
|
| 233 |
instantiated. The use of a template specialization in a default argument
|
| 234 |
shall not cause the template to be implicitly instantiated except that a
|
| 235 |
class template may be instantiated where its complete type is needed to
|
|
@@ -242,10 +301,12 @@ specializations are placed in the namespace where the template is
|
|
| 242 |
defined. Implicitly instantiated specializations for members of a class
|
| 243 |
template are placed in the namespace where the enclosing class template
|
| 244 |
is defined. Implicitly instantiated member templates are placed in the
|
| 245 |
namespace where the enclosing class or class template is defined.
|
| 246 |
|
|
|
|
|
|
|
| 247 |
``` cpp
|
| 248 |
namespace N {
|
| 249 |
template<class T> class List {
|
| 250 |
public:
|
| 251 |
T* get();
|
|
@@ -265,25 +326,29 @@ void g(Map<const char*,int>& m) {
|
|
| 265 |
|
| 266 |
a call of `lt.get()` from `Map<const char*,int>::get()` would place
|
| 267 |
`List<int>::get()` in the namespace `N` rather than in the global
|
| 268 |
namespace.
|
| 269 |
|
|
|
|
|
|
|
| 270 |
If a function template `f` is called in a way that requires a default
|
| 271 |
argument to be used, the dependent names are looked up, the semantics
|
| 272 |
constraints are checked, and the instantiation of any template used in
|
| 273 |
the default argument is done as if the default argument had been an
|
| 274 |
initializer used in a function template specialization with the same
|
| 275 |
scope, the same template parameters and the same access as that of the
|
| 276 |
function template `f` used at that point, except that the scope in which
|
| 277 |
-
a closure type is declared ([[expr.prim.lambda]]) – and
|
| 278 |
-
associated namespaces – remain as determined from the
|
| 279 |
-
definition for the default argument. This analysis is
|
| 280 |
-
argument instantiation*. The instantiated default
|
| 281 |
-
as the argument of `f`.
|
| 282 |
|
| 283 |
Each default argument is instantiated independently.
|
| 284 |
|
|
|
|
|
|
|
| 285 |
``` cpp
|
| 286 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 287 |
|
| 288 |
class A { };
|
| 289 |
|
|
@@ -294,36 +359,42 @@ void g(A a, A b, A c) {
|
|
| 294 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 295 |
f(a); // ill-formed; ydef is not declared
|
| 296 |
}
|
| 297 |
```
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
| 302 |
needed but has not yet been instantiated, the dependent names are looked
|
| 303 |
up, the semantics constraints are checked, and the instantiation of any
|
| 304 |
-
template used in the *
|
| 305 |
-
|
| 306 |
-
|
| 307 |
|
| 308 |
-
[[temp.point]] defines the point of instantiation of a
|
| 309 |
-
specialization.
|
| 310 |
|
| 311 |
-
There is an implementation-defined quantity that specifies the limit
|
| 312 |
-
the total depth of recursive instantiations, which
|
| 313 |
-
than one template. The result of an infinite
|
| 314 |
-
is undefined.
|
|
|
|
|
|
|
| 315 |
|
| 316 |
``` cpp
|
| 317 |
template<class T> class X {
|
| 318 |
X<T>* p; // OK
|
| 319 |
X<T*> a; // implicit generation of X<T> requires
|
| 320 |
// the implicit instantiation of X<T*> which requires
|
| 321 |
-
// the implicit instantiation of X<T**> which
|
| 322 |
};
|
| 323 |
```
|
| 324 |
|
|
|
|
|
|
|
| 325 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 326 |
|
| 327 |
A class, function, variable, or member template specialization can be
|
| 328 |
explicitly instantiated from its template. A member function, member
|
| 329 |
class or static data member of a class template can be explicitly
|
|
@@ -346,39 +417,49 @@ instantiation declaration begins with the `extern` keyword.
|
|
| 346 |
If the explicit instantiation is for a class or member class, the
|
| 347 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 348 |
*simple-template-id*. If the explicit instantiation is for a function or
|
| 349 |
member function, the *unqualified-id* in the *declaration* shall be
|
| 350 |
either a *template-id* or, where all template arguments can be deduced,
|
| 351 |
-
a *template-name* or *operator-function-id*.
|
| 352 |
-
|
| 353 |
-
*
|
| 354 |
-
|
| 355 |
-
template
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
``` cpp
|
| 367 |
template<class T> class Array { void mf(); };
|
| 368 |
template class Array<char>;
|
| 369 |
template void Array<int>::mf();
|
| 370 |
|
| 371 |
-
template<class T> void sort(Array<T>& v) {
|
| 372 |
template void sort(Array<char>&); // argument is deduced here
|
| 373 |
|
| 374 |
namespace N {
|
| 375 |
template<class T> void f(T&) { }
|
| 376 |
}
|
| 377 |
template void N::f<int>(int&);
|
| 378 |
```
|
| 379 |
|
|
|
|
|
|
|
| 380 |
A declaration of a function template, a variable template, a member
|
| 381 |
function or static data member of a class template, or a member function
|
| 382 |
template of a class or class template shall precede an explicit
|
| 383 |
instantiation of that entity. A definition of a class template, a member
|
| 384 |
class of a class template, or a member class template of a class or
|
|
@@ -402,97 +483,114 @@ template specialization is placed in the namespace in which the template
|
|
| 402 |
is defined. An explicit instantiation for a member of a class template
|
| 403 |
is placed in the namespace where the enclosing class template is
|
| 404 |
defined. An explicit instantiation for a member template is placed in
|
| 405 |
the namespace where the enclosing class or class template is defined.
|
| 406 |
|
|
|
|
|
|
|
| 407 |
``` cpp
|
| 408 |
namespace N {
|
| 409 |
template<class T> class Y { void mf() { } };
|
| 410 |
}
|
| 411 |
|
| 412 |
-
template class Y<int>;
|
| 413 |
-
// in the global namespace
|
| 414 |
|
| 415 |
using N::Y;
|
| 416 |
-
template class Y<int>;
|
| 417 |
-
// namespace of the template
|
| 418 |
|
| 419 |
template class N::Y<char*>; // OK: explicit instantiation in namespace N
|
| 420 |
-
template void N::Y<double>::mf(); // OK: explicit instantiation
|
| 421 |
-
// in namespace N
|
| 422 |
```
|
| 423 |
|
|
|
|
|
|
|
| 424 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 425 |
instantiation of a function template specialization or of a member
|
| 426 |
function template specialization provided it can be deduced from the
|
| 427 |
type of a function parameter ([[temp.deduct]]).
|
| 428 |
|
|
|
|
|
|
|
| 429 |
``` cpp
|
| 430 |
-
template<class T> class Array {
|
| 431 |
-
template<class T> void sort(Array<T>& v) {
|
| 432 |
|
| 433 |
-
// instantiate sort(Array<int>&) - template-argument deduced
|
| 434 |
template void sort<>(Array<int>&);
|
| 435 |
```
|
| 436 |
|
|
|
|
|
|
|
| 437 |
An explicit instantiation that names a class template specialization is
|
| 438 |
also an explicit instantiation of the same kind (declaration or
|
| 439 |
definition) of each of its members (not including members inherited from
|
| 440 |
base classes and members that are templates) that has not been
|
| 441 |
previously explicitly specialized in the translation unit containing the
|
| 442 |
-
explicit instantiation, except as described below.
|
| 443 |
-
|
| 444 |
-
|
|
|
|
| 445 |
|
| 446 |
An explicit instantiation definition that names a class template
|
| 447 |
specialization explicitly instantiates the class template specialization
|
| 448 |
and is an explicit instantiation definition of only those members that
|
| 449 |
have been defined at the point of instantiation.
|
| 450 |
|
| 451 |
-
Except for inline functions, declarations with types
|
| 452 |
-
initializer or return value ([[dcl.spec.auto]]),
|
| 453 |
-
literal types, variables of reference types, and
|
| 454 |
-
specializations, explicit instantiation declarations have
|
| 455 |
-
suppressing the implicit instantiation of the entity to
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
|
|
|
|
|
|
| 461 |
|
| 462 |
If an entity is the subject of both an explicit instantiation
|
| 463 |
declaration and an explicit instantiation definition in the same
|
| 464 |
translation unit, the definition shall follow the declaration. An entity
|
| 465 |
that is the subject of an explicit instantiation declaration and that is
|
| 466 |
also used in a way that would otherwise cause an implicit
|
| 467 |
instantiation ([[temp.inst]]) in the translation unit shall be the
|
| 468 |
subject of an explicit instantiation definition somewhere in the
|
| 469 |
program; otherwise the program is ill-formed, no diagnostic required.
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
The usual access checking rules do not apply to names used to specify
|
| 479 |
-
explicit instantiations.
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
|
|
|
|
|
|
| 484 |
|
| 485 |
An explicit instantiation does not constitute a use of a default
|
| 486 |
argument, so default argument instantiation is not done.
|
| 487 |
|
|
|
|
|
|
|
| 488 |
``` cpp
|
| 489 |
char* p = 0;
|
| 490 |
template<class T> T g(T x = &p) { return x; }
|
| 491 |
template int g<int>(int); // OK even though &p isn't an int.
|
| 492 |
```
|
| 493 |
|
|
|
|
|
|
|
| 494 |
### Explicit specialization <a id="temp.expl.spec">[[temp.expl.spec]]</a>
|
| 495 |
|
| 496 |
An explicit specialization of any of the following:
|
| 497 |
|
| 498 |
- function template
|
|
@@ -510,17 +608,19 @@ can be declared by a declaration introduced by `template<>`; that is:
|
|
| 510 |
``` bnf
|
| 511 |
explicit-specialization:
|
| 512 |
'template < >' declaration
|
| 513 |
```
|
| 514 |
|
|
|
|
|
|
|
| 515 |
``` cpp
|
| 516 |
template<class T> class stream;
|
| 517 |
|
| 518 |
-
template<> class stream<char> {
|
| 519 |
|
| 520 |
-
template<class T> class Array {
|
| 521 |
-
template<class T> void sort(Array<T>& v) {
|
| 522 |
|
| 523 |
template<> void sort<char*>(Array<char*>&);
|
| 524 |
```
|
| 525 |
|
| 526 |
Given these declarations, `stream<char>` will be used as the definition
|
|
@@ -528,34 +628,39 @@ of streams of `char`s; other streams will be handled by class template
|
|
| 528 |
specializations instantiated from the class template. Similarly,
|
| 529 |
`sort<char*>` will be used as the sort function for arguments of type
|
| 530 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 531 |
generated from the template.
|
| 532 |
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
[[
|
| 538 |
-
a declaration may also be a definition. If the declaration is not a
|
| 539 |
-
definition, the specialization may be defined later (
|
| 540 |
-
[[namespace.memdef]]).
|
| 541 |
|
| 542 |
A declaration of a function template, class template, or variable
|
| 543 |
template being explicitly specialized shall precede the declaration of
|
| 544 |
-
the explicit specialization.
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
|
| 549 |
``` cpp
|
| 550 |
-
template<> class X<int> {
|
| 551 |
|
| 552 |
template<class T> class X;
|
| 553 |
|
| 554 |
-
template<> class X<char*> {
|
| 555 |
```
|
| 556 |
|
|
|
|
|
|
|
| 557 |
A member function, a member function template, a member class, a member
|
| 558 |
enumeration, a member class template, a static data member, or a static
|
| 559 |
data member template of a class template may be explicitly specialized
|
| 560 |
for a class specialization that is implicitly instantiated; in this
|
| 561 |
case, the definition of the class template shall precede the explicit
|
|
@@ -578,10 +683,12 @@ manner as members of normal classes, and not using the `template<>`
|
|
| 578 |
syntax. The same is true when defining a member of an explicitly
|
| 579 |
specialized member class. However, `template<>` is used in defining a
|
| 580 |
member of an explicitly specialized member class template that is
|
| 581 |
specialized as a class template.
|
| 582 |
|
|
|
|
|
|
|
| 583 |
``` cpp
|
| 584 |
template<class T> struct A {
|
| 585 |
struct B { };
|
| 586 |
template<class U> struct C { };
|
| 587 |
};
|
|
@@ -593,40 +700,40 @@ template<> struct A<int> {
|
|
| 593 |
void h() {
|
| 594 |
A<int> a;
|
| 595 |
a.f(16); // A<int>::f must be defined somewhere
|
| 596 |
}
|
| 597 |
|
| 598 |
-
// template<> not used for a member of an
|
| 599 |
-
|
| 600 |
-
void A<int>::f(int) { /* ... */ }
|
| 601 |
|
| 602 |
template<> struct A<char>::B {
|
| 603 |
void f();
|
| 604 |
};
|
| 605 |
-
// template<> also not used when defining a member of
|
| 606 |
-
|
| 607 |
-
void A<char>::B::f() { /* ... */ }
|
| 608 |
|
| 609 |
template<> template<class U> struct A<char>::C {
|
| 610 |
void f();
|
| 611 |
};
|
| 612 |
-
// template<> is used when defining a member of an explicitly
|
| 613 |
-
// specialized
|
| 614 |
template<>
|
| 615 |
-
template<class U> void A<char>::C<U>::f() {
|
| 616 |
|
| 617 |
template<> struct A<short>::B {
|
| 618 |
void f();
|
| 619 |
};
|
| 620 |
-
template<> void A<short>::B::f() {
|
| 621 |
|
| 622 |
template<> template<class U> struct A<short>::C {
|
| 623 |
void f();
|
| 624 |
};
|
| 625 |
-
template<class U> void A<short>::C<U>::f() {
|
| 626 |
```
|
| 627 |
|
|
|
|
|
|
|
| 628 |
If a template, a member template or a member of a class template is
|
| 629 |
explicitly specialized then that specialization shall be declared before
|
| 630 |
the first use of that specialization that would cause an implicit
|
| 631 |
instantiation to take place, in every translation unit in which such a
|
| 632 |
use occurs; no diagnostic is required. If the program does not provide a
|
|
@@ -635,22 +742,22 @@ is used in a way that would cause an implicit instantiation to take
|
|
| 635 |
place or the member is a virtual member function, the program is
|
| 636 |
ill-formed, no diagnostic required. An implicit instantiation is never
|
| 637 |
generated for an explicit specialization that is declared but not
|
| 638 |
defined.
|
| 639 |
|
|
|
|
|
|
|
| 640 |
``` cpp
|
| 641 |
class String { };
|
| 642 |
-
template<class T> class Array {
|
| 643 |
-
template<class T> void sort(Array<T>& v) {
|
| 644 |
|
| 645 |
void f(Array<String>& v) {
|
| 646 |
-
sort(v); // use primary template
|
| 647 |
-
// sort(Array<T>&), T is String
|
| 648 |
}
|
| 649 |
|
| 650 |
-
template<> void sort<String>(Array<String>& v);
|
| 651 |
-
// after use of primary template
|
| 652 |
template<> void sort<>(Array<char*>& v); // OK: sort<char*> not yet used
|
| 653 |
template<class T> struct A {
|
| 654 |
enum E : T;
|
| 655 |
enum class S : T;
|
| 656 |
};
|
|
@@ -661,10 +768,12 @@ template<class T> enum class A<T>::S : T { sT };
|
|
| 661 |
template<> enum A<char>::E : char { echar }; // ill-formed, A<char>::E was instantiated
|
| 662 |
// when A<char> was instantiated
|
| 663 |
template<> enum class A<char>::S : char { schar }; // OK
|
| 664 |
```
|
| 665 |
|
|
|
|
|
|
|
| 666 |
The placement of explicit specialization declarations for function
|
| 667 |
templates, class templates, variable templates, member functions of
|
| 668 |
class templates, static data members of class templates, member classes
|
| 669 |
of class templates, member enumerations of class templates, member class
|
| 670 |
templates of class templates, member function templates of class
|
|
@@ -683,87 +792,108 @@ When writing a specialization, be careful about its location; or to make
|
|
| 683 |
it compile will be such a trial as to kindle its self-immolation.
|
| 684 |
|
| 685 |
A template explicit specialization is in the scope of the namespace in
|
| 686 |
which the template was defined.
|
| 687 |
|
|
|
|
|
|
|
| 688 |
``` cpp
|
| 689 |
namespace N {
|
| 690 |
-
template<class T> class X {
|
| 691 |
-
template<class T> class Y {
|
| 692 |
|
| 693 |
-
template<> class X<int> {
|
| 694 |
-
|
| 695 |
-
template<> class Y<double>; // forward declare intent to
|
| 696 |
-
// specialize for double
|
| 697 |
}
|
| 698 |
|
| 699 |
-
template<> class N::Y<double> {
|
| 700 |
-
|
| 701 |
-
template<> class N::Y<short> { /* ... */ }; // OK: specialization
|
| 702 |
-
// in enclosing namespace
|
| 703 |
```
|
| 704 |
|
|
|
|
|
|
|
| 705 |
A *simple-template-id* that names a class template explicit
|
| 706 |
specialization that has been declared but not defined can be used
|
| 707 |
exactly like the names of other incompletely-defined classes (
|
| 708 |
[[basic.types]]).
|
| 709 |
|
|
|
|
|
|
|
| 710 |
``` cpp
|
| 711 |
template<class T> class X; // X is a class template
|
| 712 |
template<> class X<int>;
|
| 713 |
|
| 714 |
X<int>* p; // OK: pointer to declared class X<int>
|
| 715 |
X<int> x; // error: object of incomplete class X<int>
|
| 716 |
```
|
| 717 |
|
|
|
|
|
|
|
| 718 |
A trailing *template-argument* can be left unspecified in the
|
| 719 |
*template-id* naming an explicit function template specialization
|
| 720 |
provided it can be deduced from the function argument type.
|
| 721 |
|
|
|
|
|
|
|
| 722 |
``` cpp
|
| 723 |
-
template<class T> class Array {
|
| 724 |
template<class T> void sort(Array<T>& v);
|
| 725 |
|
| 726 |
// explicit specialization for sort(Array<int>&)
|
| 727 |
// with deduced template-argument of type int
|
| 728 |
template<> void sort(Array<int>&);
|
| 729 |
```
|
| 730 |
|
|
|
|
|
|
|
| 731 |
A function with the same name as a template and a type that exactly
|
| 732 |
matches that of a template specialization is not an explicit
|
| 733 |
specialization ([[temp.fct]]).
|
| 734 |
|
| 735 |
-
An explicit specialization of a function template is inline
|
| 736 |
-
is declared with the `inline` specifier or defined as
|
| 737 |
-
independently of whether its function
|
|
|
|
|
|
|
|
|
|
| 738 |
|
| 739 |
``` cpp
|
| 740 |
-
template<class T> void f(T) {
|
| 741 |
-
template<class T> inline T g(T) {
|
| 742 |
|
| 743 |
-
template<> inline void f<>(int) {
|
| 744 |
-
template<> int g<>(int) {
|
| 745 |
```
|
| 746 |
|
|
|
|
|
|
|
| 747 |
An explicit specialization of a static data member of a template or an
|
| 748 |
explicit specialization of a static data member template is a definition
|
| 749 |
if the declaration includes an initializer; otherwise, it is a
|
| 750 |
-
declaration.
|
| 751 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 752 |
|
| 753 |
``` cpp
|
| 754 |
template<> X Q<int>::x; // declaration
|
| 755 |
template<> X Q<int>::x (); // error: declares a function
|
| 756 |
template<> X Q<int>::x { }; // definition
|
| 757 |
```
|
| 758 |
|
|
|
|
|
|
|
| 759 |
A member or a member template of a class template may be explicitly
|
| 760 |
specialized for a given implicit instantiation of the class template,
|
| 761 |
even if the member or member template is defined in the class template
|
| 762 |
definition. An explicit specialization of a member or member template is
|
| 763 |
specified using the syntax for explicit specialization.
|
| 764 |
|
|
|
|
|
|
|
| 765 |
``` cpp
|
| 766 |
template<class T> struct A {
|
| 767 |
void f(T);
|
| 768 |
template<class X1> void g1(T, X1);
|
| 769 |
template<class X2> void g2(T, X2);
|
|
@@ -787,37 +917,45 @@ template<> template<>
|
|
| 787 |
|
| 788 |
// member specialization even if defined in class definition
|
| 789 |
template<> void A<int>::h(int) { }
|
| 790 |
```
|
| 791 |
|
|
|
|
|
|
|
| 792 |
A member or a member template may be nested within many enclosing class
|
| 793 |
templates. In an explicit specialization for such a member, the member
|
| 794 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 795 |
template that is explicitly specialized.
|
| 796 |
|
|
|
|
|
|
|
| 797 |
``` cpp
|
| 798 |
template<class T1> class A {
|
| 799 |
template<class T2> class B {
|
| 800 |
void mf();
|
| 801 |
};
|
| 802 |
};
|
| 803 |
template<> template<> class A<int>::B<double>;
|
| 804 |
template<> template<> void A<char>::B<char>::mf();
|
| 805 |
```
|
| 806 |
|
|
|
|
|
|
|
| 807 |
In an explicit specialization declaration for a member of a class
|
| 808 |
template or a member template that appears in namespace scope, the
|
| 809 |
member template and some of its enclosing class templates may remain
|
| 810 |
unspecialized, except that the declaration shall not explicitly
|
| 811 |
specialize a class member template if its enclosing class templates are
|
| 812 |
not explicitly specialized as well. In such explicit specialization
|
| 813 |
declaration, the keyword `template` followed by a
|
| 814 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 815 |
preceding the explicit specialization declaration of the member. The
|
| 816 |
-
types of the *template-
|
| 817 |
shall be the same as those specified in the primary template definition.
|
| 818 |
|
|
|
|
|
|
|
| 819 |
``` cpp
|
| 820 |
template <class T1> class A {
|
| 821 |
template<class T2> class B {
|
| 822 |
template<class T3> void mf1(T3);
|
| 823 |
void mf2();
|
|
@@ -832,10 +970,12 @@ template <> template <> template<class T>
|
|
| 832 |
template <class Y> template <>
|
| 833 |
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
|
| 834 |
// its enclosing class template A is not
|
| 835 |
```
|
| 836 |
|
|
|
|
|
|
|
| 837 |
A specialization of a member function template, member class template,
|
| 838 |
or static data member template of a non-specialized class template is
|
| 839 |
itself a template.
|
| 840 |
|
| 841 |
An explicit specialization declaration shall not be a friend
|
|
@@ -846,10 +986,10 @@ definition for one of the following explicit specializations:
|
|
| 846 |
|
| 847 |
- the explicit specialization of a function template;
|
| 848 |
- the explicit specialization of a member function template;
|
| 849 |
- the explicit specialization of a member function of a class template
|
| 850 |
where the class template specialization to which the member function
|
| 851 |
-
specialization belongs is implicitly instantiated.
|
| 852 |
-
arguments may be specified in the declaration or definition
|
| 853 |
-
member function of a class template specialization that is
|
| 854 |
-
specialized.
|
| 855 |
|
|
|
|
| 23 |
explicitly specialized shall be a *simple-template-id*. In the explicit
|
| 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*:
|
| 29 |
+
|
| 30 |
``` cpp
|
| 31 |
template<class T = int> struct A {
|
| 32 |
static int x;
|
| 33 |
};
|
| 34 |
template<class U> void g(U) { }
|
|
|
|
| 44 |
static int x;
|
| 45 |
};
|
| 46 |
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 ([[temp.inst]]) for a given argument list or be explicitly
|
| 53 |
instantiated ([[temp.explicit]]). A specialization is a class,
|
| 54 |
function, or class member that is either instantiated or explicitly
|
| 55 |
specialized ([[temp.expl.spec]]).
|
|
|
|
| 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 |
+
|
| 74 |
``` cpp
|
| 75 |
template<class T> class X {
|
| 76 |
static T s;
|
| 77 |
};
|
| 78 |
template<class T> T X<T>::s = 0;
|
|
|
|
| 83 |
`X<int>`
|
| 84 |
|
| 85 |
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 ([[temp.dep.type]]) without using the syntactic form of a function
|
| 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; // ill-formed: would declare A<function>::t as a static member function
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
— *end example*]
|
| 105 |
+
|
| 106 |
### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
|
| 107 |
|
| 108 |
Unless a class template specialization has been explicitly
|
| 109 |
instantiated ([[temp.explicit]]) or explicitly specialized (
|
| 110 |
[[temp.expl.spec]]), the class template specialization is implicitly
|
| 111 |
instantiated when the specialization is referenced in a context that
|
| 112 |
requires a completely-defined object type or when the completeness of
|
| 113 |
+
the class type affects the semantics of the program.
|
| 114 |
+
|
| 115 |
+
[*Note 1*: In particular, if the semantics of an expression depend on
|
| 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
|
| 121 |
+
involved. — *end note*]
|
| 122 |
+
|
| 123 |
+
[*Example 1*:
|
| 124 |
+
|
| 125 |
+
``` cpp
|
| 126 |
+
template<class T> class B { ... };
|
| 127 |
+
template<class T> class D : public B<T> { ... };
|
| 128 |
+
|
| 129 |
+
void f(void*);
|
| 130 |
+
void f(B<int>*);
|
| 131 |
+
|
| 132 |
+
void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
| 133 |
+
f(p); // instantiation of D<int> required: call f(B<int>*)
|
| 134 |
+
B<char>* q = pp; // instantiation of D<char> required: convert D<char>* to B<char>*
|
| 135 |
+
delete ppp; // instantiation of D<double> required
|
| 136 |
+
}
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
— *end example*]
|
| 140 |
+
|
| 141 |
+
If a class template has been declared, but not defined, at the point of
|
| 142 |
+
instantiation ([[temp.point]]), the instantiation yields an incomplete
|
| 143 |
+
class type ([[basic.types]]).
|
| 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 2*: Within a template declaration, a local class (
|
| 155 |
+
[[class.local]]) or enumeration and the members of a local class are
|
| 156 |
+
never considered to be entities that can be separately instantiated
|
| 157 |
+
(this includes their default arguments, *noexcept-specifier*s, and
|
| 158 |
+
non-static data member initializers, if any). As a result, the dependent
|
| 159 |
+
names are looked up, the semantic constraints are checked, and any
|
| 160 |
+
templates used are instantiated as part of the instantiation of the
|
| 161 |
+
entity within which the local class or enumeration is
|
| 162 |
+
declared. — *end note*]
|
| 163 |
+
|
| 164 |
+
The implicit instantiation of a class template specialization causes the
|
| 165 |
+
implicit instantiation of the declarations, but not of the definitions,
|
| 166 |
+
default arguments, or *noexcept-specifier*s of the class member
|
| 167 |
+
functions, member classes, scoped member enumerations, static data
|
| 168 |
+
members, member templates, and friends; and it causes the implicit
|
| 169 |
+
instantiation of the definitions of unscoped member enumerations and
|
| 170 |
+
member anonymous unions. However, for the purpose of determining whether
|
| 171 |
+
an instantiated redeclaration is valid according to [[basic.def.odr]]
|
| 172 |
+
and [[class.mem]], a declaration that corresponds to a definition in the
|
| 173 |
+
template is considered to be a definition.
|
| 174 |
+
|
| 175 |
+
[*Example 3*:
|
| 176 |
|
| 177 |
``` cpp
|
| 178 |
template<class T, class U>
|
| 179 |
struct Outer {
|
| 180 |
template<class X, class Y> struct Inner;
|
|
|
|
| 190 |
defined but noted as being associated with a definition in
|
| 191 |
`Outer<T, U>`.) \#2 is also a redeclaration of \#1a. It is noted as
|
| 192 |
associated with a definition, so it is an invalid redeclaration of the
|
| 193 |
same partial specialization.
|
| 194 |
|
| 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; // ill-formed: produces second definition of f(U)
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
— *end example*]
|
| 204 |
+
|
| 205 |
Unless a member of a class template or a member template has been
|
| 206 |
explicitly instantiated or explicitly specialized, the specialization of
|
| 207 |
the member is implicitly instantiated when the specialization is
|
| 208 |
referenced in a context that requires the member definition to exist; in
|
| 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 has been explicitly
|
| 215 |
instantiated or explicitly specialized, the function template
|
| 216 |
specialization is implicitly instantiated when the specialization is
|
| 217 |
+
referenced in a context that requires a function definition to exist. A
|
| 218 |
+
function whose declaration was instantiated from a friend function
|
| 219 |
+
definition is implicitly instantiated when it is referenced in a context
|
| 220 |
+
that requires a function definition to exist. Unless a call is to a
|
| 221 |
+
function template explicit specialization or to a member function of an
|
| 222 |
+
explicitly specialized class template, a default argument for a function
|
| 223 |
+
template or a member function of a class template is implicitly
|
| 224 |
+
instantiated when the function is called in a context that requires the
|
| 225 |
+
value of the default argument.
|
| 226 |
+
|
| 227 |
+
[*Example 4*:
|
| 228 |
|
| 229 |
``` cpp
|
| 230 |
template<class T> struct Z {
|
| 231 |
void f();
|
| 232 |
void g();
|
|
|
|
| 244 |
```
|
| 245 |
|
| 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 has been explicitly
|
| 252 |
instantiated or explicitly specialized, the variable template
|
| 253 |
specialization is implicitly instantiated when the specialization is
|
| 254 |
used. A default template argument for a variable template is implicitly
|
| 255 |
instantiated when the variable template is referenced in a context that
|
| 256 |
requires the value of the default argument.
|
| 257 |
|
| 258 |
+
If the function selected by overload resolution ([[over.match]]) can be
|
| 259 |
+
determined without instantiating a class template definition, it is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
unspecified whether that instantiation actually takes place.
|
| 261 |
|
| 262 |
+
[*Example 5*:
|
| 263 |
+
|
| 264 |
``` cpp
|
| 265 |
template <class T> struct S {
|
| 266 |
operator int();
|
| 267 |
};
|
| 268 |
|
|
|
|
| 274 |
f(sr); // instantiation of S<int> allowed but not required
|
| 275 |
// instantiation of S<float> allowed but not required
|
| 276 |
};
|
| 277 |
```
|
| 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 ([[temp.over]]).
|
| 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 ([[stmt.if]]), unless such
|
| 289 |
+
instantiation is required. It is unspecified whether or not an
|
| 290 |
implementation implicitly instantiates a virtual member function of a
|
| 291 |
class template if the virtual member function would not otherwise be
|
| 292 |
instantiated. The use of a template specialization in a default argument
|
| 293 |
shall not cause the template to be implicitly instantiated except that a
|
| 294 |
class template may be instantiated where its complete type is needed to
|
|
|
|
| 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 6*:
|
| 307 |
+
|
| 308 |
``` cpp
|
| 309 |
namespace N {
|
| 310 |
template<class T> class List {
|
| 311 |
public:
|
| 312 |
T* get();
|
|
|
|
| 326 |
|
| 327 |
a call of `lt.get()` from `Map<const char*,int>::get()` would place
|
| 328 |
`List<int>::get()` in the namespace `N` rather than in the global
|
| 329 |
namespace.
|
| 330 |
|
| 331 |
+
— *end example*]
|
| 332 |
+
|
| 333 |
If a function template `f` is called in a way that requires a default
|
| 334 |
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 ([[expr.prim.lambda.closure]]) – and
|
| 341 |
+
therefore its associated namespaces – remain as determined from the
|
| 342 |
+
context of the definition for the default argument. This analysis is
|
| 343 |
+
called *default argument instantiation*. The instantiated default
|
| 344 |
+
argument is then used as the argument of `f`.
|
| 345 |
|
| 346 |
Each default argument is instantiated independently.
|
| 347 |
|
| 348 |
+
[*Example 7*:
|
| 349 |
+
|
| 350 |
``` cpp
|
| 351 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 352 |
|
| 353 |
class A { };
|
| 354 |
|
|
|
|
| 359 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 360 |
f(a); // ill-formed; ydef is not declared
|
| 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 ([[except.spec]]). If such an *noexcept-specifier* is
|
| 369 |
needed but has not yet been instantiated, the dependent names are looked
|
| 370 |
up, the semantics constraints are checked, and the instantiation of any
|
| 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 3*: [[temp.point]] defines the point of instantiation of a
|
| 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 ([[implimits]]), which
|
| 380 |
+
could involve more than one template. The result of an infinite
|
| 381 |
+
recursion in instantiation is undefined.
|
| 382 |
+
|
| 383 |
+
[*Example 8*:
|
| 384 |
|
| 385 |
``` cpp
|
| 386 |
template<class T> class X {
|
| 387 |
X<T>* p; // OK
|
| 388 |
X<T*> a; // implicit generation of X<T> requires
|
| 389 |
// the implicit instantiation of X<T*> which requires
|
| 390 |
+
// the implicit instantiation of X<T**> which …
|
| 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
|
|
|
|
| 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*. If the explicit instantiation is for a function or
|
| 420 |
member function, the *unqualified-id* in the *declaration* shall be
|
| 421 |
either a *template-id* or, where all template arguments can be deduced,
|
| 422 |
+
a *template-name* or *operator-function-id*.
|
| 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 declaration shall be a
|
| 433 |
+
*template-id*. An explicit instantiation shall appear in an enclosing
|
| 434 |
+
namespace of its template. If the name declared in the explicit
|
| 435 |
+
instantiation is an unqualified name, the explicit instantiation shall
|
| 436 |
+
appear in the namespace where its template is declared or, if that
|
| 437 |
+
namespace is inline ([[namespace.def]]), any namespace from its
|
| 438 |
+
enclosing namespace set.
|
| 439 |
+
|
| 440 |
+
[*Note 2*: Regarding qualified names in declarators, see
|
| 441 |
+
[[dcl.meaning]]. — *end note*]
|
| 442 |
+
|
| 443 |
+
[*Example 1*:
|
| 444 |
|
| 445 |
``` cpp
|
| 446 |
template<class T> class Array { void mf(); };
|
| 447 |
template class Array<char>;
|
| 448 |
template void Array<int>::mf();
|
| 449 |
|
| 450 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 451 |
template void sort(Array<char>&); // argument is deduced here
|
| 452 |
|
| 453 |
namespace N {
|
| 454 |
template<class T> void f(T&) { }
|
| 455 |
}
|
| 456 |
template void N::f<int>(int&);
|
| 457 |
```
|
| 458 |
|
| 459 |
+
— *end example*]
|
| 460 |
+
|
| 461 |
A declaration of a function template, a variable template, a member
|
| 462 |
function or static data member of a class template, or a member function
|
| 463 |
template of a class or class template shall precede an explicit
|
| 464 |
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
|
|
|
|
| 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 2*:
|
| 489 |
+
|
| 490 |
``` cpp
|
| 491 |
namespace N {
|
| 492 |
template<class T> class Y { void mf() { } };
|
| 493 |
}
|
| 494 |
|
| 495 |
+
template class Y<int>; // error: class template Y not visible in the global namespace
|
|
|
|
| 496 |
|
| 497 |
using N::Y;
|
| 498 |
+
template class Y<int>; // error: explicit instantiation outside of the namespace of the template
|
|
|
|
| 499 |
|
| 500 |
template class N::Y<char*>; // OK: explicit instantiation in namespace N
|
| 501 |
+
template void N::Y<double>::mf(); // OK: explicit instantiation in namespace N
|
|
|
|
| 502 |
```
|
| 503 |
|
| 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 ([[temp.deduct]]).
|
| 510 |
|
| 511 |
+
[*Example 3*:
|
| 512 |
+
|
| 513 |
``` cpp
|
| 514 |
+
template<class T> class Array { ... };
|
| 515 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 516 |
|
| 517 |
+
// instantiate sort(Array<int>&) -- template-argument deduced
|
| 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, except as described below.
|
| 529 |
+
|
| 530 |
+
[*Note 3*: In addition, it will typically be an explicit instantiation
|
| 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 |
+
Except for inline functions and variables, declarations with types
|
| 539 |
+
deduced from their initializer or return value ([[dcl.spec.auto]]),
|
| 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 |
instantiation ([[temp.inst]]) in the translation unit shall be the
|
| 557 |
subject of an explicit instantiation definition somewhere in the
|
| 558 |
program; otherwise the program is ill-formed, no diagnostic required.
|
| 559 |
+
|
| 560 |
+
[*Note 5*: This rule does apply to inline functions even though an
|
| 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 4*:
|
| 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.
|
| 588 |
```
|
| 589 |
|
| 590 |
+
— *end example*]
|
| 591 |
+
|
| 592 |
### Explicit specialization <a id="temp.expl.spec">[[temp.expl.spec]]</a>
|
| 593 |
|
| 594 |
An explicit specialization of any of the following:
|
| 595 |
|
| 596 |
- function template
|
|
|
|
| 608 |
``` bnf
|
| 609 |
explicit-specialization:
|
| 610 |
'template < >' declaration
|
| 611 |
```
|
| 612 |
|
| 613 |
+
[*Example 1*:
|
| 614 |
+
|
| 615 |
``` cpp
|
| 616 |
template<class T> class stream;
|
| 617 |
|
| 618 |
+
template<> class stream<char> { ... };
|
| 619 |
|
| 620 |
+
template<class T> class Array { ... };
|
| 621 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 622 |
|
| 623 |
template<> void sort<char*>(Array<char*>&);
|
| 624 |
```
|
| 625 |
|
| 626 |
Given these declarations, `stream<char>` will be used as the definition
|
|
|
|
| 628 |
specializations instantiated from the class template. Similarly,
|
| 629 |
`sort<char*>` will be used as the sort function for arguments of type
|
| 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
|
| 640 |
template being explicitly specialized shall precede the declaration of
|
| 641 |
+
the explicit specialization.
|
| 642 |
+
|
| 643 |
+
[*Note 1*: A declaration, but not a definition of the template is
|
| 644 |
+
required. — *end note*]
|
| 645 |
+
|
| 646 |
+
The definition of a class or class template shall precede the
|
| 647 |
+
declaration of an explicit specialization for a member template of the
|
| 648 |
+
class or class template.
|
| 649 |
+
|
| 650 |
+
[*Example 2*:
|
| 651 |
|
| 652 |
``` cpp
|
| 653 |
+
template<> class X<int> { ... }; // error: X not a template
|
| 654 |
|
| 655 |
template<class T> class X;
|
| 656 |
|
| 657 |
+
template<> class X<char*> { ... }; // OK: X is a template
|
| 658 |
```
|
| 659 |
|
| 660 |
+
— *end example*]
|
| 661 |
+
|
| 662 |
A member function, a member function template, a member class, a member
|
| 663 |
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
|
|
|
|
| 683 |
syntax. The same is true when defining a member of an explicitly
|
| 684 |
specialized member class. However, `template<>` is used in defining a
|
| 685 |
member of an explicitly specialized member class template that is
|
| 686 |
specialized as a class template.
|
| 687 |
|
| 688 |
+
[*Example 3*:
|
| 689 |
+
|
| 690 |
``` cpp
|
| 691 |
template<class T> struct A {
|
| 692 |
struct B { };
|
| 693 |
template<class U> struct C { };
|
| 694 |
};
|
|
|
|
| 700 |
void h() {
|
| 701 |
A<int> a;
|
| 702 |
a.f(16); // A<int>::f must be defined somewhere
|
| 703 |
}
|
| 704 |
|
| 705 |
+
// template<> not used for a member of an explicitly specialized class template
|
| 706 |
+
void A<int>::f(int) { ... }
|
|
|
|
| 707 |
|
| 708 |
template<> struct A<char>::B {
|
| 709 |
void f();
|
| 710 |
};
|
| 711 |
+
// template<> also not used when defining a member of an explicitly specialized member class
|
| 712 |
+
void A<char>::B::f() { ... }
|
|
|
|
| 713 |
|
| 714 |
template<> template<class U> struct A<char>::C {
|
| 715 |
void f();
|
| 716 |
};
|
| 717 |
+
// template<> is used when defining a member of an explicitly specialized member class template
|
| 718 |
+
// specialized as a class template
|
| 719 |
template<>
|
| 720 |
+
template<class U> void A<char>::C<U>::f() { ... }
|
| 721 |
|
| 722 |
template<> struct A<short>::B {
|
| 723 |
void f();
|
| 724 |
};
|
| 725 |
+
template<> void A<short>::B::f() { ... } // error: template<> not permitted
|
| 726 |
|
| 727 |
template<> template<class U> struct A<short>::C {
|
| 728 |
void f();
|
| 729 |
};
|
| 730 |
+
template<class U> void A<short>::C<U>::f() { ... } // error: template<> required
|
| 731 |
```
|
| 732 |
|
| 733 |
+
— *end example*]
|
| 734 |
+
|
| 735 |
If a template, a member template or a member of a class template is
|
| 736 |
explicitly specialized then that specialization shall be declared before
|
| 737 |
the first use of that specialization that would cause an implicit
|
| 738 |
instantiation to take place, in every translation unit in which such a
|
| 739 |
use occurs; no diagnostic is required. If the program does not provide a
|
|
|
|
| 742 |
place or the member is a virtual member function, the program is
|
| 743 |
ill-formed, no diagnostic required. An implicit instantiation is never
|
| 744 |
generated for an explicit specialization that is declared but not
|
| 745 |
defined.
|
| 746 |
|
| 747 |
+
[*Example 4*:
|
| 748 |
+
|
| 749 |
``` cpp
|
| 750 |
class String { };
|
| 751 |
+
template<class T> class Array { ... };
|
| 752 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 753 |
|
| 754 |
void f(Array<String>& v) {
|
| 755 |
+
sort(v); // use primary template sort(Array<T>&), T is String
|
|
|
|
| 756 |
}
|
| 757 |
|
| 758 |
+
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
|
|
|
| 759 |
template<> void sort<>(Array<char*>& v); // OK: sort<char*> not yet used
|
| 760 |
template<class T> struct A {
|
| 761 |
enum E : T;
|
| 762 |
enum class S : T;
|
| 763 |
};
|
|
|
|
| 768 |
template<> enum A<char>::E : char { echar }; // ill-formed, A<char>::E was instantiated
|
| 769 |
// when A<char> was instantiated
|
| 770 |
template<> enum class A<char>::S : char { schar }; // OK
|
| 771 |
```
|
| 772 |
|
| 773 |
+
— *end example*]
|
| 774 |
+
|
| 775 |
The placement of explicit specialization declarations for function
|
| 776 |
templates, class templates, variable templates, member functions of
|
| 777 |
class templates, static data members of class templates, member classes
|
| 778 |
of class templates, member enumerations of class templates, member class
|
| 779 |
templates of class templates, member function templates of class
|
|
|
|
| 792 |
it compile will be such a trial as to kindle its self-immolation.
|
| 793 |
|
| 794 |
A template explicit specialization is in the scope of the namespace in
|
| 795 |
which the template was defined.
|
| 796 |
|
| 797 |
+
[*Example 5*:
|
| 798 |
+
|
| 799 |
``` cpp
|
| 800 |
namespace N {
|
| 801 |
+
template<class T> class X { ... };
|
| 802 |
+
template<class T> class Y { ... };
|
| 803 |
|
| 804 |
+
template<> class X<int> { ... }; // OK: specialization in same namespace
|
| 805 |
+
template<> class Y<double>; // forward-declare intent to specialize for double
|
|
|
|
|
|
|
| 806 |
}
|
| 807 |
|
| 808 |
+
template<> class N::Y<double> { ... }; // OK: specialization in enclosing namespace
|
| 809 |
+
template<> class N::Y<short> { ... }; // OK: specialization in enclosing namespace
|
|
|
|
|
|
|
| 810 |
```
|
| 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
|
| 823 |
template<> class X<int>;
|
| 824 |
|
| 825 |
X<int>* p; // OK: pointer to declared class X<int>
|
| 826 |
X<int> x; // error: object of incomplete class X<int>
|
| 827 |
```
|
| 828 |
|
| 829 |
+
— *end example*]
|
| 830 |
+
|
| 831 |
A trailing *template-argument* can be left unspecified in the
|
| 832 |
*template-id* naming an explicit function template specialization
|
| 833 |
provided it can be deduced from the function argument type.
|
| 834 |
|
| 835 |
+
[*Example 7*:
|
| 836 |
+
|
| 837 |
``` cpp
|
| 838 |
+
template<class T> class Array { ... };
|
| 839 |
template<class T> void sort(Array<T>& v);
|
| 840 |
|
| 841 |
// explicit specialization for sort(Array<int>&)
|
| 842 |
// with deduced template-argument of type int
|
| 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 ([[temp.fct]]).
|
| 851 |
|
| 852 |
+
An explicit specialization of a function or variable template is inline
|
| 853 |
+
only if it is declared with the `inline` specifier or defined as
|
| 854 |
+
deleted, and independently of whether its function or variable template
|
| 855 |
+
is inline.
|
| 856 |
+
|
| 857 |
+
[*Example 8*:
|
| 858 |
|
| 859 |
``` cpp
|
| 860 |
+
template<class T> void f(T) { ... }
|
| 861 |
+
template<class T> inline T g(T) { ... }
|
| 862 |
|
| 863 |
+
template<> inline void f<>(int) { ... } // OK: inline
|
| 864 |
+
template<> int g<>(int) { ... } // OK: not inline
|
| 865 |
```
|
| 866 |
|
| 867 |
+
— *end example*]
|
| 868 |
+
|
| 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 2*:
|
| 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
|
| 880 |
template<> X Q<int>::x; // declaration
|
| 881 |
template<> X Q<int>::x (); // error: declares a function
|
| 882 |
template<> X Q<int>::x { }; // definition
|
| 883 |
```
|
| 884 |
|
| 885 |
+
— *end note*]
|
| 886 |
+
|
| 887 |
A member or a member template of a class template may be explicitly
|
| 888 |
specialized for a given implicit instantiation of the class template,
|
| 889 |
even if the member or member template is defined in the class template
|
| 890 |
definition. An explicit specialization of a member or member template is
|
| 891 |
specified using the syntax for explicit specialization.
|
| 892 |
|
| 893 |
+
[*Example 9*:
|
| 894 |
+
|
| 895 |
``` cpp
|
| 896 |
template<class T> struct A {
|
| 897 |
void f(T);
|
| 898 |
template<class X1> void g1(T, X1);
|
| 899 |
template<class X2> void g2(T, X2);
|
|
|
|
| 917 |
|
| 918 |
// member specialization even if defined in class definition
|
| 919 |
template<> void A<int>::h(int) { }
|
| 920 |
```
|
| 921 |
|
| 922 |
+
— *end example*]
|
| 923 |
+
|
| 924 |
A member or a member template may be nested within many enclosing class
|
| 925 |
templates. In an explicit specialization for such a member, the member
|
| 926 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 927 |
template that is explicitly specialized.
|
| 928 |
|
| 929 |
+
[*Example 10*:
|
| 930 |
+
|
| 931 |
``` cpp
|
| 932 |
template<class T1> class A {
|
| 933 |
template<class T2> class B {
|
| 934 |
void mf();
|
| 935 |
};
|
| 936 |
};
|
| 937 |
template<> template<> class A<int>::B<double>;
|
| 938 |
template<> template<> void A<char>::B<char>::mf();
|
| 939 |
```
|
| 940 |
|
| 941 |
+
— *end example*]
|
| 942 |
+
|
| 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.
|
| 954 |
|
| 955 |
+
[*Example 11*:
|
| 956 |
+
|
| 957 |
``` cpp
|
| 958 |
template <class T1> class A {
|
| 959 |
template<class T2> class B {
|
| 960 |
template<class T3> void mf1(T3);
|
| 961 |
void mf2();
|
|
|
|
| 970 |
template <class Y> template <>
|
| 971 |
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
|
| 972 |
// its enclosing class template A is not
|
| 973 |
```
|
| 974 |
|
| 975 |
+
— *end example*]
|
| 976 |
+
|
| 977 |
A specialization of a member function template, member class template,
|
| 978 |
or static data member template of a non-specialized class template is
|
| 979 |
itself a template.
|
| 980 |
|
| 981 |
An explicit specialization declaration shall not be a friend
|
|
|
|
| 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 3*: Default
|
| 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 |
|