- tmp/tmpzybbk7yq/{from.md → to.md} +132 -214
tmp/tmpzybbk7yq/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 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
|
|
@@ -20,16 +22,16 @@ 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
|
| 27 |
-
|
| 28 |
specialization declaration for a function template or a member function
|
| 29 |
-
template, the
|
| 30 |
-
|
| 31 |
|
| 32 |
[*Example 1*:
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
template<class T = int> struct A {
|
|
@@ -65,23 +67,24 @@ For a given template and a given set of *template-argument*s,
|
|
| 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 |
-
|
| 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)
|
| 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 |
|
|
@@ -167,13 +170,14 @@ void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
|
| 167 |
}
|
| 168 |
```
|
| 169 |
|
| 170 |
— *end example*]
|
| 171 |
|
| 172 |
-
If
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
|
| 176 |
[*Example 2*:
|
| 177 |
|
| 178 |
``` cpp
|
| 179 |
template<class T> class X;
|
|
@@ -219,21 +223,21 @@ template<> void C<int>::g() { } // error: redefinition of C<int>::g
|
|
| 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]],
|
| 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;
|
| 233 |
template<class Y> struct Inner<T, Y>; // #1a
|
| 234 |
-
template<class Y> struct Inner<T, Y> { }; // #1b; OK
|
| 235 |
template<class Y> struct Inner<U, Y> { }; // #2
|
| 236 |
};
|
| 237 |
|
| 238 |
Outer<int, int> outer; // error at #2
|
| 239 |
```
|
|
@@ -252,19 +256,18 @@ 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
|
| 258 |
-
specialization
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
static data member
|
| 264 |
-
|
| 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
|
|
@@ -279,11 +282,11 @@ 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 |
-
[[
|
| 285 |
that no out-of-line copy of it be generated in the translation
|
| 286 |
unit. — *end note*]
|
| 287 |
|
| 288 |
[*Example 5*:
|
| 289 |
|
|
@@ -367,11 +370,11 @@ 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
|
| 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
|
|
@@ -379,67 +382,36 @@ instantiation of substatements of a constexpr if statement within its
|
|
| 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
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
function call causes specializations in the default argument to be
|
| 389 |
-
implicitly instantiated.
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
-
|
| 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:
|
| 404 |
-
T* get();
|
| 405 |
-
};
|
| 406 |
-
}
|
| 407 |
-
|
| 408 |
-
template<class K, class V> class Map {
|
| 409 |
-
public:
|
| 410 |
-
N::List<V> lt;
|
| 411 |
-
V get(K);
|
| 412 |
-
};
|
| 413 |
-
|
| 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 |
-
|
| 425 |
-
If a function template `f` is called in a way that requires a default
|
| 426 |
argument to be used, the dependent names are looked up, the semantics
|
| 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]]
|
| 433 |
-
its associated namespaces
|
| 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
|
| 441 |
|
| 442 |
``` cpp
|
| 443 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 444 |
|
| 445 |
class A { };
|
|
@@ -470,11 +442,11 @@ template specialization. — *end note*]
|
|
| 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
|
| 476 |
|
| 477 |
``` cpp
|
| 478 |
template<class T> class X {
|
| 479 |
X<T>* p; // OK
|
| 480 |
X<T*> a; // implicit generation of X<T> requires
|
|
@@ -496,11 +468,11 @@ 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
|
| 502 |
|
| 503 |
``` cpp
|
| 504 |
template<typename T> concept C = sizeof(T) > 2;
|
| 505 |
template<typename T> concept D = C<T> && sizeof(T) > 4;
|
| 506 |
|
|
@@ -518,11 +490,11 @@ 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
|
| 524 |
|
| 525 |
``` cpp
|
| 526 |
template<typename T> struct S1 {
|
| 527 |
template<typename U>
|
| 528 |
requires false
|
|
@@ -572,33 +544,12 @@ appertain to an explicit instantiation.
|
|
| 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
|
| 578 |
-
in the *declarator* shall be
|
| 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*:
|
| 602 |
|
| 603 |
``` cpp
|
| 604 |
template<class T> class Array { void mf(); };
|
|
@@ -614,26 +565,28 @@ namespace N {
|
|
| 614 |
template void N::f<int>(int&);
|
| 615 |
```
|
| 616 |
|
| 617 |
— *end example*]
|
| 618 |
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
template
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
class template
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
|
|
|
|
|
|
| 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
|
| 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*:
|
|
@@ -667,41 +620,17 @@ 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
|
| 673 |
-
template specialization is placed in the namespace in which the template
|
| 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 |
-
}
|
| 685 |
-
|
| 686 |
-
template class Y<int>; // error: class template Y not visible in the global namespace
|
| 687 |
-
|
| 688 |
-
using N::Y;
|
| 689 |
-
template class Y<int>; // error: explicit instantiation outside of the namespace of the template
|
| 690 |
-
|
| 691 |
-
template class N::Y<char*>; // OK: explicit instantiation in namespace N
|
| 692 |
-
template void N::Y<double>::mf(); // OK: explicit instantiation in namespace N
|
| 693 |
-
```
|
| 694 |
-
|
| 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
|
| 700 |
-
|
|
|
|
| 701 |
|
| 702 |
-
[*Example
|
| 703 |
|
| 704 |
``` cpp
|
| 705 |
template<class T> class Array { ... };
|
| 706 |
template<class T> void sort(Array<T>& v) { ... }
|
| 707 |
|
|
@@ -709,50 +638,49 @@ template<class T> void sort(Array<T>& v) { ... }
|
|
| 709 |
template void sort<>(Array<int>&);
|
| 710 |
```
|
| 711 |
|
| 712 |
— *end example*]
|
| 713 |
|
| 714 |
-
[*Note
|
| 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
|
| 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
|
| 731 |
except as described below.
|
| 732 |
|
| 733 |
-
[*Note
|
| 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
|
| 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
|
| 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*]
|
|
@@ -761,11 +689,11 @@ 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
|
| 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.
|
|
@@ -814,65 +742,67 @@ specializations instantiated from the class template. Similarly,
|
|
| 814 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 815 |
generated from the template.
|
| 816 |
|
| 817 |
— *end example*]
|
| 818 |
|
| 819 |
-
|
| 820 |
-
|
|
|
|
| 821 |
|
| 822 |
An explicit specialization may be declared in any scope in which the
|
| 823 |
-
corresponding primary template may be defined
|
| 824 |
-
[[class.mem]], [[temp.mem]]
|
| 825 |
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
|
|
|
| 829 |
|
| 830 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 831 |
required. — *end note*]
|
| 832 |
|
| 833 |
-
The definition of a class or class template shall
|
| 834 |
declaration of an explicit specialization for a member template of the
|
| 835 |
class or class template.
|
| 836 |
|
| 837 |
[*Example 2*:
|
| 838 |
|
| 839 |
``` cpp
|
| 840 |
template<> class X<int> { ... }; // error: X not a template
|
| 841 |
|
| 842 |
template<class T> class X;
|
| 843 |
|
| 844 |
-
template<> class X<char*> { ... }; // OK
|
| 845 |
```
|
| 846 |
|
| 847 |
— *end example*]
|
| 848 |
|
| 849 |
A member function, a member function template, a member class, a member
|
| 850 |
enumeration, a member class template, a static data member, or a static
|
| 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
|
| 854 |
-
specialization for the member of the class template. If such an
|
| 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.
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
|
| 875 |
[*Example 3*:
|
| 876 |
|
| 877 |
``` cpp
|
| 878 |
template<class T> struct A {
|
|
@@ -918,20 +848,20 @@ template<class U> void A<short>::C<U>::f() { ... } // error: template<> requi
|
|
| 918 |
```
|
| 919 |
|
| 920 |
— *end example*]
|
| 921 |
|
| 922 |
If a template, a member template or a member of a class template is
|
| 923 |
-
explicitly specialized
|
| 924 |
-
|
| 925 |
-
instantiation to take place, in every translation unit in which
|
| 926 |
-
use occurs; no diagnostic is required. If the program does not
|
| 927 |
-
definition for an explicit specialization and either the
|
| 928 |
-
is used in a way that would cause an implicit
|
| 929 |
-
place or the member is a virtual member function,
|
| 930 |
-
ill-formed, no diagnostic required. An implicit
|
| 931 |
-
generated for an explicit specialization that is
|
| 932 |
-
defined.
|
| 933 |
|
| 934 |
[*Example 4*:
|
| 935 |
|
| 936 |
``` cpp
|
| 937 |
class String { };
|
|
@@ -941,11 +871,11 @@ template<class T> void sort(Array<T>& v) { ... }
|
|
| 941 |
void f(Array<String>& v) {
|
| 942 |
sort(v); // use primary template sort(Array<T>&), T is String
|
| 943 |
}
|
| 944 |
|
| 945 |
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
| 946 |
-
template<> void sort<>(Array<char*>& v); // OK
|
| 947 |
template<class T> struct A {
|
| 948 |
enum E : T;
|
| 949 |
enum class S : T;
|
| 950 |
};
|
| 951 |
template<> enum A<int>::E : int { eint }; // OK
|
|
@@ -976,52 +906,32 @@ can affect whether a program is well-formed according to the relative
|
|
| 976 |
positioning of the explicit specialization declarations and their points
|
| 977 |
of instantiation in the translation unit as specified above and below.
|
| 978 |
When writing a specialization, be careful about its location; or to make
|
| 979 |
it compile will be such a trial as to kindle its self-immolation.
|
| 980 |
|
| 981 |
-
A template explicit specialization is in the scope of the namespace in
|
| 982 |
-
which the template was defined.
|
| 983 |
-
|
| 984 |
-
[*Example 5*:
|
| 985 |
-
|
| 986 |
-
``` cpp
|
| 987 |
-
namespace N {
|
| 988 |
-
template<class T> class X { ... };
|
| 989 |
-
template<class T> class Y { ... };
|
| 990 |
-
|
| 991 |
-
template<> class X<int> { ... }; // OK: specialization in same namespace
|
| 992 |
-
template<> class Y<double>; // forward-declare intent to specialize for double
|
| 993 |
-
}
|
| 994 |
-
|
| 995 |
-
template<> class N::Y<double> { ... }; // OK: specialization in enclosing namespace
|
| 996 |
-
template<> class N::Y<short> { ... }; // OK: specialization in enclosing namespace
|
| 997 |
-
```
|
| 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
|
| 1007 |
|
| 1008 |
``` cpp
|
| 1009 |
template<class T> class X; // X is a class template
|
| 1010 |
template<> class X<int>;
|
| 1011 |
|
| 1012 |
-
X<int>* p; // OK
|
| 1013 |
X<int> x; // error: object of incomplete class X<int>
|
| 1014 |
```
|
| 1015 |
|
| 1016 |
— *end example*]
|
| 1017 |
|
| 1018 |
A trailing *template-argument* can be left unspecified in the
|
| 1019 |
*template-id* naming an explicit function template specialization
|
| 1020 |
-
provided it can be deduced
|
| 1021 |
|
| 1022 |
-
[*Example
|
| 1023 |
|
| 1024 |
``` cpp
|
| 1025 |
template<class T> class Array { ... };
|
| 1026 |
template<class T> void sort(Array<T>& v);
|
| 1027 |
|
|
@@ -1044,22 +954,29 @@ arguments are left unspecified. — *end note*]
|
|
| 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
|
| 1050 |
-
|
| 1051 |
-
template
|
|
|
|
| 1052 |
|
| 1053 |
-
[*Example
|
| 1054 |
|
| 1055 |
``` cpp
|
| 1056 |
template<class T> void f(T) { ... }
|
| 1057 |
template<class T> inline T g(T) { ... }
|
| 1058 |
|
| 1059 |
-
template<> inline void f<>(int) { ... } // OK
|
| 1060 |
-
template<> int g<>(int) { ... } // OK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1061 |
```
|
| 1062 |
|
| 1063 |
— *end example*]
|
| 1064 |
|
| 1065 |
An explicit specialization of a static data member of a template or an
|
|
@@ -1067,28 +984,29 @@ 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
|
| 1073 |
-
default-initialization
|
|
|
|
| 1074 |
|
| 1075 |
``` cpp
|
| 1076 |
template<> X Q<int>::x; // declaration
|
| 1077 |
template<> X Q<int>::x (); // error: declares a function
|
| 1078 |
-
template<> X Q<int>::x
|
| 1079 |
```
|
| 1080 |
|
| 1081 |
— *end note*]
|
| 1082 |
|
| 1083 |
A member or a member template of a class template may be explicitly
|
| 1084 |
specialized for a given implicit instantiation of the class template,
|
| 1085 |
even if the member or member template is defined in the class template
|
| 1086 |
definition. An explicit specialization of a member or member template is
|
| 1087 |
specified using the syntax for explicit specialization.
|
| 1088 |
|
| 1089 |
-
[*Example
|
| 1090 |
|
| 1091 |
``` cpp
|
| 1092 |
template<class T> struct A {
|
| 1093 |
void f(T);
|
| 1094 |
template<class X1> void g1(T, X1);
|
|
@@ -1120,11 +1038,11 @@ template<> void A<int>::h(int) { }
|
|
| 1120 |
A member or a member template may be nested within many enclosing class
|
| 1121 |
templates. In an explicit specialization for such a member, the member
|
| 1122 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 1123 |
template that is explicitly specialized.
|
| 1124 |
|
| 1125 |
-
[*Example
|
| 1126 |
|
| 1127 |
``` cpp
|
| 1128 |
template<class T1> class A {
|
| 1129 |
template<class T2> class B {
|
| 1130 |
void mf();
|
|
@@ -1146,11 +1064,11 @@ 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.
|
| 1150 |
|
| 1151 |
-
[*Example
|
| 1152 |
|
| 1153 |
``` cpp
|
| 1154 |
template <class T1> class A {
|
| 1155 |
template<class T2> class B {
|
| 1156 |
template<class T3> void mf1(T3);
|
|
@@ -1183,9 +1101,9 @@ definition for one of the following explicit specializations:
|
|
| 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
|
| 1189 |
of a member function of a class template specialization that is
|
| 1190 |
explicitly specialized. — *end note*]
|
| 1191 |
|
|
|
|
| 1 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="temp.spec.general">[[temp.spec.general]]</a>
|
| 4 |
+
|
| 5 |
The act of instantiating a function, a variable, a class, a member of a
|
| 6 |
class template, or a member template is referred to as *template
|
| 7 |
instantiation*.
|
| 8 |
|
| 9 |
A function instantiated from a function template is called an
|
|
|
|
| 22 |
|
| 23 |
An explicit specialization may be declared for a function template, a
|
| 24 |
variable template, a class template, a member of a class template, or a
|
| 25 |
member template. An explicit specialization declaration is introduced by
|
| 26 |
`template<>`. In an explicit specialization declaration for a variable
|
| 27 |
+
template, a class template, a member of a class template, or a class
|
| 28 |
+
member template, the variable or class that is explicitly specialized
|
| 29 |
+
shall be specified with a *simple-template-id*. In the explicit
|
| 30 |
specialization declaration for a function template or a member function
|
| 31 |
+
template, the function or member function explicitly specialized may be
|
| 32 |
+
specified using a *template-id*.
|
| 33 |
|
| 34 |
[*Example 1*:
|
| 35 |
|
| 36 |
``` cpp
|
| 37 |
template<class T = int> struct A {
|
|
|
|
| 67 |
program,
|
| 68 |
- an explicit specialization shall be defined at most once in a program,
|
| 69 |
as specified in [[basic.def.odr]], and
|
| 70 |
- both an explicit instantiation and a declaration of an explicit
|
| 71 |
specialization shall not appear in a program unless the explicit
|
| 72 |
+
specialization is reachable from the explicit instantiation.
|
| 73 |
|
| 74 |
+
An implementation is not required to diagnose a violation of this rule
|
| 75 |
+
if neither declaration is reachable from the other.
|
| 76 |
|
| 77 |
The usual access checking rules do not apply to names in a declaration
|
| 78 |
of an explicit instantiation or explicit specialization, with the
|
| 79 |
exception of names appearing in a function body, default argument,
|
| 80 |
+
*base-clause*, *member-specification*, *enumerator-list*, or static data
|
| 81 |
member or variable template initializer.
|
| 82 |
|
| 83 |
[*Note 1*: In particular, the template arguments and names used in the
|
| 84 |
function declarator (including parameter types, return types and
|
| 85 |
+
exception specifications) can be private types or objects that would
|
| 86 |
normally not be accessible. — *end note*]
|
| 87 |
|
| 88 |
Each class template specialization instantiated from a template has its
|
| 89 |
own copy of any static members.
|
| 90 |
|
|
|
|
| 170 |
}
|
| 171 |
```
|
| 172 |
|
| 173 |
— *end example*]
|
| 174 |
|
| 175 |
+
If the template selected for the specialization
|
| 176 |
+
[[temp.spec.partial.match]] has been declared, but not defined, at the
|
| 177 |
+
point of instantiation [[temp.point]], the instantiation yields an
|
| 178 |
+
incomplete class type [[term.incomplete.type]].
|
| 179 |
|
| 180 |
[*Example 2*:
|
| 181 |
|
| 182 |
``` cpp
|
| 183 |
template<class T> class X;
|
|
|
|
| 223 |
|
| 224 |
— *end example*]
|
| 225 |
|
| 226 |
However, for the purpose of determining whether an instantiated
|
| 227 |
redeclaration is valid according to [[basic.def.odr]] and
|
| 228 |
+
[[class.mem]], an instantiated declaration that corresponds to a
|
| 229 |
+
definition in the template is considered to be a definition.
|
| 230 |
|
| 231 |
[*Example 4*:
|
| 232 |
|
| 233 |
``` cpp
|
| 234 |
template<class T, class U>
|
| 235 |
struct Outer {
|
| 236 |
template<class X, class Y> struct Inner;
|
| 237 |
template<class Y> struct Inner<T, Y>; // #1a
|
| 238 |
+
template<class Y> struct Inner<T, Y> { }; // #1b; OK, valid redeclaration of #1a
|
| 239 |
template<class Y> struct Inner<U, Y> { }; // #2
|
| 240 |
};
|
| 241 |
|
| 242 |
Outer<int, int> outer; // error at #2
|
| 243 |
```
|
|
|
|
| 256 |
Friendly<float> ff; // error: produces second definition of f(U)
|
| 257 |
```
|
| 258 |
|
| 259 |
— *end example*]
|
| 260 |
|
| 261 |
+
Unless a member of a templated class is a declared specialization, the
|
| 262 |
+
specialization of the member is implicitly instantiated when the
|
| 263 |
+
specialization is referenced in a context that requires the member
|
| 264 |
+
definition to exist or if the existence of the definition of the member
|
| 265 |
+
affects the semantics of the program; in particular, the initialization
|
| 266 |
+
(and any associated side effects) of a static data member does not occur
|
| 267 |
+
unless the static data member is itself used in a way that requires the
|
| 268 |
+
definition of the static data member to exist.
|
|
|
|
| 269 |
|
| 270 |
Unless a function template specialization is a declared specialization,
|
| 271 |
the function template specialization is implicitly instantiated when the
|
| 272 |
specialization is referenced in a context that requires a function
|
| 273 |
definition to exist or if the existence of the definition affects the
|
|
|
|
| 282 |
context that requires the value of the default argument.
|
| 283 |
|
| 284 |
[*Note 4*: An inline function that is the subject of an explicit
|
| 285 |
instantiation declaration is not a declared specialization; the intent
|
| 286 |
is that it still be implicitly instantiated when odr-used
|
| 287 |
+
[[term.odr.use]] so that the body can be considered for inlining, but
|
| 288 |
that no out-of-line copy of it be generated in the translation
|
| 289 |
unit. — *end note*]
|
| 290 |
|
| 291 |
[*Example 5*:
|
| 292 |
|
|
|
|
| 370 |
used in a way that involves overload resolution, a declaration of the
|
| 371 |
specialization is implicitly instantiated [[temp.over]].
|
| 372 |
|
| 373 |
An implementation shall not implicitly instantiate a function template,
|
| 374 |
a variable template, a member template, a non-virtual member function, a
|
| 375 |
+
member class or static data member of a templated class, or a
|
| 376 |
substatement of a constexpr if statement [[stmt.if]], unless such
|
| 377 |
instantiation is required.
|
| 378 |
|
| 379 |
[*Note 5*: The instantiation of a generic lambda does not require
|
| 380 |
instantiation of substatements of a constexpr if statement within its
|
|
|
|
| 382 |
instantiated. — *end note*]
|
| 383 |
|
| 384 |
It is unspecified whether or not an implementation implicitly
|
| 385 |
instantiates a virtual member function of a class template if the
|
| 386 |
virtual member function would not otherwise be instantiated. The use of
|
| 387 |
+
a template specialization in a default argument or default member
|
| 388 |
+
initializer shall not cause the template to be implicitly instantiated
|
| 389 |
+
except where needed to determine the correctness of the default argument
|
| 390 |
+
or default member initializer. The use of a default argument in a
|
| 391 |
function call causes specializations in the default argument to be
|
| 392 |
+
implicitly instantiated. Similarly, the use of a default member
|
| 393 |
+
initializer in a constructor definition or an aggregate initialization
|
| 394 |
+
causes specializations in the default member initializer to be
|
| 395 |
+
instantiated.
|
| 396 |
|
| 397 |
+
If a templated function `f` is called in a way that requires a default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
argument to be used, the dependent names are looked up, the semantics
|
| 399 |
constraints are checked, and the instantiation of any template used in
|
| 400 |
the default argument is done as if the default argument had been an
|
| 401 |
initializer used in a function template specialization with the same
|
| 402 |
scope, the same template parameters and the same access as that of the
|
| 403 |
function template `f` used at that point, except that the scope in which
|
| 404 |
+
a closure type is declared [[expr.prim.lambda.closure]] — and therefore
|
| 405 |
+
its associated namespaces — remain as determined from the context of the
|
| 406 |
definition for the default argument. This analysis is called *default
|
| 407 |
argument instantiation*. The instantiated default argument is then used
|
| 408 |
as the argument of `f`.
|
| 409 |
|
| 410 |
Each default argument is instantiated independently.
|
| 411 |
|
| 412 |
+
[*Example 8*:
|
| 413 |
|
| 414 |
``` cpp
|
| 415 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 416 |
|
| 417 |
class A { };
|
|
|
|
| 442 |
There is an *implementation-defined* quantity that specifies the limit
|
| 443 |
on the total depth of recursive instantiations [[implimits]], which
|
| 444 |
could involve more than one template. The result of an infinite
|
| 445 |
recursion in instantiation is undefined.
|
| 446 |
|
| 447 |
+
[*Example 9*:
|
| 448 |
|
| 449 |
``` cpp
|
| 450 |
template<class T> class X {
|
| 451 |
X<T>* p; // OK
|
| 452 |
X<T*> a; // implicit generation of X<T> requires
|
|
|
|
| 468 |
|
| 469 |
[*Note 7*: The satisfaction of constraints is determined during
|
| 470 |
template argument deduction [[temp.deduct]] and overload resolution
|
| 471 |
[[over.match]]. — *end note*]
|
| 472 |
|
| 473 |
+
[*Example 10*:
|
| 474 |
|
| 475 |
``` cpp
|
| 476 |
template<typename T> concept C = sizeof(T) > 2;
|
| 477 |
template<typename T> concept D = C<T> && sizeof(T) > 4;
|
| 478 |
|
|
|
|
| 490 |
the implicit declaration of a default constructor for `S<char>`
|
| 491 |
[[class.default.ctor]], so there is no viable constructor for `s1`.
|
| 492 |
|
| 493 |
— *end example*]
|
| 494 |
|
| 495 |
+
[*Example 11*:
|
| 496 |
|
| 497 |
``` cpp
|
| 498 |
template<typename T> struct S1 {
|
| 499 |
template<typename U>
|
| 500 |
requires false
|
|
|
|
| 544 |
If the explicit instantiation is for a class or member class, the
|
| 545 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 546 |
*simple-template-id*; otherwise, the *declaration* shall be a
|
| 547 |
*simple-declaration* whose *init-declarator-list* comprises a single
|
| 548 |
*init-declarator* that does not have an *initializer*. If the explicit
|
| 549 |
+
instantiation is for a variable template specialization, the
|
| 550 |
+
*unqualified-id* in the *declarator* shall be a *simple-template-id*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
|
| 552 |
[*Example 1*:
|
| 553 |
|
| 554 |
``` cpp
|
| 555 |
template<class T> class Array { void mf(); };
|
|
|
|
| 565 |
template void N::f<int>(int&);
|
| 566 |
```
|
| 567 |
|
| 568 |
— *end example*]
|
| 569 |
|
| 570 |
+
An explicit instantiation does not introduce a name
|
| 571 |
+
[[basic.scope.scope]]. A declaration of a function template, a variable
|
| 572 |
+
template, a member function or static data member of a class template,
|
| 573 |
+
or a member function template of a class or class template shall be
|
| 574 |
+
reachable from any explicit instantiation of that entity. A definition
|
| 575 |
+
of a class template, a member class of a class template, or a member
|
| 576 |
+
class template of a class or class template shall be reachable from any
|
| 577 |
+
explicit instantiation of that entity unless an explicit specialization
|
| 578 |
+
of the entity with the same template arguments is reachable therefrom.
|
| 579 |
+
If the *declaration* of the explicit instantiation names an
|
| 580 |
+
implicitly-declared special member function [[special]], the program is
|
| 581 |
+
ill-formed.
|
| 582 |
|
| 583 |
The *declaration* in an *explicit-instantiation* and the *declaration*
|
| 584 |
produced by the corresponding substitution into the templated function,
|
| 585 |
variable, or class are two declarations of the same entity.
|
| 586 |
|
| 587 |
+
[*Note 1*:
|
| 588 |
|
| 589 |
These declarations are required to have matching types as specified in
|
| 590 |
[[basic.link]], except as specified in [[except.spec]].
|
| 591 |
|
| 592 |
[*Example 2*:
|
|
|
|
| 620 |
an explicit instantiation definition, the definition of a function
|
| 621 |
template, a variable template, a member function template, or a member
|
| 622 |
function or static data member of a class template shall be present in
|
| 623 |
every translation unit in which it is explicitly instantiated.
|
| 624 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 626 |
instantiation of a function template specialization or of a member
|
| 627 |
+
function template specialization provided it can be deduced
|
| 628 |
+
[[temp.deduct.decl]]. If all template arguments can be deduced, the
|
| 629 |
+
empty template argument list `<>` may be omitted.
|
| 630 |
|
| 631 |
+
[*Example 3*:
|
| 632 |
|
| 633 |
``` cpp
|
| 634 |
template<class T> class Array { ... };
|
| 635 |
template<class T> void sort(Array<T>& v) { ... }
|
| 636 |
|
|
|
|
| 638 |
template void sort<>(Array<int>&);
|
| 639 |
```
|
| 640 |
|
| 641 |
— *end example*]
|
| 642 |
|
| 643 |
+
[*Note 2*: An explicit instantiation of a constrained template is
|
| 644 |
required to satisfy that template’s associated constraints
|
| 645 |
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 646 |
forming the template name of an explicit instantiation in which all
|
| 647 |
template arguments are specified [[temp.names]], or, for explicit
|
| 648 |
instantiations of function templates, during template argument deduction
|
| 649 |
[[temp.deduct.decl]] when one or more trailing template arguments are
|
| 650 |
left unspecified. — *end note*]
|
| 651 |
|
| 652 |
An explicit instantiation that names a class template specialization is
|
| 653 |
also an explicit instantiation of the same kind (declaration or
|
| 654 |
+
definition) of each of its direct non-template members that has not been
|
|
|
|
| 655 |
previously explicitly specialized in the translation unit containing the
|
| 656 |
explicit instantiation, provided that the associated constraints, if
|
| 657 |
any, of that member are satisfied by the template arguments of the
|
| 658 |
+
explicit instantiation [[temp.constr.decl]], [[temp.constr.constr]],
|
| 659 |
except as described below.
|
| 660 |
|
| 661 |
+
[*Note 3*: In addition, it will typically be an explicit instantiation
|
| 662 |
of certain implementation-dependent data about the class. — *end note*]
|
| 663 |
|
| 664 |
An explicit instantiation definition that names a class template
|
| 665 |
specialization explicitly instantiates the class template specialization
|
| 666 |
and is an explicit instantiation definition of only those members that
|
| 667 |
have been defined at the point of instantiation.
|
| 668 |
|
| 669 |
An explicit instantiation of a prospective destructor [[class.dtor]]
|
| 670 |
+
shall correspond to the selected destructor of the class.
|
| 671 |
|
| 672 |
If an entity is the subject of both an explicit instantiation
|
| 673 |
declaration and an explicit instantiation definition in the same
|
| 674 |
translation unit, the definition shall follow the declaration. An entity
|
| 675 |
that is the subject of an explicit instantiation declaration and that is
|
| 676 |
also used in a way that would otherwise cause an implicit instantiation
|
| 677 |
[[temp.inst]] in the translation unit shall be the subject of an
|
| 678 |
explicit instantiation definition somewhere in the program; otherwise
|
| 679 |
the program is ill-formed, no diagnostic required.
|
| 680 |
|
| 681 |
+
[*Note 4*: This rule does apply to inline functions even though an
|
| 682 |
explicit instantiation declaration of such an entity has no other
|
| 683 |
normative effect. This is needed to ensure that if the address of an
|
| 684 |
inline function is taken in a translation unit in which the
|
| 685 |
implementation chose to suppress the out-of-line body, another
|
| 686 |
translation unit will supply the body. — *end note*]
|
|
|
|
| 689 |
a template with internal linkage.
|
| 690 |
|
| 691 |
An explicit instantiation does not constitute a use of a default
|
| 692 |
argument, so default argument instantiation is not done.
|
| 693 |
|
| 694 |
+
[*Example 4*:
|
| 695 |
|
| 696 |
``` cpp
|
| 697 |
char* p = 0;
|
| 698 |
template<class T> T g(T x = &p) { return x; }
|
| 699 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
|
|
| 742 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 743 |
generated from the template.
|
| 744 |
|
| 745 |
— *end example*]
|
| 746 |
|
| 747 |
+
The *declaration* in an *explicit-specialization* shall not be an
|
| 748 |
+
*export-declaration*. An explicit specialization shall not use a
|
| 749 |
+
*storage-class-specifier* [[dcl.stc]] other than `thread_local`.
|
| 750 |
|
| 751 |
An explicit specialization may be declared in any scope in which the
|
| 752 |
+
corresponding primary template may be defined
|
| 753 |
+
[[dcl.meaning]], [[class.mem]], [[temp.mem]].
|
| 754 |
|
| 755 |
+
An explicit specialization does not introduce a name
|
| 756 |
+
[[basic.scope.scope]]. A declaration of a function template, class
|
| 757 |
+
template, or variable template being explicitly specialized shall be
|
| 758 |
+
reachable from the declaration of the explicit specialization.
|
| 759 |
|
| 760 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 761 |
required. — *end note*]
|
| 762 |
|
| 763 |
+
The definition of a class or class template shall be reachable from the
|
| 764 |
declaration of an explicit specialization for a member template of the
|
| 765 |
class or class template.
|
| 766 |
|
| 767 |
[*Example 2*:
|
| 768 |
|
| 769 |
``` cpp
|
| 770 |
template<> class X<int> { ... }; // error: X not a template
|
| 771 |
|
| 772 |
template<class T> class X;
|
| 773 |
|
| 774 |
+
template<> class X<char*> { ... }; // OK, X is a template
|
| 775 |
```
|
| 776 |
|
| 777 |
— *end example*]
|
| 778 |
|
| 779 |
A member function, a member function template, a member class, a member
|
| 780 |
enumeration, a member class template, a static data member, or a static
|
| 781 |
data member template of a class template may be explicitly specialized
|
| 782 |
for a class specialization that is implicitly instantiated; in this
|
| 783 |
+
case, the definition of the class template shall be reachable from the
|
| 784 |
+
explicit specialization for the member of the class template. If such an
|
| 785 |
+
explicit specialization for the member of a class template names an
|
| 786 |
implicitly-declared special member function [[special]], the program is
|
| 787 |
ill-formed.
|
| 788 |
|
| 789 |
A member of an explicitly specialized class is not implicitly
|
| 790 |
instantiated from the member declaration of the class template; instead,
|
| 791 |
the member of the class template specialization shall itself be
|
| 792 |
+
explicitly defined if its definition is required. The definition of the
|
| 793 |
+
class template explicit specialization shall be reachable from the
|
| 794 |
+
definition of any member of it. The definition of an explicitly
|
| 795 |
+
specialized class is unrelated to the definition of a generated
|
| 796 |
+
specialization. That is, its members need not have the same names,
|
| 797 |
+
types, etc. as the members of a generated specialization. Members of an
|
| 798 |
+
explicitly specialized class template are defined in the same manner as
|
| 799 |
+
members of normal classes, and not using the `template<>` syntax. The
|
| 800 |
+
same is true when defining a member of an explicitly specialized member
|
| 801 |
+
class. However, `template<>` is used in defining a member of an
|
| 802 |
+
explicitly specialized member class template that is specialized as a
|
| 803 |
+
class template.
|
| 804 |
|
| 805 |
[*Example 3*:
|
| 806 |
|
| 807 |
``` cpp
|
| 808 |
template<class T> struct A {
|
|
|
|
| 848 |
```
|
| 849 |
|
| 850 |
— *end example*]
|
| 851 |
|
| 852 |
If a template, a member template or a member of a class template is
|
| 853 |
+
explicitly specialized, a declaration of that specialization shall be
|
| 854 |
+
reachable from every use of that specialization that would cause an
|
| 855 |
+
implicit instantiation to take place, in every translation unit in which
|
| 856 |
+
such a use occurs; no diagnostic is required. If the program does not
|
| 857 |
+
provide a definition for an explicit specialization and either the
|
| 858 |
+
specialization is used in a way that would cause an implicit
|
| 859 |
+
instantiation to take place or the member is a virtual member function,
|
| 860 |
+
the program is ill-formed, no diagnostic required. An implicit
|
| 861 |
+
instantiation is never generated for an explicit specialization that is
|
| 862 |
+
declared but not defined.
|
| 863 |
|
| 864 |
[*Example 4*:
|
| 865 |
|
| 866 |
``` cpp
|
| 867 |
class String { };
|
|
|
|
| 871 |
void f(Array<String>& v) {
|
| 872 |
sort(v); // use primary template sort(Array<T>&), T is String
|
| 873 |
}
|
| 874 |
|
| 875 |
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
| 876 |
+
template<> void sort<>(Array<char*>& v); // OK, sort<char*> not yet used
|
| 877 |
template<class T> struct A {
|
| 878 |
enum E : T;
|
| 879 |
enum class S : T;
|
| 880 |
};
|
| 881 |
template<> enum A<int>::E : int { eint }; // OK
|
|
|
|
| 906 |
positioning of the explicit specialization declarations and their points
|
| 907 |
of instantiation in the translation unit as specified above and below.
|
| 908 |
When writing a specialization, be careful about its location; or to make
|
| 909 |
it compile will be such a trial as to kindle its self-immolation.
|
| 910 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 911 |
A *simple-template-id* that names a class template explicit
|
| 912 |
specialization that has been declared but not defined can be used
|
| 913 |
exactly like the names of other incompletely-defined classes
|
| 914 |
[[basic.types]].
|
| 915 |
|
| 916 |
+
[*Example 5*:
|
| 917 |
|
| 918 |
``` cpp
|
| 919 |
template<class T> class X; // X is a class template
|
| 920 |
template<> class X<int>;
|
| 921 |
|
| 922 |
+
X<int>* p; // OK, pointer to declared class X<int>
|
| 923 |
X<int> x; // error: object of incomplete class X<int>
|
| 924 |
```
|
| 925 |
|
| 926 |
— *end example*]
|
| 927 |
|
| 928 |
A trailing *template-argument* can be left unspecified in the
|
| 929 |
*template-id* naming an explicit function template specialization
|
| 930 |
+
provided it can be deduced [[temp.deduct.decl]].
|
| 931 |
|
| 932 |
+
[*Example 6*:
|
| 933 |
|
| 934 |
``` cpp
|
| 935 |
template<class T> class Array { ... };
|
| 936 |
template<class T> void sort(Array<T>& v);
|
| 937 |
|
|
|
|
| 954 |
A function with the same name as a template and a type that exactly
|
| 955 |
matches that of a template specialization is not an explicit
|
| 956 |
specialization [[temp.fct]].
|
| 957 |
|
| 958 |
Whether an explicit specialization of a function or variable template is
|
| 959 |
+
inline, constexpr, constinit, or consteval is determined by the explicit
|
| 960 |
+
specialization and is independent of those properties of the template.
|
| 961 |
+
Similarly, attributes appearing in the declaration of a template have no
|
| 962 |
+
effect on an explicit specialization of that template.
|
| 963 |
|
| 964 |
+
[*Example 7*:
|
| 965 |
|
| 966 |
``` cpp
|
| 967 |
template<class T> void f(T) { ... }
|
| 968 |
template<class T> inline T g(T) { ... }
|
| 969 |
|
| 970 |
+
template<> inline void f<>(int) { ... } // OK, inline
|
| 971 |
+
template<> int g<>(int) { ... } // OK, not inline
|
| 972 |
+
|
| 973 |
+
template<typename> [[noreturn]] void h([[maybe_unused]] int i);
|
| 974 |
+
template<> void h<int>(int i) {
|
| 975 |
+
// Implementations are expected not to warn that the function returns
|
| 976 |
+
// but can warn about the unused parameter.
|
| 977 |
+
}
|
| 978 |
```
|
| 979 |
|
| 980 |
— *end example*]
|
| 981 |
|
| 982 |
An explicit specialization of a static data member of a template or an
|
|
|
|
| 984 |
if the declaration includes an initializer; otherwise, it is a
|
| 985 |
declaration.
|
| 986 |
|
| 987 |
[*Note 3*:
|
| 988 |
|
| 989 |
+
The definition of a static data member of a template for which
|
| 990 |
+
default-initialization is desired can use functional cast notation
|
| 991 |
+
[[expr.type.conv]]:
|
| 992 |
|
| 993 |
``` cpp
|
| 994 |
template<> X Q<int>::x; // declaration
|
| 995 |
template<> X Q<int>::x (); // error: declares a function
|
| 996 |
+
template<> X Q<int>::x = X(); // definition
|
| 997 |
```
|
| 998 |
|
| 999 |
— *end note*]
|
| 1000 |
|
| 1001 |
A member or a member template of a class template may be explicitly
|
| 1002 |
specialized for a given implicit instantiation of the class template,
|
| 1003 |
even if the member or member template is defined in the class template
|
| 1004 |
definition. An explicit specialization of a member or member template is
|
| 1005 |
specified using the syntax for explicit specialization.
|
| 1006 |
|
| 1007 |
+
[*Example 8*:
|
| 1008 |
|
| 1009 |
``` cpp
|
| 1010 |
template<class T> struct A {
|
| 1011 |
void f(T);
|
| 1012 |
template<class X1> void g1(T, X1);
|
|
|
|
| 1038 |
A member or a member template may be nested within many enclosing class
|
| 1039 |
templates. In an explicit specialization for such a member, the member
|
| 1040 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 1041 |
template that is explicitly specialized.
|
| 1042 |
|
| 1043 |
+
[*Example 9*:
|
| 1044 |
|
| 1045 |
``` cpp
|
| 1046 |
template<class T1> class A {
|
| 1047 |
template<class T2> class B {
|
| 1048 |
void mf();
|
|
|
|
| 1064 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 1065 |
preceding the explicit specialization declaration of the member. The
|
| 1066 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 1067 |
shall be the same as those specified in the primary template definition.
|
| 1068 |
|
| 1069 |
+
[*Example 10*:
|
| 1070 |
|
| 1071 |
``` cpp
|
| 1072 |
template <class T1> class A {
|
| 1073 |
template<class T2> class B {
|
| 1074 |
template<class T3> void mf1(T3);
|
|
|
|
| 1101 |
- the explicit specialization of a function template;
|
| 1102 |
- the explicit specialization of a member function template;
|
| 1103 |
- the explicit specialization of a member function of a class template
|
| 1104 |
where the class template specialization to which the member function
|
| 1105 |
specialization belongs is implicitly instantiated. \[*Note 4*: Default
|
| 1106 |
+
function arguments can be specified in the declaration or definition
|
| 1107 |
of a member function of a class template specialization that is
|
| 1108 |
explicitly specialized. — *end note*]
|
| 1109 |
|