- tmp/tmp2smd35p7/{from.md → to.md} +1142 -1399
tmp/tmp2smd35p7/{from.md → to.md}
RENAMED
|
@@ -38,11 +38,11 @@ constraint-logical-and-expression:
|
|
| 38 |
primary-expression
|
| 39 |
constraint-logical-and-expression '&&' primary-expression
|
| 40 |
```
|
| 41 |
|
| 42 |
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 43 |
-
*template-declaration*
|
| 44 |
two consecutive `>` tokens [[temp.names]]. — *end note*]
|
| 45 |
|
| 46 |
The *declaration* in a *template-declaration* (if any) shall
|
| 47 |
|
| 48 |
- declare or define a function, a class, or a variable, or
|
|
@@ -78,49 +78,44 @@ struct matrix_constants {
|
|
| 78 |
};
|
| 79 |
```
|
| 80 |
|
| 81 |
— *end example*]
|
| 82 |
|
| 83 |
-
A *template-declaration* can appear only as a namespace
|
| 84 |
-
scope declaration.
|
| 85 |
-
*export-declaration*. In a function template declaration, the last
|
| 86 |
-
component of the *declarator-id* shall not be a *template-id*.
|
| 87 |
|
| 88 |
-
|
| 89 |
-
*
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
In a *template-declaration*, explicit specialization, or explicit
|
| 95 |
instantiation the *init-declarator-list* in the declaration shall
|
| 96 |
contain at most one declarator. When such a declaration is used to
|
| 97 |
declare a class template, no declarator is permitted.
|
| 98 |
|
| 99 |
-
A
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
specialization shall not have C linkage. Use of a linkage specification
|
| 104 |
-
other than `"C"` or `"C++"` with any of these constructs is
|
| 105 |
-
conditionally-supported, with *implementation-defined* semantics.
|
| 106 |
-
Template definitions shall obey the one-definition rule
|
| 107 |
-
[[basic.def.odr]].
|
| 108 |
|
| 109 |
-
[*Note
|
| 110 |
functions of class templates are considered definitions for the purpose
|
| 111 |
-
of template instantiation [[temp.decls]] and must
|
| 112 |
-
one-definition rule. — *end note*]
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
[[temp.
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
An entity is *templated* if it is
|
| 124 |
|
| 125 |
- a template,
|
| 126 |
- an entity defined [[basic.def]] or created [[class.temporary]] in a
|
|
@@ -128,23 +123,29 @@ An entity is *templated* if it is
|
|
| 128 |
- a member of a templated entity,
|
| 129 |
- an enumerator for an enumeration that is a templated entity, or
|
| 130 |
- the closure type of a *lambda-expression* [[expr.prim.lambda.closure]]
|
| 131 |
appearing in the declaration of a templated entity.
|
| 132 |
|
| 133 |
-
[*Note
|
| 134 |
-
defined in a templated entity is a templated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
A *template-declaration* is written in terms of its template parameters.
|
| 137 |
The optional *requires-clause* following a *template-parameter-list*
|
| 138 |
allows the specification of constraints [[temp.constr.decl]] on template
|
| 139 |
arguments [[temp.arg]]. The *requires-clause* introduces the
|
| 140 |
*constraint-expression* that results from interpreting the
|
| 141 |
*constraint-logical-or-expression* as a *constraint-expression*. The
|
| 142 |
*constraint-logical-or-expression* of a *requires-clause* is an
|
| 143 |
unevaluated operand [[expr.context]].
|
| 144 |
|
| 145 |
-
[*Note
|
| 146 |
|
| 147 |
The expression in a *requires-clause* uses a restricted grammar to avoid
|
| 148 |
ambiguities. Parentheses can be used to specify arbitrary expressions in
|
| 149 |
a *requires-clause*.
|
| 150 |
|
|
@@ -196,20 +197,24 @@ type-parameter-key:
|
|
| 196 |
type-constraint:
|
| 197 |
nested-name-specifierₒₚₜ concept-name
|
| 198 |
nested-name-specifierₒₚₜ concept-name '<' template-argument-listₒₚₜ '>'
|
| 199 |
```
|
| 200 |
|
|
|
|
|
|
|
|
|
|
| 201 |
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 202 |
-
*type-parameter*
|
| 203 |
consecutive `>` tokens [[temp.names]]. — *end note*]
|
| 204 |
|
| 205 |
There is no semantic difference between `class` and `typename` in a
|
| 206 |
*type-parameter-key*. `typename` followed by an *unqualified-id* names a
|
| 207 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 208 |
-
the type in a non-type
|
| 209 |
-
|
| 210 |
-
*
|
|
|
|
| 211 |
|
| 212 |
[*Example 1*:
|
| 213 |
|
| 214 |
``` cpp
|
| 215 |
class T { ... };
|
|
@@ -228,18 +233,19 @@ unnamed non-type *template-parameter* of class `T`.
|
|
| 228 |
|
| 229 |
A storage class shall not be specified in a *template-parameter*
|
| 230 |
declaration. Types shall not be defined in a *template-parameter*
|
| 231 |
declaration.
|
| 232 |
|
| 233 |
-
|
|
|
|
| 234 |
its *identifier* to be a *typedef-name* (if declared without `template`)
|
| 235 |
or *template-name* (if declared with `template`) in the scope of the
|
| 236 |
template declaration.
|
| 237 |
|
| 238 |
[*Note 2*:
|
| 239 |
|
| 240 |
-
A template argument
|
| 241 |
example,
|
| 242 |
|
| 243 |
``` cpp
|
| 244 |
template<class T> class myarray { ... };
|
| 245 |
|
|
@@ -297,11 +303,11 @@ A *structural type* is one of the following:
|
|
| 297 |
- an lvalue reference type, or
|
| 298 |
- a literal class type with the following properties:
|
| 299 |
- all base classes and non-static data members are public and
|
| 300 |
non-mutable and
|
| 301 |
- the types of all bases classes and non-static data members are
|
| 302 |
-
structural types or (possibly
|
| 303 |
|
| 304 |
An *id-expression* naming a non-type *template-parameter* of class type
|
| 305 |
`T` denotes a static storage duration object of type `const T`, known as
|
| 306 |
a *template parameter object*, whose value is that of the corresponding
|
| 307 |
template argument after it has been converted to the type of the
|
|
@@ -324,13 +330,13 @@ template<const X& x, int i, A a> void f() {
|
|
| 324 |
i++; // error: change of template-parameter value
|
| 325 |
|
| 326 |
&x; // OK
|
| 327 |
&i; // error: address of non-reference template-parameter
|
| 328 |
&a; // OK
|
| 329 |
-
int& ri = i; // error: non-const reference
|
| 330 |
-
const int& cri = i; // OK
|
| 331 |
-
const A& ra = a; // OK
|
| 332 |
}
|
| 333 |
```
|
| 334 |
|
| 335 |
— *end example*]
|
| 336 |
|
|
@@ -380,14 +386,14 @@ specified after `=` in a *template-parameter*. A default
|
|
| 380 |
parameter pack [[temp.variadic]]. A default *template-argument* may be
|
| 381 |
specified in a template declaration. A default *template-argument* shall
|
| 382 |
not be specified in the *template-parameter-list*s of the definition of
|
| 383 |
a member of a class template that appears outside of the member’s class.
|
| 384 |
A default *template-argument* shall not be specified in a friend class
|
| 385 |
-
template declaration. If a friend function template declaration
|
| 386 |
specifies a default *template-argument*, that declaration shall be a
|
| 387 |
-
definition and shall be
|
| 388 |
-
|
| 389 |
|
| 390 |
The set of default *template-argument*s available for use is obtained by
|
| 391 |
merging the default arguments from all prior declarations of the
|
| 392 |
template in the same way default function arguments are
|
| 393 |
[[dcl.fct.default]].
|
|
@@ -431,27 +437,15 @@ template<class... T, class... U> void f() { } // error
|
|
| 431 |
template<class... T, class U> void g() { } // error
|
| 432 |
```
|
| 433 |
|
| 434 |
— *end example*]
|
| 435 |
|
| 436 |
-
A *template-parameter* shall not be given default arguments by two
|
| 437 |
-
different declarations in the same scope.
|
| 438 |
-
|
| 439 |
-
[*Example 8*:
|
| 440 |
-
|
| 441 |
-
``` cpp
|
| 442 |
-
template<class T = int> class X;
|
| 443 |
-
template<class T = int> class X { ... }; // error
|
| 444 |
-
```
|
| 445 |
-
|
| 446 |
-
— *end example*]
|
| 447 |
-
|
| 448 |
When parsing a default *template-argument* for a non-type
|
| 449 |
*template-parameter*, the first non-nested `>` is taken as the end of
|
| 450 |
the *template-parameter-list* rather than a greater-than operator.
|
| 451 |
|
| 452 |
-
[*Example
|
| 453 |
|
| 454 |
``` cpp
|
| 455 |
template<int i = 3 > 4 > // syntax error
|
| 456 |
class X { ... };
|
| 457 |
|
|
@@ -464,11 +458,11 @@ class Y { ... };
|
|
| 464 |
A *template-parameter* of a template *template-parameter* is permitted
|
| 465 |
to have a default *template-argument*. When such default arguments are
|
| 466 |
specified, they apply to the template *template-parameter* in the scope
|
| 467 |
of the template *template-parameter*.
|
| 468 |
|
| 469 |
-
[*Example
|
| 470 |
|
| 471 |
``` cpp
|
| 472 |
template <template <class TT = float> class T> struct A {
|
| 473 |
inline void f();
|
| 474 |
inline void g();
|
|
@@ -494,11 +488,11 @@ unexpanded packs is a pack expansion. A type parameter pack with a
|
|
| 494 |
*type-constraint* that contains an unexpanded parameter pack is a pack
|
| 495 |
expansion. A template parameter pack that is a pack expansion shall not
|
| 496 |
expand a template parameter pack declared in the same
|
| 497 |
*template-parameter-list*.
|
| 498 |
|
| 499 |
-
[*Example
|
| 500 |
|
| 501 |
``` cpp
|
| 502 |
template <class... Types> // Types is a template type parameter pack
|
| 503 |
class Tuple; // but not a pack expansion
|
| 504 |
|
|
@@ -549,41 +543,60 @@ template-argument:
|
|
| 549 |
constant-expression
|
| 550 |
type-id
|
| 551 |
id-expression
|
| 552 |
```
|
| 553 |
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
distinct `>` tokens, the first of which is taken as the end of the
|
| 578 |
*template-argument-list* and completes the *template-id*.
|
| 579 |
|
| 580 |
-
[*Note
|
| 581 |
-
terminate an enclosing *template-id* construct or it
|
| 582 |
different construct (e.g., a cast). — *end note*]
|
| 583 |
|
| 584 |
-
[*Example
|
| 585 |
|
| 586 |
``` cpp
|
| 587 |
template<int i> class X { ... };
|
| 588 |
|
| 589 |
X< 1>2 > x1; // syntax error
|
|
@@ -595,53 +608,25 @@ Y<X<6>>1>> x4; // syntax error
|
|
| 595 |
Y<X<(6>>1)>> x5; // OK
|
| 596 |
```
|
| 597 |
|
| 598 |
— *end example*]
|
| 599 |
|
| 600 |
-
The keyword `template`
|
| 601 |
-
*
|
| 602 |
-
*decltype-specifier*. In a *qualified-id* of a *declarator-id* or in a
|
| 603 |
-
*qualified-id* formed by a *class-head-name* [[class.pre]] or
|
| 604 |
-
*enum-head-name* [[dcl.enum]], the keyword `template` shall not appear
|
| 605 |
-
at the top level. In a *qualified-id* used as the name in a
|
| 606 |
-
*typename-specifier* [[temp.res]], *elaborated-type-specifier*
|
| 607 |
-
[[dcl.type.elab]], *using-declaration* [[namespace.udecl]], or
|
| 608 |
-
*class-or-decltype* [[class.derived]], an optional keyword `template`
|
| 609 |
-
appearing at the top level is ignored. In these contexts, a `<` token is
|
| 610 |
-
always assumed to introduce a *template-argument-list*. In all other
|
| 611 |
-
contexts, when naming a template specialization of a member of an
|
| 612 |
-
unknown specialization [[temp.dep.type]], the member template name shall
|
| 613 |
-
be prefixed by the keyword `template`.
|
| 614 |
|
| 615 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
|
| 617 |
-
``
|
| 618 |
-
struct X {
|
| 619 |
-
template<std::size_t> X* alloc();
|
| 620 |
-
template<std::size_t> static X* adjust();
|
| 621 |
-
};
|
| 622 |
-
template<class T> void f(T* p) {
|
| 623 |
-
T* p1 = p->alloc<200>(); // error: < means less than
|
| 624 |
-
T* p2 = p->template alloc<200>(); // OK: < starts template argument list
|
| 625 |
-
T::adjust<100>(); // error: < means less than
|
| 626 |
-
T::template adjust<100>(); // OK: < starts template argument list
|
| 627 |
-
}
|
| 628 |
-
```
|
| 629 |
-
|
| 630 |
-
— *end example*]
|
| 631 |
-
|
| 632 |
-
A name prefixed by the keyword `template` shall be a *template-id* or
|
| 633 |
-
the name shall refer to a class template or an alias template.
|
| 634 |
-
|
| 635 |
-
[*Note 4*: The keyword `template` may not be applied to non-template
|
| 636 |
members of class templates. — *end note*]
|
| 637 |
|
| 638 |
-
[*Note
|
| 639 |
-
prefix is allowed
|
| 640 |
-
|
| 641 |
-
`->` or `.` is not dependent on a *template-parameter*, or the use does
|
| 642 |
-
not appear in the scope of a template. — *end note*]
|
| 643 |
|
| 644 |
[*Example 3*:
|
| 645 |
|
| 646 |
``` cpp
|
| 647 |
template <class T> struct A {
|
|
@@ -649,19 +634,19 @@ template <class T> struct A {
|
|
| 649 |
template <class U> void f(U);
|
| 650 |
};
|
| 651 |
|
| 652 |
template <class T> void f(T t) {
|
| 653 |
A<T> a;
|
| 654 |
-
a.template f<>(t); // OK
|
| 655 |
a.template f(t); // error: not a template-id
|
| 656 |
}
|
| 657 |
|
| 658 |
template <class T> struct B {
|
| 659 |
template <class T2> struct C { };
|
| 660 |
};
|
| 661 |
|
| 662 |
-
//
|
| 663 |
template <class T, template <class X> class TT = T::template C> struct D { };
|
| 664 |
D<B<int> > db;
|
| 665 |
```
|
| 666 |
|
| 667 |
— *end example*]
|
|
@@ -698,13 +683,12 @@ using T5 = X<S>; // OK
|
|
| 698 |
|
| 699 |
— *end example*]
|
| 700 |
|
| 701 |
When the *template-name* of a *simple-template-id* names a constrained
|
| 702 |
non-function template or a constrained template *template-parameter*,
|
| 703 |
-
|
| 704 |
-
[[temp.
|
| 705 |
-
are non-dependent [[temp.dep.temp]], the associated constraints
|
| 706 |
[[temp.constr.decl]] of the constrained template shall be satisfied
|
| 707 |
[[temp.constr.constr]].
|
| 708 |
|
| 709 |
[*Example 5*:
|
| 710 |
|
|
@@ -745,11 +729,11 @@ A *concept-id* is a *simple-template-id* where the *template-name* is a
|
|
| 745 |
name a template specialization. A concept-id evaluates to `true` if the
|
| 746 |
concept’s normalized *constraint-expression* [[temp.constr.decl]] is
|
| 747 |
satisfied [[temp.constr.constr]] by the specified template arguments and
|
| 748 |
`false` otherwise.
|
| 749 |
|
| 750 |
-
[*Note
|
| 751 |
concept-id appearing in a *constraint-expression* is not evaluated
|
| 752 |
except as necessary to determine whether the normalized constraints are
|
| 753 |
satisfied. — *end note*]
|
| 754 |
|
| 755 |
[*Example 6*:
|
|
@@ -761,10 +745,12 @@ static_assert(C<int>); // OK
|
|
| 761 |
|
| 762 |
— *end example*]
|
| 763 |
|
| 764 |
## Template arguments <a id="temp.arg">[[temp.arg]]</a>
|
| 765 |
|
|
|
|
|
|
|
| 766 |
There are three forms of *template-argument*, corresponding to the three
|
| 767 |
forms of *template-parameter*: type, non-type and template. The type and
|
| 768 |
form of each *template-argument* specified in a *template-id* shall
|
| 769 |
match the type and form specified for the corresponding parameter
|
| 770 |
declared by the template in its *template-parameter-list*. When the
|
|
@@ -795,10 +781,17 @@ void bar() {
|
|
| 795 |
}
|
| 796 |
```
|
| 797 |
|
| 798 |
— *end example*]
|
| 799 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
In a *template-argument*, an ambiguity between a *type-id* and an
|
| 801 |
expression is resolved to a *type-id*, regardless of the form of the
|
| 802 |
corresponding *template-parameter*.[^3]
|
| 803 |
|
| 804 |
[*Example 2*:
|
|
@@ -812,17 +805,13 @@ void g() {
|
|
| 812 |
}
|
| 813 |
```
|
| 814 |
|
| 815 |
— *end example*]
|
| 816 |
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
[*Note 1*: If the name of the *template-argument* is accessible at the
|
| 821 |
-
point where it is used as a *template-argument*, there is no further
|
| 822 |
-
access restriction in the resulting instantiation where the
|
| 823 |
-
corresponding *template-parameter* name is used. — *end note*]
|
| 824 |
|
| 825 |
[*Example 3*:
|
| 826 |
|
| 827 |
``` cpp
|
| 828 |
template<class T> class X {
|
|
@@ -830,13 +819,13 @@ template<class T> class X {
|
|
| 830 |
};
|
| 831 |
|
| 832 |
class Y {
|
| 833 |
private:
|
| 834 |
struct S { ... };
|
| 835 |
-
X<S> x; // OK
|
| 836 |
// X<Y::S> has a static member of type Y::S
|
| 837 |
-
// OK
|
| 838 |
};
|
| 839 |
|
| 840 |
X<Y::S> y; // error: S not accessible
|
| 841 |
```
|
| 842 |
|
|
@@ -869,14 +858,14 @@ brackets shall still be used as the *template-argument-list*.
|
|
| 869 |
|
| 870 |
[*Example 5*:
|
| 871 |
|
| 872 |
``` cpp
|
| 873 |
template<class T = char> class String;
|
| 874 |
-
String<>* p; // OK
|
| 875 |
String* q; // syntax error
|
| 876 |
template<class ... Elements> class Tuple;
|
| 877 |
-
Tuple<>* t; // OK
|
| 878 |
Tuple* u; // syntax error
|
| 879 |
```
|
| 880 |
|
| 881 |
— *end example*]
|
| 882 |
|
|
@@ -889,26 +878,28 @@ that is a class template specialization may explicitly specify the
|
|
| 889 |
``` cpp
|
| 890 |
template<class T> struct A {
|
| 891 |
~A();
|
| 892 |
};
|
| 893 |
void f(A<int>* p, A<int>* q) {
|
| 894 |
-
p->A<int>::~A(); // OK
|
| 895 |
-
q->A<int>::~A<int>(); // OK
|
| 896 |
}
|
| 897 |
```
|
| 898 |
|
| 899 |
— *end example*]
|
| 900 |
|
| 901 |
If the use of a *template-argument* gives rise to an ill-formed
|
| 902 |
construct in the instantiation of a template specialization, the program
|
| 903 |
is ill-formed.
|
| 904 |
|
| 905 |
-
When name lookup for the name
|
| 906 |
-
both non-template functions in the overload set and
|
| 907 |
-
in the overload set for which the
|
| 908 |
-
*template-
|
| 909 |
-
|
|
|
|
|
|
|
| 910 |
|
| 911 |
When a *simple-template-id* does not name a function, a default
|
| 912 |
*template-argument* is implicitly instantiated [[temp.inst]] when the
|
| 913 |
value of that default argument is needed.
|
| 914 |
|
|
@@ -953,12 +944,12 @@ void f() {
|
|
| 953 |
}
|
| 954 |
```
|
| 955 |
|
| 956 |
— *end example*]
|
| 957 |
|
| 958 |
-
[*Note 1*: A template type argument
|
| 959 |
-
[[
|
| 960 |
|
| 961 |
### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
|
| 962 |
|
| 963 |
If the type `T` of a *template-parameter* [[temp.param]] contains a
|
| 964 |
placeholder type [[dcl.spec.auto]] or a placeholder for a deduced class
|
|
@@ -1080,22 +1071,21 @@ C<Y{X{1}.n}> c; // error: subobject of temporary object used to
|
|
| 1080 |
|
| 1081 |
### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
|
| 1082 |
|
| 1083 |
A *template-argument* for a template *template-parameter* shall be the
|
| 1084 |
name of a class template or an alias template, expressed as
|
| 1085 |
-
*id-expression*.
|
| 1086 |
-
|
| 1087 |
-
template argument with the corresponding parameter; partial
|
| 1088 |
specializations are not considered even if their parameter lists match
|
| 1089 |
that of the template template parameter.
|
| 1090 |
|
| 1091 |
-
Any partial specializations [[temp.
|
| 1092 |
-
primary
|
| 1093 |
-
|
| 1094 |
-
|
| 1095 |
-
|
| 1096 |
-
|
| 1097 |
|
| 1098 |
[*Example 1*:
|
| 1099 |
|
| 1100 |
``` cpp
|
| 1101 |
template<class T> class A { // primary template
|
|
@@ -1165,12 +1155,12 @@ template <class T1> struct A;
|
|
| 1165 |
template <class T1, class T2> struct B;
|
| 1166 |
template <int N> struct C;
|
| 1167 |
template <class T1, int N> struct D;
|
| 1168 |
template <class T1, class T2, int N = 17> struct E;
|
| 1169 |
|
| 1170 |
-
eval<A<int>> eA; // OK
|
| 1171 |
-
eval<B<int, float>> eB; // OK
|
| 1172 |
eval<C<17>> eC; // error: C does not match TT in partial specialization
|
| 1173 |
eval<D<int, 17>> eD; // error: D does not match TT in partial specialization
|
| 1174 |
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
|
| 1175 |
```
|
| 1176 |
|
|
@@ -1218,18 +1208,22 @@ according to the partial ordering rules for function templates
|
|
| 1218 |
If the rewrite produces an invalid type, then `P` is not at least as
|
| 1219 |
specialized as `A`.
|
| 1220 |
|
| 1221 |
## Template constraints <a id="temp.constr">[[temp.constr]]</a>
|
| 1222 |
|
| 1223 |
-
|
| 1224 |
-
|
|
|
|
|
|
|
| 1225 |
defined in [[temp.constr.constr]]. Constraints are associated with
|
| 1226 |
declarations in [[temp.constr.decl]]. Declarations are partially ordered
|
| 1227 |
by their associated constraints [[temp.constr.order]]. — *end note*]
|
| 1228 |
|
| 1229 |
### Constraints <a id="temp.constr.constr">[[temp.constr.constr]]</a>
|
| 1230 |
|
|
|
|
|
|
|
| 1231 |
A *constraint* is a sequence of logical operations and operands that
|
| 1232 |
specifies requirements on template arguments. The operands of a logical
|
| 1233 |
operation are constraints. There are three different kinds of
|
| 1234 |
constraints:
|
| 1235 |
|
|
@@ -1280,11 +1274,11 @@ template<typename T>
|
|
| 1280 |
requires (sizeof(T) > 1) && (get_value<T>())
|
| 1281 |
void f(T); // has associated constraint sizeof(T) > 1 ∧ get_value<T>()
|
| 1282 |
|
| 1283 |
void f(int);
|
| 1284 |
|
| 1285 |
-
f('a'); // OK
|
| 1286 |
```
|
| 1287 |
|
| 1288 |
In the satisfaction of the associated constraints [[temp.constr.decl]]
|
| 1289 |
of `f`, the constraint `sizeof(char) > 1` is not satisfied; the second
|
| 1290 |
operand is not checked for satisfaction.
|
|
@@ -1391,14 +1385,14 @@ void h() {
|
|
| 1391 |
}
|
| 1392 |
```
|
| 1393 |
|
| 1394 |
— *end example*]
|
| 1395 |
|
| 1396 |
-
|
| 1397 |
-
|
| 1398 |
-
|
| 1399 |
-
|
| 1400 |
|
| 1401 |
[*Example 2*:
|
| 1402 |
|
| 1403 |
``` cpp
|
| 1404 |
template <unsigned N> void f2()
|
|
@@ -1599,11 +1593,11 @@ names a concept specialization [[expr.prim.id]]. — *end note*]
|
|
| 1599 |
|
| 1600 |
``` cpp
|
| 1601 |
template<typename T> concept C1 = sizeof(T) == 1;
|
| 1602 |
template<typename T> concept C2 = C1<T> && 1 == 2;
|
| 1603 |
template<typename T> concept C3 = requires { typename T::type; };
|
| 1604 |
-
template<typename T> concept C4 = requires (T x) { ++x; }
|
| 1605 |
|
| 1606 |
template<C2 U> void f1(U); // #1
|
| 1607 |
template<C3 U> void f2(U); // #2
|
| 1608 |
template<C4 U> void f3(U); // #3
|
| 1609 |
```
|
|
@@ -1618,12 +1612,15 @@ mapping `T` ↦ `U`).
|
|
| 1618 |
— *end example*]
|
| 1619 |
|
| 1620 |
### Partial ordering by constraints <a id="temp.constr.order">[[temp.constr.order]]</a>
|
| 1621 |
|
| 1622 |
A constraint P *subsumes* a constraint Q if and only if, for every
|
| 1623 |
-
disjunctive clause Pᵢ in the disjunctive normal form[^4]
|
| 1624 |
-
|
|
|
|
|
|
|
|
|
|
| 1625 |
of Q, where
|
| 1626 |
|
| 1627 |
- a disjunctive clause Pᵢ subsumes a conjunctive clause Qⱼ if and only
|
| 1628 |
if there exists an atomic constraint Pᵢₐ in Pᵢ for which there exists
|
| 1629 |
an atomic constraint $Q_{jb}$ in Qⱼ such that Pᵢₐ subsumes $Q_{jb}$,
|
|
@@ -1645,11 +1642,11 @@ partial ordering is used to determine
|
|
| 1645 |
- the best viable candidate of non-template functions
|
| 1646 |
[[over.match.best]],
|
| 1647 |
- the address of a non-template function [[over.over]],
|
| 1648 |
- the matching of template template arguments [[temp.arg.template]],
|
| 1649 |
- the partial ordering of class template specializations
|
| 1650 |
-
[[temp.
|
| 1651 |
- the partial ordering of function templates [[temp.func.order]].
|
| 1652 |
|
| 1653 |
— *end note*]
|
| 1654 |
|
| 1655 |
A declaration `D1` is *at least as constrained* as a declaration `D2` if
|
|
@@ -1753,56 +1750,56 @@ declares `y` and `z` to be of the same type.
|
|
| 1753 |
If an expression e is type-dependent [[temp.dep.expr]], `decltype(e)`
|
| 1754 |
denotes a unique dependent type. Two such *decltype-specifier*s refer to
|
| 1755 |
the same type only if their *expression*s are equivalent
|
| 1756 |
[[temp.over.link]].
|
| 1757 |
|
| 1758 |
-
[*Note 1*: However, such a type
|
| 1759 |
*typedef-name*. — *end note*]
|
| 1760 |
|
| 1761 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 1762 |
|
| 1763 |
-
|
| 1764 |
-
|
| 1765 |
-
|
| 1766 |
-
|
| 1767 |
-
|
| 1768 |
-
|
| 1769 |
-
|
| 1770 |
-
|
| 1771 |
-
template
|
| 1772 |
-
|
| 1773 |
-
|
| 1774 |
-
|
| 1775 |
-
|
| 1776 |
-
[*Note 1*: However, this syntax is allowed in class template partial
|
| 1777 |
-
specializations [[temp.class.spec]]. — *end note*]
|
| 1778 |
|
| 1779 |
For purposes of name lookup and instantiation, default arguments,
|
| 1780 |
*type-constraint*s, *requires-clause*s [[temp.pre]], and
|
| 1781 |
*noexcept-specifier*s of function templates and of member functions of
|
| 1782 |
class templates are considered definitions; each default argument,
|
| 1783 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 1784 |
separate definition which is unrelated to the templated function
|
| 1785 |
-
definition or to any other default arguments *type-constraint*s,
|
| 1786 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 1787 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 1788 |
are considered definitions.
|
| 1789 |
|
| 1790 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 1791 |
possible to partially or explicitly specialize an alias template.
|
| 1792 |
|
| 1793 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 1794 |
|
|
|
|
|
|
|
| 1795 |
A *class template* defines the layout and operations for an unbounded
|
| 1796 |
set of related types.
|
| 1797 |
|
| 1798 |
[*Example 1*:
|
| 1799 |
|
| 1800 |
-
|
| 1801 |
-
definitions: one class `List<T>` for every type
|
| 1802 |
-
linked list of elements of type `T`. Similarly, a
|
| 1803 |
-
describing a contiguous, dynamic array
|
|
|
|
| 1804 |
|
| 1805 |
``` cpp
|
| 1806 |
template<class T> class Array {
|
| 1807 |
T* v;
|
| 1808 |
int sz;
|
|
@@ -1812,26 +1809,25 @@ public:
|
|
| 1812 |
T& elem(int i) { return v[i]; }
|
| 1813 |
};
|
| 1814 |
```
|
| 1815 |
|
| 1816 |
The prefix `template<class T>` specifies that a template is being
|
| 1817 |
-
declared and that a *type-name* `T`
|
| 1818 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 1819 |
|
| 1820 |
— *end example*]
|
| 1821 |
|
| 1822 |
-
|
| 1823 |
-
|
| 1824 |
-
of
|
| 1825 |
-
template definition
|
| 1826 |
-
|
| 1827 |
-
|
| 1828 |
-
the template parameter names used in the
|
| 1829 |
-
template
|
| 1830 |
-
definition
|
| 1831 |
-
|
| 1832 |
-
pack shall be expanded with an ellipsis in the template argument list.
|
| 1833 |
|
| 1834 |
[*Example 2*:
|
| 1835 |
|
| 1836 |
``` cpp
|
| 1837 |
template<class T1, class T2> struct A {
|
|
@@ -1862,11 +1858,11 @@ template<C T> struct S {
|
|
| 1862 |
void g();
|
| 1863 |
void h();
|
| 1864 |
template<D U> struct Inner;
|
| 1865 |
};
|
| 1866 |
|
| 1867 |
-
template<C A> void S<A>::f() { } // OK
|
| 1868 |
template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
|
| 1869 |
|
| 1870 |
template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
|
| 1871 |
void S<T>::h() { } // functionally equivalent but not equivalent
|
| 1872 |
|
|
@@ -1874,13 +1870,15 @@ template<C X> template<D Y>
|
|
| 1874 |
struct S<X>::Inner { }; // OK
|
| 1875 |
```
|
| 1876 |
|
| 1877 |
— *end example*]
|
| 1878 |
|
| 1879 |
-
|
| 1880 |
-
|
| 1881 |
-
|
|
|
|
|
|
|
| 1882 |
|
| 1883 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 1884 |
|
| 1885 |
A member function of a class template may be defined outside of the
|
| 1886 |
class template definition in which it is declared.
|
|
@@ -1897,11 +1895,11 @@ public:
|
|
| 1897 |
T& elem(int i) { return v[i]; }
|
| 1898 |
};
|
| 1899 |
```
|
| 1900 |
|
| 1901 |
declares three member functions of a class template. The subscript
|
| 1902 |
-
function
|
| 1903 |
|
| 1904 |
``` cpp
|
| 1905 |
template<class T> T& Array<T>::operator[](int i) {
|
| 1906 |
if (i<0 || sz<=i) error("Array: range error");
|
| 1907 |
return v[i];
|
|
@@ -1950,12 +1948,12 @@ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
|
| 1950 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 1951 |
|
| 1952 |
Deduction guides are used when a *template-name* appears as a type
|
| 1953 |
specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
|
| 1954 |
guides are not found by name lookup. Instead, when performing class
|
| 1955 |
-
template argument deduction [[over.match.class.deduct]],
|
| 1956 |
-
guides declared for the class template are considered.
|
| 1957 |
|
| 1958 |
``` bnf
|
| 1959 |
deduction-guide:
|
| 1960 |
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 1961 |
```
|
|
@@ -1981,15 +1979,15 @@ S x{A()}; // x is of type S<short, int>
|
|
| 1981 |
|
| 1982 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 1983 |
deduction guide as in a function declaration [[dcl.fct]]. The
|
| 1984 |
*simple-template-id* shall name a class template specialization. The
|
| 1985 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 1986 |
-
the *simple-template-id*. A *deduction-guide* shall
|
| 1987 |
-
|
| 1988 |
-
template,
|
| 1989 |
-
same
|
| 1990 |
-
|
| 1991 |
|
| 1992 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 1993 |
|
| 1994 |
A member class of a class template may be defined outside the class
|
| 1995 |
template definition in which it is declared.
|
|
@@ -2001,13 +1999,13 @@ instantiation [[temp.inst]]. For example,
|
|
| 2001 |
|
| 2002 |
``` cpp
|
| 2003 |
template<class T> struct A {
|
| 2004 |
class B;
|
| 2005 |
};
|
| 2006 |
-
A<int>::B* b1; // OK
|
| 2007 |
template<class T> class A<T>::B { };
|
| 2008 |
-
A<int>::B b2; // OK
|
| 2009 |
```
|
| 2010 |
|
| 2011 |
— *end note*]
|
| 2012 |
|
| 2013 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
|
@@ -2043,11 +2041,11 @@ of unknown bound can have a different bound from its definition, if any.
|
|
| 2043 |
``` cpp
|
| 2044 |
template <class T> struct A {
|
| 2045 |
static int i[];
|
| 2046 |
};
|
| 2047 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 2048 |
-
template <> int A<int>::i[] = { 1 }; // OK
|
| 2049 |
```
|
| 2050 |
|
| 2051 |
— *end example*]
|
| 2052 |
|
| 2053 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
|
@@ -2139,11 +2137,11 @@ int main() {
|
|
| 2139 |
}
|
| 2140 |
```
|
| 2141 |
|
| 2142 |
— *end example*]
|
| 2143 |
|
| 2144 |
-
A member function template shall not be virtual.
|
| 2145 |
|
| 2146 |
[*Example 4*:
|
| 2147 |
|
| 2148 |
``` cpp
|
| 2149 |
template <class T> struct AA {
|
|
@@ -2164,19 +2162,21 @@ class B {
|
|
| 2164 |
virtual void f(int);
|
| 2165 |
};
|
| 2166 |
|
| 2167 |
class D : public B {
|
| 2168 |
template <class T> void f(T); // does not override B::f(int)
|
| 2169 |
-
void f(int i) { f<>(i); } // overriding function that calls the template
|
| 2170 |
};
|
| 2171 |
```
|
| 2172 |
|
| 2173 |
— *end example*]
|
| 2174 |
|
|
|
|
|
|
|
| 2175 |
A specialization of a conversion function template is referenced in the
|
| 2176 |
same way as a non-template conversion function that converts to the same
|
| 2177 |
-
type.
|
| 2178 |
|
| 2179 |
[*Example 6*:
|
| 2180 |
|
| 2181 |
``` cpp
|
| 2182 |
struct A {
|
|
@@ -2193,27 +2193,15 @@ int main() {
|
|
| 2193 |
}
|
| 2194 |
```
|
| 2195 |
|
| 2196 |
— *end example*]
|
| 2197 |
|
| 2198 |
-
|
| 2199 |
-
|
| 2200 |
-
conversion function template
|
| 2201 |
|
| 2202 |
-
|
| 2203 |
-
lookup. Instead, any conversion function templates visible in the
|
| 2204 |
-
context of the use are considered. For each such operator, if argument
|
| 2205 |
-
deduction succeeds [[temp.deduct.conv]], the resulting specialization is
|
| 2206 |
-
used as if found by name lookup.
|
| 2207 |
-
|
| 2208 |
-
A *using-declaration* in a derived class cannot refer to a
|
| 2209 |
-
specialization of a conversion function template in a base class.
|
| 2210 |
-
|
| 2211 |
-
Overload resolution [[over.ics.rank]] and partial ordering
|
| 2212 |
-
[[temp.func.order]] are used to select the best conversion function
|
| 2213 |
-
among multiple specializations of conversion function templates and/or
|
| 2214 |
-
non-template conversion functions.
|
| 2215 |
|
| 2216 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 2217 |
|
| 2218 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 2219 |
more template arguments.
|
|
@@ -2342,94 +2330,72 @@ template<class ... Args1> struct zip {
|
|
| 2342 |
typedef Tuple<Pair<Args1, Args2> ... > type;
|
| 2343 |
};
|
| 2344 |
};
|
| 2345 |
|
| 2346 |
typedef zip<short, int>::with<unsigned short, unsigned>::type T1;
|
| 2347 |
-
// T1 is Tuple<Pair<short, unsigned short>, Pair<int, unsigned>
|
| 2348 |
typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
| 2349 |
// error: different number of arguments specified for Args1 and Args2
|
| 2350 |
|
| 2351 |
template<class ... Args>
|
| 2352 |
-
void g(Args ... args) { // OK
|
| 2353 |
-
f(const_cast<const Args*>(&args)...); // OK
|
| 2354 |
f(5 ...); // error: pattern does not contain any packs
|
| 2355 |
f(args); // error: pack ``args'' is not expanded
|
| 2356 |
-
f(h(args ...) + args ...); // OK
|
| 2357 |
// second ``args'' expanded within f
|
| 2358 |
}
|
| 2359 |
```
|
| 2360 |
|
| 2361 |
— *end example*]
|
| 2362 |
|
| 2363 |
-
The instantiation of a pack expansion
|
| 2364 |
-
|
| 2365 |
-
|
| 2366 |
-
|
| 2367 |
-
|
| 2368 |
-
|
| 2369 |
-
follows:
|
| 2370 |
|
| 2371 |
-
- if the pack is a template parameter pack, the element is
|
| 2372 |
-
|
| 2373 |
-
|
|
|
|
|
|
|
|
|
|
| 2374 |
- if the pack is a function parameter pack, the element is an
|
| 2375 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 2376 |
from instantiation of the function parameter pack declaration;
|
| 2377 |
otherwise
|
| 2378 |
- if the pack is an *init-capture* pack, the element is an
|
| 2379 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 2380 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 2381 |
-
pack.
|
| 2382 |
|
| 2383 |
-
|
| 2384 |
-
|
| 2385 |
-
|
| 2386 |
-
|
| 2387 |
-
etc. — *end note*]
|
| 2388 |
-
|
| 2389 |
-
When N is zero, the instantiation of the expansion produces an empty
|
| 2390 |
-
list. Such an instantiation does not alter the syntactic interpretation
|
| 2391 |
-
of the enclosing construct, even in cases where omitting the list
|
| 2392 |
-
entirely would otherwise be ill-formed or would result in an ambiguity
|
| 2393 |
-
in the grammar.
|
| 2394 |
-
|
| 2395 |
-
[*Example 6*:
|
| 2396 |
-
|
| 2397 |
-
``` cpp
|
| 2398 |
-
template<class... T> struct X : T... { };
|
| 2399 |
-
template<class... T> void f(T... values) {
|
| 2400 |
-
X<T...> x(values...);
|
| 2401 |
-
}
|
| 2402 |
-
|
| 2403 |
-
template void f<>(); // OK: X<> has no base classes
|
| 2404 |
-
// x is a variable of type X<> that is value-initialized
|
| 2405 |
-
```
|
| 2406 |
-
|
| 2407 |
-
— *end example*]
|
| 2408 |
|
| 2409 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 2410 |
-
an integral constant
|
| 2411 |
-
expands.
|
| 2412 |
|
| 2413 |
-
The instantiation of a *fold-expression* produces:
|
| 2414 |
|
| 2415 |
-
- `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary
|
| 2416 |
-
|
| 2417 |
-
|
| 2418 |
-
|
| 2419 |
-
|
| 2420 |
-
|
| 2421 |
-
|
|
|
|
| 2422 |
|
| 2423 |
-
In each case, *op* is the *fold-operator*
|
| 2424 |
-
|
| 2425 |
-
|
| 2426 |
-
with its iᵗʰ element. For a binary fold-expression, E is generated by
|
| 2427 |
-
instantiating the *cast-expression* that did not contain an unexpanded
|
| 2428 |
-
pack.
|
| 2429 |
|
| 2430 |
-
[*Example
|
| 2431 |
|
| 2432 |
``` cpp
|
| 2433 |
template<typename ...Args>
|
| 2434 |
bool all(Args ...args) { return (... && args); }
|
| 2435 |
|
|
@@ -2439,12 +2405,12 @@ bool b = all(true, true, true, false);
|
|
| 2439 |
Within the instantiation of `all`, the returned expression expands to
|
| 2440 |
`((true && true) && true) && false`, which evaluates to `false`.
|
| 2441 |
|
| 2442 |
— *end example*]
|
| 2443 |
|
| 2444 |
-
If N is zero for a unary fold
|
| 2445 |
-
|
| 2446 |
[[temp.fold.empty]], the instantiation is ill-formed.
|
| 2447 |
|
| 2448 |
**Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
|
| 2449 |
|
| 2450 |
| Operator | Value when pack is empty |
|
|
@@ -2452,29 +2418,39 @@ shown in [[temp.fold.empty]]; if the operator is not listed in
|
|
| 2452 |
| `&&` | `true` |
|
| 2453 |
| `||` | `false` |
|
| 2454 |
| `,` | `void()` |
|
| 2455 |
|
| 2456 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2457 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 2458 |
|
| 2459 |
A friend of a class or class template can be a function template or
|
| 2460 |
class template, a specialization of a function template or class
|
| 2461 |
-
template, or a non-template function or class.
|
| 2462 |
-
declaration that is not a template declaration:
|
| 2463 |
-
|
| 2464 |
-
- if the name of the friend is a qualified or unqualified *template-id*,
|
| 2465 |
-
the friend declaration refers to a specialization of a function
|
| 2466 |
-
template, otherwise,
|
| 2467 |
-
- if the name of the friend is a *qualified-id* and a matching
|
| 2468 |
-
non-template function is found in the specified class or namespace,
|
| 2469 |
-
the friend declaration refers to that function, otherwise,
|
| 2470 |
-
- if the name of the friend is a *qualified-id* and a matching function
|
| 2471 |
-
template is found in the specified class or namespace, the friend
|
| 2472 |
-
declaration refers to the deduced specialization of that function
|
| 2473 |
-
template [[temp.deduct.decl]], otherwise,
|
| 2474 |
-
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 2475 |
-
non-template function.
|
| 2476 |
|
| 2477 |
[*Example 1*:
|
| 2478 |
|
| 2479 |
``` cpp
|
| 2480 |
template<class T> class task;
|
|
@@ -2504,10 +2480,19 @@ function template `preempt` as a friend; and each specialization of the
|
|
| 2504 |
template has the class template specialization `task<int>` as a friend,
|
| 2505 |
and has all specializations of the class template `frd` as friends.
|
| 2506 |
|
| 2507 |
— *end example*]
|
| 2508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2509 |
A friend template may be declared within a class or class template. A
|
| 2510 |
friend function template may be defined within a class or class
|
| 2511 |
template, but a friend class template may not be defined in a class or
|
| 2512 |
class template. In these cases, all specializations of the friend class
|
| 2513 |
or friend function template are friends of the class or class template
|
|
@@ -2524,11 +2509,11 @@ class A {
|
|
| 2524 |
|
| 2525 |
— *end example*]
|
| 2526 |
|
| 2527 |
A template friend declaration specifies that all specializations of that
|
| 2528 |
template, whether they are implicitly instantiated [[temp.inst]],
|
| 2529 |
-
partially specialized [[temp.
|
| 2530 |
[[temp.expl.spec]], are friends of the class containing the template
|
| 2531 |
friend declaration.
|
| 2532 |
|
| 2533 |
[*Example 3*:
|
| 2534 |
|
|
@@ -2547,17 +2532,17 @@ template<class T> struct A<T*> { X::Y ab; }; // OK
|
|
| 2547 |
A template friend declaration may declare a member of a dependent type
|
| 2548 |
to be a friend. The friend declaration shall declare a function or
|
| 2549 |
specify a type with an *elaborated-type-specifier*, in either case with
|
| 2550 |
a *nested-name-specifier* ending with a *simple-template-id*, *C*, whose
|
| 2551 |
*template-name* names a class template. The template parameters of the
|
| 2552 |
-
template friend declaration shall be deducible from *C*
|
| 2553 |
-
[[temp.deduct.type]]
|
| 2554 |
the class template is a friend of the class granting friendship if
|
| 2555 |
deduction of the template parameters of *C* from *S* succeeds, and
|
| 2556 |
substituting the deduced template arguments into the friend declaration
|
| 2557 |
-
produces a declaration that
|
| 2558 |
-
|
| 2559 |
|
| 2560 |
[*Example 4*:
|
| 2561 |
|
| 2562 |
``` cpp
|
| 2563 |
template<class T> struct A {
|
|
@@ -2593,13 +2578,10 @@ class C {
|
|
| 2593 |
}; // of those function templates
|
| 2594 |
```
|
| 2595 |
|
| 2596 |
— *end example*]
|
| 2597 |
|
| 2598 |
-
[*Note 1*: A friend declaration may first declare a member of an
|
| 2599 |
-
enclosing namespace scope [[temp.inject]]. — *end note*]
|
| 2600 |
-
|
| 2601 |
A friend template shall not be declared in a local class.
|
| 2602 |
|
| 2603 |
Friend declarations shall not declare partial specializations.
|
| 2604 |
|
| 2605 |
[*Example 5*:
|
|
@@ -2623,30 +2605,28 @@ definition. A friend function template with a constraint that depends on
|
|
| 2623 |
a template parameter from an enclosing template shall be a definition.
|
| 2624 |
Such a constrained friend function or function template declaration does
|
| 2625 |
not declare the same function or function template as a declaration in
|
| 2626 |
any other scope.
|
| 2627 |
|
| 2628 |
-
###
|
| 2629 |
|
| 2630 |
-
|
| 2631 |
-
|
| 2632 |
-
|
| 2633 |
-
specialization* of the class template named in the *simple-template-id*.
|
| 2634 |
-
A partial specialization of a class template provides an alternative
|
| 2635 |
definition of the template that is used instead of the primary
|
| 2636 |
definition when the arguments in a specialization match those given in
|
| 2637 |
-
the partial specialization [[temp.
|
| 2638 |
-
template shall
|
| 2639 |
-
A partial specialization shall be
|
| 2640 |
-
|
| 2641 |
-
specialization as the result of an implicit or explicit instantiation
|
| 2642 |
-
|
| 2643 |
-
required.
|
| 2644 |
|
| 2645 |
-
|
| 2646 |
-
|
| 2647 |
-
|
|
|
|
| 2648 |
|
| 2649 |
[*Example 1*:
|
| 2650 |
|
| 2651 |
``` cpp
|
| 2652 |
template<class T1, class T2, int I> class A { };
|
|
@@ -2660,11 +2640,11 @@ The first declaration declares the primary (unspecialized) class
|
|
| 2660 |
template. The second and subsequent declarations declare partial
|
| 2661 |
specializations of the primary template.
|
| 2662 |
|
| 2663 |
— *end example*]
|
| 2664 |
|
| 2665 |
-
A
|
| 2666 |
|
| 2667 |
[*Example 2*:
|
| 2668 |
|
| 2669 |
``` cpp
|
| 2670 |
template<typename T> concept C = true;
|
|
@@ -2681,38 +2661,18 @@ specialization succeeds, while the reverse does not. \#2 is more
|
|
| 2681 |
specialized because the template arguments are equivalent, but the
|
| 2682 |
partial specialization is more constrained [[temp.constr.order]].
|
| 2683 |
|
| 2684 |
— *end example*]
|
| 2685 |
|
| 2686 |
-
The template
|
| 2687 |
-
|
| 2688 |
-
specializations, the template argument list is explicitly written
|
| 2689 |
-
immediately following the class template name. For primary templates,
|
| 2690 |
-
this list is implicitly described by the template parameter list.
|
| 2691 |
-
Specifically, the order of the template arguments is the sequence in
|
| 2692 |
-
which they appear in the template parameter list.
|
| 2693 |
|
| 2694 |
-
|
| 2695 |
-
|
|
|
|
| 2696 |
|
| 2697 |
-
[*
|
| 2698 |
-
|
| 2699 |
-
The template argument list cannot be specified in the primary template
|
| 2700 |
-
declaration. For example,
|
| 2701 |
-
|
| 2702 |
-
``` cpp
|
| 2703 |
-
template<class T1, class T2, int I>
|
| 2704 |
-
class A<T1, T2, I> { }; // error
|
| 2705 |
-
```
|
| 2706 |
-
|
| 2707 |
-
— *end note*]
|
| 2708 |
-
|
| 2709 |
-
A class template partial specialization may be declared in any scope in
|
| 2710 |
-
which the corresponding primary template may be defined (
|
| 2711 |
-
[[namespace.memdef]], [[class.mem]], [[temp.mem]]).
|
| 2712 |
-
|
| 2713 |
-
[*Example 4*:
|
| 2714 |
|
| 2715 |
``` cpp
|
| 2716 |
template<class T> struct A {
|
| 2717 |
struct C {
|
| 2718 |
template<class T2> struct B { };
|
|
@@ -2727,18 +2687,19 @@ template<class T> template<class T2>
|
|
| 2727 |
A<short>::C::B<int*> absip; // uses partial specialization #2
|
| 2728 |
```
|
| 2729 |
|
| 2730 |
— *end example*]
|
| 2731 |
|
| 2732 |
-
Partial specialization declarations
|
| 2733 |
-
|
| 2734 |
-
|
| 2735 |
-
also considered. One consequence is that a *using-declaration* which
|
| 2736 |
-
refers to a class template does not restrict the set of partial
|
| 2737 |
-
specializations which may be found through the *using-declaration*.
|
| 2738 |
|
| 2739 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2740 |
|
| 2741 |
``` cpp
|
| 2742 |
namespace N {
|
| 2743 |
template<class T1, class T2> class A { }; // primary template
|
| 2744 |
}
|
|
@@ -2756,61 +2717,61 @@ A<int,int*> a; // uses the partial specialization, which is found through t
|
|
| 2756 |
— *end example*]
|
| 2757 |
|
| 2758 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 2759 |
parameter. All other non-type arguments are specialized.
|
| 2760 |
|
| 2761 |
-
Within the argument list of a
|
| 2762 |
-
|
| 2763 |
|
| 2764 |
- The type of a template parameter corresponding to a specialized
|
| 2765 |
-
non-type argument shall not be dependent on a parameter of the
|
| 2766 |
specialization.
|
| 2767 |
-
\[*Example
|
| 2768 |
``` cpp
|
| 2769 |
template <class T, T t> struct C {};
|
| 2770 |
template <class T> struct C<T, 1>; // error
|
| 2771 |
|
| 2772 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 2773 |
int array[5];
|
| 2774 |
template< int X > class A<X,&array> { }; // error
|
| 2775 |
```
|
| 2776 |
|
| 2777 |
— *end example*]
|
| 2778 |
-
- The specialization shall be more specialized than the primary
|
| 2779 |
-
[[temp.
|
| 2780 |
-
- The template parameter list of a specialization shall not
|
| 2781 |
-
default template argument values.[^8]
|
| 2782 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 2783 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 2784 |
template argument list.
|
| 2785 |
|
| 2786 |
The usual access checking rules do not apply to non-dependent names used
|
| 2787 |
to specify template arguments of the *simple-template-id* of the partial
|
| 2788 |
specialization.
|
| 2789 |
|
| 2790 |
-
[*Note 2*: The template arguments
|
| 2791 |
would normally not be accessible. Dependent names cannot be checked when
|
| 2792 |
declaring the partial specialization, but will be checked when
|
| 2793 |
substituting into the partial specialization. — *end note*]
|
| 2794 |
|
| 2795 |
-
#### Matching of
|
| 2796 |
|
| 2797 |
-
When a
|
| 2798 |
-
|
| 2799 |
-
|
| 2800 |
-
|
| 2801 |
-
|
| 2802 |
-
|
| 2803 |
|
| 2804 |
-
- If exactly one matching specialization is found, the
|
| 2805 |
-
generated from that specialization.
|
| 2806 |
-
- If more than one matching specialization is found, the partial
|
| 2807 |
-
rules [[temp.
|
| 2808 |
-
specializations is more specialized than the
|
| 2809 |
-
|
| 2810 |
-
|
| 2811 |
-
the program is ill-formed.
|
| 2812 |
- If no matches are found, the instantiation is generated from the
|
| 2813 |
primary template.
|
| 2814 |
|
| 2815 |
A partial specialization matches a given actual template argument list
|
| 2816 |
if the template arguments of the partial specialization can be deduced
|
|
@@ -2868,21 +2829,22 @@ template <int I, int J, int K> struct B {};
|
|
| 2868 |
template <int I> struct B<I, I*2, 2> {}; // OK
|
| 2869 |
```
|
| 2870 |
|
| 2871 |
— *end example*]
|
| 2872 |
|
| 2873 |
-
In a
|
| 2874 |
-
`A<int, int, 1>`) the argument list shall match the
|
| 2875 |
-
list of the primary template. The template arguments
|
| 2876 |
-
are deduced from the arguments of the
|
|
|
|
| 2877 |
|
| 2878 |
-
#### Partial ordering of
|
| 2879 |
|
| 2880 |
-
For two
|
| 2881 |
-
|
| 2882 |
-
|
| 2883 |
-
|
| 2884 |
[[temp.func.order]]:
|
| 2885 |
|
| 2886 |
- Each of the two function templates has the same template parameters
|
| 2887 |
and associated constraints [[temp.constr.decl]] as the corresponding
|
| 2888 |
partial specialization.
|
|
@@ -2936,26 +2898,20 @@ template<D T> void f(S<T>); // B
|
|
| 2936 |
The partial specialization \#2 is more specialized than \#1 because `B`
|
| 2937 |
is more specialized than `A`.
|
| 2938 |
|
| 2939 |
— *end example*]
|
| 2940 |
|
| 2941 |
-
#### Members of class template specializations <a id="temp.
|
| 2942 |
|
| 2943 |
-
The
|
| 2944 |
-
|
| 2945 |
-
|
| 2946 |
-
|
| 2947 |
-
|
| 2948 |
-
|
| 2949 |
-
|
| 2950 |
-
|
| 2951 |
-
that are used in a way that requires a definition shall be defined; the
|
| 2952 |
-
definitions of members of the primary template are never used as
|
| 2953 |
-
definitions for members of a class template partial specialization. An
|
| 2954 |
-
explicit specialization of a member of a class template partial
|
| 2955 |
-
specialization is declared in the same way as an explicit specialization
|
| 2956 |
-
of the primary template.
|
| 2957 |
|
| 2958 |
[*Example 1*:
|
| 2959 |
|
| 2960 |
``` cpp
|
| 2961 |
// primary class template
|
|
@@ -2992,15 +2948,15 @@ int main() {
|
|
| 2992 |
— *end example*]
|
| 2993 |
|
| 2994 |
If a member template of a class template is partially specialized, the
|
| 2995 |
member template partial specializations are member templates of the
|
| 2996 |
enclosing class template; if the enclosing class template is
|
| 2997 |
-
instantiated
|
| 2998 |
-
|
| 2999 |
-
|
| 3000 |
-
|
| 3001 |
-
|
| 3002 |
specializations of the member template are ignored for this
|
| 3003 |
specialization of the enclosing class template. If a partial
|
| 3004 |
specialization of the member template is explicitly specialized for a
|
| 3005 |
given (implicit) specialization of the enclosing class template, the
|
| 3006 |
primary member template and its other partial specializations are still
|
|
@@ -3023,28 +2979,33 @@ A<char>::B<int> abci; // uses #1
|
|
| 3023 |
|
| 3024 |
— *end example*]
|
| 3025 |
|
| 3026 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 3027 |
|
|
|
|
|
|
|
| 3028 |
A function template defines an unbounded set of related functions.
|
| 3029 |
|
| 3030 |
[*Example 1*:
|
| 3031 |
|
| 3032 |
-
A family of sort functions
|
| 3033 |
|
| 3034 |
``` cpp
|
| 3035 |
template<class T> class Array { };
|
| 3036 |
template<class T> void sort(Array<T>&);
|
| 3037 |
```
|
| 3038 |
|
| 3039 |
— *end example*]
|
| 3040 |
|
| 3041 |
-
A function template can
|
| 3042 |
-
|
| 3043 |
-
|
| 3044 |
-
|
| 3045 |
-
|
|
|
|
|
|
|
|
|
|
| 3046 |
|
| 3047 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 3048 |
|
| 3049 |
It is possible to overload function templates so that two different
|
| 3050 |
function template specializations have the same type.
|
|
@@ -3079,17 +3040,17 @@ names of the template parameters are significant only for establishing
|
|
| 3079 |
the relationship between the template parameters and the rest of the
|
| 3080 |
signature.
|
| 3081 |
|
| 3082 |
[*Note 1*:
|
| 3083 |
|
| 3084 |
-
Two distinct function templates
|
| 3085 |
and function parameter lists, even if overload resolution alone cannot
|
| 3086 |
distinguish them.
|
| 3087 |
|
| 3088 |
``` cpp
|
| 3089 |
template<class T> void f();
|
| 3090 |
-
template<int I> void f(); // OK
|
| 3091 |
// distinguishable with an explicit template argument list
|
| 3092 |
```
|
| 3093 |
|
| 3094 |
— *end note*]
|
| 3095 |
|
|
@@ -3126,25 +3087,27 @@ another token that names the same template parameter in the other
|
|
| 3126 |
expression. Two unevaluated operands that do not involve template
|
| 3127 |
parameters are considered equivalent if two function definitions
|
| 3128 |
containing the expressions would satisfy the one-definition rule, except
|
| 3129 |
that the tokens used to name types and declarations may differ as long
|
| 3130 |
as they name the same entities, and the tokens used to form concept-ids
|
| 3131 |
-
may differ as long as the two *template-id*s are the same
|
|
|
|
| 3132 |
|
| 3133 |
[*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
|
| 3134 |
type. — *end note*]
|
| 3135 |
|
| 3136 |
Two *lambda-expression*s are never considered equivalent.
|
| 3137 |
|
| 3138 |
[*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
|
| 3139 |
signature of a function template with external linkage. — *end note*]
|
| 3140 |
|
| 3141 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 3142 |
-
only the name itself is considered, not the result of name lookup
|
| 3143 |
-
|
| 3144 |
-
|
| 3145 |
-
first declaration
|
|
|
|
| 3146 |
|
| 3147 |
[*Example 3*:
|
| 3148 |
|
| 3149 |
``` cpp
|
| 3150 |
template <int I, int J> void f(A<I+J>); // #1
|
|
@@ -3153,11 +3116,11 @@ template <int K, int L> void f(A<K+L>); // same as #1
|
|
| 3153 |
template <class T> decltype(g(T())) h();
|
| 3154 |
int g(int);
|
| 3155 |
template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup…
|
| 3156 |
{ return g(T()); } // …{} although the lookup here does find g(int)
|
| 3157 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 3158 |
-
//
|
| 3159 |
|
| 3160 |
// ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
|
| 3161 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 3162 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 3163 |
|
|
@@ -3174,13 +3137,25 @@ of template arguments, the evaluation of the expression results in the
|
|
| 3174 |
same value. Two unevaluated operands that are not equivalent are
|
| 3175 |
functionally equivalent if, for any given set of template arguments, the
|
| 3176 |
expressions perform the same operations in the same order with the same
|
| 3177 |
entities.
|
| 3178 |
|
| 3179 |
-
[*Note
|
| 3180 |
parentheses. — *end note*]
|
| 3181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3182 |
Two *template-head*s are *equivalent* if their
|
| 3183 |
*template-parameter-list*s have the same length, corresponding
|
| 3184 |
*template-parameter*s are equivalent and are both declared with
|
| 3185 |
*type-constraint*s that are equivalent if either *template-parameter* is
|
| 3186 |
declared with a *type-constraint*, and if either *template-head* has a
|
|
@@ -3200,25 +3175,24 @@ When determining whether types or *type-constraint*s are equivalent, the
|
|
| 3200 |
rules above are used to compare expressions involving template
|
| 3201 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 3202 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 3203 |
template argument lists.
|
| 3204 |
|
| 3205 |
-
|
| 3206 |
-
|
| 3207 |
-
|
| 3208 |
-
|
| 3209 |
-
expressions involving template parameters. Two function templates are
|
| 3210 |
-
*functionally equivalent* if they are declared in the same scope, have
|
| 3211 |
-
the same name, accept and are satisfied by the same set of template
|
| 3212 |
-
argument lists, and have return types and parameter lists that are
|
| 3213 |
-
functionally equivalent using the rules described above to compare
|
| 3214 |
-
expressions involving template parameters. If the validity or meaning of
|
| 3215 |
-
the program depends on whether two constructs are equivalent, and they
|
| 3216 |
-
are functionally equivalent but not equivalent, the program is
|
| 3217 |
-
ill-formed, no diagnostic required.
|
| 3218 |
|
| 3219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3220 |
|
| 3221 |
This rule guarantees that equivalent declarations will be linked with
|
| 3222 |
one another, while not requiring implementations to use heroic efforts
|
| 3223 |
to guarantee that functionally equivalent declarations will be treated
|
| 3224 |
as distinct. For example, the last two declarations are functionally
|
|
@@ -3240,24 +3214,23 @@ template <int I> void f(A<I>, A<I+1+2+3+4>);
|
|
| 3240 |
|
| 3241 |
— *end note*]
|
| 3242 |
|
| 3243 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 3244 |
|
| 3245 |
-
If
|
| 3246 |
-
|
| 3247 |
-
|
| 3248 |
-
|
| 3249 |
-
|
| 3250 |
-
|
| 3251 |
-
specialization refers:
|
| 3252 |
|
| 3253 |
- during overload resolution for a call to a function template
|
| 3254 |
specialization [[over.match.best]];
|
| 3255 |
- when the address of a function template specialization is taken;
|
| 3256 |
- when a placement operator delete that is a function template
|
| 3257 |
-
specialization is selected to match a placement operator new
|
| 3258 |
-
[[basic.stc.dynamic.deallocation]], [[expr.new]]
|
| 3259 |
- when a friend function declaration [[temp.friend]], an explicit
|
| 3260 |
instantiation [[temp.explicit]] or an explicit specialization
|
| 3261 |
[[temp.expl.spec]] refers to a function template specialization.
|
| 3262 |
|
| 3263 |
Partial ordering selects which of two function templates is more
|
|
@@ -3407,12 +3380,12 @@ template<class T, class... U> void f(T, U...); // #1
|
|
| 3407 |
template<class T > void f(T); // #2
|
| 3408 |
template<class T, class... U> void g(T*, U...); // #3
|
| 3409 |
template<class T > void g(T); // #4
|
| 3410 |
|
| 3411 |
void h(int i) {
|
| 3412 |
-
f(&i); // OK
|
| 3413 |
-
g(&i); // OK
|
| 3414 |
}
|
| 3415 |
```
|
| 3416 |
|
| 3417 |
— *end example*]
|
| 3418 |
|
|
@@ -3462,20 +3435,20 @@ template <typename T> concept C = True<T>;
|
|
| 3462 |
|
| 3463 |
void f(C auto &, auto &) = delete;
|
| 3464 |
template <C Q> void f(Q &, C auto &);
|
| 3465 |
|
| 3466 |
void g(struct A *ap, struct B *bp) {
|
| 3467 |
-
f(*ap, *bp); // OK
|
| 3468 |
}
|
| 3469 |
|
| 3470 |
template <typename T, typename U> struct X {};
|
| 3471 |
|
| 3472 |
template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
|
| 3473 |
template <C T, C U, C V> bool operator==(T, X<U, V>);
|
| 3474 |
|
| 3475 |
void h() {
|
| 3476 |
-
X<void *, int>{} == 0; // OK
|
| 3477 |
}
|
| 3478 |
```
|
| 3479 |
|
| 3480 |
— *end example*]
|
| 3481 |
|
|
@@ -3496,11 +3469,11 @@ it is equivalent to the associated type obtained by substitution of its
|
|
| 3496 |
[*Example 1*:
|
| 3497 |
|
| 3498 |
``` cpp
|
| 3499 |
template<class T> struct Alloc { ... };
|
| 3500 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 3501 |
-
Vec<int> v; // same as vector<int, Alloc<int>
|
| 3502 |
|
| 3503 |
template<class T>
|
| 3504 |
void process(Vec<T>& v)
|
| 3505 |
{ ... }
|
| 3506 |
|
|
@@ -3513,11 +3486,11 @@ template<template<class> class TT>
|
|
| 3513 |
|
| 3514 |
f(v); // error: Vec not deduced
|
| 3515 |
|
| 3516 |
template<template<class,class> class TT>
|
| 3517 |
void g(TT<int, Alloc<int>>);
|
| 3518 |
-
g(v); // OK
|
| 3519 |
```
|
| 3520 |
|
| 3521 |
— *end example*]
|
| 3522 |
|
| 3523 |
However, if the *template-id* is dependent, subsequent template argument
|
|
@@ -3569,20 +3542,21 @@ template <class T>
|
|
| 3569 |
A *concept* is a template that defines constraints on its template
|
| 3570 |
arguments.
|
| 3571 |
|
| 3572 |
``` bnf
|
| 3573 |
concept-definition:
|
| 3574 |
-
concept concept-name '=' constraint-expression ';'
|
| 3575 |
```
|
| 3576 |
|
| 3577 |
``` bnf
|
| 3578 |
concept-name:
|
| 3579 |
identifier
|
| 3580 |
```
|
| 3581 |
|
| 3582 |
A *concept-definition* declares a concept. Its *identifier* becomes a
|
| 3583 |
-
*concept-name* referring to that concept within its scope.
|
|
|
|
| 3584 |
|
| 3585 |
[*Example 1*:
|
| 3586 |
|
| 3587 |
``` cpp
|
| 3588 |
template<typename T>
|
|
@@ -3598,83 +3572,133 @@ template<C T> // C, as a type-constraint, constrains f2(T)
|
|
| 3598 |
T f2(T x) { return x; }
|
| 3599 |
```
|
| 3600 |
|
| 3601 |
— *end example*]
|
| 3602 |
|
| 3603 |
-
A *concept-definition* shall
|
| 3604 |
[[basic.scope.namespace]].
|
| 3605 |
|
| 3606 |
A concept shall not have associated constraints [[temp.constr.decl]].
|
| 3607 |
|
| 3608 |
A concept is not instantiated [[temp.spec]].
|
| 3609 |
|
| 3610 |
[*Note 1*: A concept-id [[temp.names]] is evaluated as an expression. A
|
| 3611 |
concept cannot be explicitly instantiated [[temp.explicit]], explicitly
|
| 3612 |
-
specialized [[temp.expl.spec]], or partially specialized
|
|
|
|
| 3613 |
|
| 3614 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 3615 |
operand [[expr.context]].
|
| 3616 |
|
| 3617 |
The first declared template parameter of a concept definition is its
|
| 3618 |
*prototype parameter*. A *type concept* is a concept whose prototype
|
| 3619 |
parameter is a type *template-parameter*.
|
| 3620 |
|
| 3621 |
## Name resolution <a id="temp.res">[[temp.res]]</a>
|
| 3622 |
|
| 3623 |
-
|
| 3624 |
|
| 3625 |
-
|
| 3626 |
-
|
| 3627 |
-
|
| 3628 |
-
|
|
|
|
|
|
|
|
|
|
| 3629 |
|
| 3630 |
-
|
| 3631 |
-
|
| 3632 |
-
|
| 3633 |
-
|
|
|
|
|
|
|
| 3634 |
|
| 3635 |
[*Example 1*:
|
| 3636 |
|
| 3637 |
``` cpp
|
| 3638 |
-
|
| 3639 |
-
|
| 3640 |
-
|
| 3641 |
-
|
| 3642 |
-
|
| 3643 |
-
|
| 3644 |
-
|
| 3645 |
-
|
| 3646 |
-
|
| 3647 |
-
|
| 3648 |
-
|
| 3649 |
-
|
| 3650 |
-
|
| 3651 |
-
|
| 3652 |
-
|
| 3653 |
-
|
| 3654 |
-
|
| 3655 |
-
|
| 3656 |
-
|
|
|
|
| 3657 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3658 |
};
|
|
|
|
|
|
|
| 3659 |
```
|
| 3660 |
|
|
|
|
|
|
|
|
|
|
| 3661 |
— *end example*]
|
| 3662 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3663 |
``` bnf
|
| 3664 |
typename-specifier:
|
| 3665 |
typename nested-name-specifier identifier
|
| 3666 |
typename nested-name-specifier 'templateₒₚₜ ' simple-template-id
|
| 3667 |
```
|
| 3668 |
|
| 3669 |
-
|
| 3670 |
-
*
|
| 3671 |
-
|
| 3672 |
-
|
| 3673 |
-
|
| 3674 |
|
| 3675 |
-
[*
|
|
|
|
|
|
|
|
|
|
| 3676 |
|
| 3677 |
``` cpp
|
| 3678 |
struct A {
|
| 3679 |
struct X { };
|
| 3680 |
int X;
|
|
@@ -3686,52 +3710,44 @@ template<class T> void f(T t) {
|
|
| 3686 |
typename T::X x;
|
| 3687 |
}
|
| 3688 |
void foo() {
|
| 3689 |
A a;
|
| 3690 |
B b;
|
| 3691 |
-
f(b); // OK
|
| 3692 |
f(a); // error: T::X refers to the data member A::X not the struct A::X
|
| 3693 |
}
|
| 3694 |
```
|
| 3695 |
|
| 3696 |
— *end example*]
|
| 3697 |
|
| 3698 |
-
A qualified name
|
| 3699 |
-
|
| 3700 |
-
assumed to name a type, without the use of the `typename` keyword. In a
|
| 3701 |
-
*nested-name-specifier* that immediately contains a
|
| 3702 |
-
*nested-name-specifier* that depends on a template parameter, the
|
| 3703 |
-
*identifier* or *simple-template-id* is implicitly assumed to name a
|
| 3704 |
-
type, without the use of the `typename` keyword.
|
| 3705 |
|
| 3706 |
-
|
| 3707 |
-
|
| 3708 |
-
|
| 3709 |
-
|
| 3710 |
-
|
| 3711 |
-
-
|
| 3712 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3713 |
- *simple-declaration* or a *function-definition* in namespace scope,
|
| 3714 |
- *member-declaration*,
|
| 3715 |
-
- *parameter-declaration* in a *member-declaration*
|
| 3716 |
*parameter-declaration* appears in a default argument,
|
| 3717 |
- *parameter-declaration* in a *declarator* of a function or function
|
| 3718 |
template declaration whose *declarator-id* is qualified, unless that
|
| 3719 |
*parameter-declaration* appears in a default argument,
|
| 3720 |
- *parameter-declaration* in a *lambda-declarator* or
|
| 3721 |
*requirement-parameter-list*, unless that *parameter-declaration*
|
| 3722 |
appears in a default argument, or
|
| 3723 |
- *parameter-declaration* of a (non-type) *template-parameter*.
|
| 3724 |
|
| 3725 |
-
|
| 3726 |
-
in a *type-id*, *new-type-id*, or *defining-type-id* and the smallest
|
| 3727 |
-
enclosing *type-id*, *new-type-id*, or *defining-type-id* is a
|
| 3728 |
-
*new-type-id*, *defining-type-id*, *trailing-return-type*, default
|
| 3729 |
-
argument of a *type-parameter* of a template, or *type-id* of a
|
| 3730 |
-
`static_cast`, `const_cast`, `reinterpret_cast`, or `dynamic_cast`.
|
| 3731 |
-
|
| 3732 |
-
[*Example 3*:
|
| 3733 |
|
| 3734 |
``` cpp
|
| 3735 |
template<class T> T::R f(); // OK, return type of a function declaration at global scope
|
| 3736 |
template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
|
| 3737 |
// a void variable template
|
|
@@ -3749,15 +3765,16 @@ template<typename T> void f() {
|
|
| 3749 |
}
|
| 3750 |
```
|
| 3751 |
|
| 3752 |
— *end example*]
|
| 3753 |
|
| 3754 |
-
A *qualified-id*
|
| 3755 |
-
|
| 3756 |
-
|
|
|
|
| 3757 |
|
| 3758 |
-
[*Example
|
| 3759 |
|
| 3760 |
``` cpp
|
| 3761 |
template <class T> void f(int i) {
|
| 3762 |
T::x * i; // expression, not the declaration of a variable i
|
| 3763 |
}
|
|
@@ -3776,63 +3793,53 @@ int main() {
|
|
| 3776 |
}
|
| 3777 |
```
|
| 3778 |
|
| 3779 |
— *end example*]
|
| 3780 |
|
| 3781 |
-
Within the definition of a class template or within the definition of a
|
| 3782 |
-
member of a class template following the *declarator-id*, the keyword
|
| 3783 |
-
`typename` is not required when referring to a member of the current
|
| 3784 |
-
instantiation [[temp.dep.type]].
|
| 3785 |
-
|
| 3786 |
-
[*Example 5*:
|
| 3787 |
-
|
| 3788 |
-
``` cpp
|
| 3789 |
-
template<class T> struct A {
|
| 3790 |
-
typedef int B;
|
| 3791 |
-
B b; // OK, no typename required
|
| 3792 |
-
};
|
| 3793 |
-
```
|
| 3794 |
-
|
| 3795 |
-
— *end example*]
|
| 3796 |
-
|
| 3797 |
The validity of a template may be checked prior to any instantiation.
|
| 3798 |
|
| 3799 |
-
[*Note
|
| 3800 |
every template to be checked in this way. — *end note*]
|
| 3801 |
|
| 3802 |
The program is ill-formed, no diagnostic required, if:
|
| 3803 |
|
| 3804 |
-
- no valid specialization
|
| 3805 |
-
|
| 3806 |
-
and the template is not
|
| 3807 |
-
|
| 3808 |
-
|
|
|
|
|
|
|
|
|
|
| 3809 |
- every valid specialization of a variadic template requires an empty
|
| 3810 |
template parameter pack, or
|
| 3811 |
- a hypothetical instantiation of a template immediately following its
|
| 3812 |
definition would be ill-formed due to a construct that does not depend
|
| 3813 |
on a template parameter, or
|
| 3814 |
- the interpretation of such a construct in the hypothetical
|
| 3815 |
instantiation is different from the interpretation of the
|
| 3816 |
corresponding construct in any actual instantiation of the template.
|
| 3817 |
-
|
|
|
|
|
|
|
| 3818 |
This can happen in situations including the following:
|
|
|
|
| 3819 |
- a type used in a non-dependent name is incomplete at the point at
|
| 3820 |
which a template is defined but is complete at the point at which an
|
| 3821 |
instantiation is performed, or
|
| 3822 |
- lookup for a name in the template definition found a
|
| 3823 |
-
|
| 3824 |
-
|
| 3825 |
-
|
| 3826 |
-
|
| 3827 |
-
|
| 3828 |
-
|
| 3829 |
-
|
| 3830 |
- constant expression evaluation [[expr.const]] within the template
|
| 3831 |
instantiation uses
|
| 3832 |
-
|
| 3833 |
-
|
| 3834 |
- the value of a `constexpr` object or
|
| 3835 |
- the value of a reference or
|
| 3836 |
- the definition of a constexpr function,
|
| 3837 |
|
| 3838 |
and that entity was not defined when the template was defined, or
|
|
@@ -3846,23 +3853,25 @@ The program is ill-formed, no diagnostic required, if:
|
|
| 3846 |
— *end note*]
|
| 3847 |
|
| 3848 |
Otherwise, no diagnostic shall be issued for a template for which a
|
| 3849 |
valid specialization can be generated.
|
| 3850 |
|
| 3851 |
-
[*Note
|
| 3852 |
according to the other rules in this document. Exactly when these errors
|
| 3853 |
are diagnosed is a quality of implementation issue. — *end note*]
|
| 3854 |
|
| 3855 |
-
[*Example
|
| 3856 |
|
| 3857 |
``` cpp
|
| 3858 |
int j;
|
| 3859 |
template<class T> class X {
|
| 3860 |
void f(T t, int i, char* p) {
|
| 3861 |
t = i; // diagnosed if X::f is instantiated, and the assignment to t is an error
|
| 3862 |
p = i; // may be diagnosed even if X::f is not instantiated
|
| 3863 |
p = j; // may be diagnosed even if X::f is not instantiated
|
|
|
|
|
|
|
| 3864 |
}
|
| 3865 |
void g(T t) {
|
| 3866 |
+; // may be diagnosed even if X::g is not instantiated
|
| 3867 |
}
|
| 3868 |
};
|
|
@@ -3874,79 +3883,11 @@ template<class... T> union X : T... { }; // error: union with base cl
|
|
| 3874 |
template<class... T> struct A : T..., T... { }; // error: duplicate base class
|
| 3875 |
```
|
| 3876 |
|
| 3877 |
— *end example*]
|
| 3878 |
|
| 3879 |
-
|
| 3880 |
-
definition, the usual lookup rules ([[basic.lookup.unqual]],
|
| 3881 |
-
[[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
|
| 3882 |
-
names dependent on the template parameters is postponed until the actual
|
| 3883 |
-
template argument is known [[temp.dep]].
|
| 3884 |
-
|
| 3885 |
-
[*Example 7*:
|
| 3886 |
-
|
| 3887 |
-
``` cpp
|
| 3888 |
-
#include <iostream>
|
| 3889 |
-
using namespace std;
|
| 3890 |
-
|
| 3891 |
-
template<class T> class Set {
|
| 3892 |
-
T* p;
|
| 3893 |
-
int cnt;
|
| 3894 |
-
public:
|
| 3895 |
-
Set();
|
| 3896 |
-
Set<T>(const Set<T>&);
|
| 3897 |
-
void printall() {
|
| 3898 |
-
for (int i = 0; i<cnt; i++)
|
| 3899 |
-
cout << p[i] << '\n';
|
| 3900 |
-
}
|
| 3901 |
-
};
|
| 3902 |
-
```
|
| 3903 |
-
|
| 3904 |
-
In the example, `i` is the local variable `i` declared in `printall`,
|
| 3905 |
-
`cnt` is the member `cnt` declared in `Set`, and `cout` is the standard
|
| 3906 |
-
output stream declared in `iostream`. However, not every declaration can
|
| 3907 |
-
be found this way; the resolution of some names must be postponed until
|
| 3908 |
-
the actual *template-argument*s are known. For example, even though the
|
| 3909 |
-
name `operator<<` is known within the definition of `printall()` and a
|
| 3910 |
-
declaration of it can be found in `<iostream>`, the actual declaration
|
| 3911 |
-
of `operator<<` needed to print `p[i]` cannot be known until it is known
|
| 3912 |
-
what type `T` is [[temp.dep]].
|
| 3913 |
-
|
| 3914 |
-
— *end example*]
|
| 3915 |
-
|
| 3916 |
-
If a name does not depend on a *template-parameter* (as defined in
|
| 3917 |
-
[[temp.dep]]), a declaration (or set of declarations) for that name
|
| 3918 |
-
shall be in scope at the point where the name appears in the template
|
| 3919 |
-
definition; the name is bound to the declaration (or declarations) found
|
| 3920 |
-
at that point and this binding is not affected by declarations that are
|
| 3921 |
-
visible at the point of instantiation.
|
| 3922 |
-
|
| 3923 |
-
[*Example 8*:
|
| 3924 |
-
|
| 3925 |
-
``` cpp
|
| 3926 |
-
void f(char);
|
| 3927 |
-
|
| 3928 |
-
template<class T> void g(T t) {
|
| 3929 |
-
f(1); // f(char)
|
| 3930 |
-
f(T(1)); // dependent
|
| 3931 |
-
f(t); // dependent
|
| 3932 |
-
dd++; // not dependent; error: declaration for dd not found
|
| 3933 |
-
}
|
| 3934 |
-
|
| 3935 |
-
enum E { e };
|
| 3936 |
-
void f(E);
|
| 3937 |
-
|
| 3938 |
-
double dd;
|
| 3939 |
-
void h() {
|
| 3940 |
-
g(e); // will cause one call of f(char) followed by two calls of f(E)
|
| 3941 |
-
g('a'); // will cause three calls of f(char)
|
| 3942 |
-
}
|
| 3943 |
-
```
|
| 3944 |
-
|
| 3945 |
-
— *end example*]
|
| 3946 |
-
|
| 3947 |
-
[*Note 5*: For purposes of name lookup, default arguments and
|
| 3948 |
*noexcept-specifier*s of function templates and default arguments and
|
| 3949 |
*noexcept-specifier*s of member functions of class templates are
|
| 3950 |
considered definitions [[temp.decls]]. — *end note*]
|
| 3951 |
|
| 3952 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
|
@@ -3957,18 +3898,18 @@ as a *template-name* or a *type-name*. When it is used with a
|
|
| 3957 |
*template-argument-list*, as a *template-argument* for a template
|
| 3958 |
*template-parameter*, or as the final identifier in the
|
| 3959 |
*elaborated-type-specifier* of a friend class template declaration, it
|
| 3960 |
is a *template-name* that refers to the class template itself.
|
| 3961 |
Otherwise, it is a *type-name* equivalent to the *template-name*
|
| 3962 |
-
followed by the
|
| 3963 |
-
|
|
|
|
| 3964 |
|
| 3965 |
-
|
| 3966 |
-
specialization
|
| 3967 |
-
|
| 3968 |
-
|
| 3969 |
-
specialization enclosed in `<>`.
|
| 3970 |
|
| 3971 |
[*Example 1*:
|
| 3972 |
|
| 3973 |
``` cpp
|
| 3974 |
template<template<class> class T> class A { };
|
|
@@ -3985,11 +3926,11 @@ template<> class Y<int> {
|
|
| 3985 |
|
| 3986 |
— *end example*]
|
| 3987 |
|
| 3988 |
The injected-class-name of a class template or class template
|
| 3989 |
specialization can be used as either a *template-name* or a *type-name*
|
| 3990 |
-
wherever it is
|
| 3991 |
|
| 3992 |
[*Example 2*:
|
| 3993 |
|
| 3994 |
``` cpp
|
| 3995 |
template <class T> struct Base {
|
|
@@ -3998,12 +3939,12 @@ template <class T> struct Base {
|
|
| 3998 |
|
| 3999 |
template <class T> struct Derived: public Base<T> {
|
| 4000 |
typename Derived::Base* p; // meaning Derived::Base<T>
|
| 4001 |
};
|
| 4002 |
|
| 4003 |
-
template<class T, template<class> class U = T::
|
| 4004 |
-
Third<Derived<int> > t; // OK
|
| 4005 |
```
|
| 4006 |
|
| 4007 |
— *end example*]
|
| 4008 |
|
| 4009 |
A lookup that finds an injected-class-name [[class.member.lookup]] can
|
|
@@ -4041,135 +3982,105 @@ template<class T> class X {
|
|
| 4041 |
};
|
| 4042 |
```
|
| 4043 |
|
| 4044 |
— *end example*]
|
| 4045 |
|
| 4046 |
-
The name of a *template-parameter* shall not be
|
| 4047 |
-
|
| 4048 |
-
|
| 4049 |
|
| 4050 |
[*Example 5*:
|
| 4051 |
|
| 4052 |
``` cpp
|
| 4053 |
template<class T, int i> class Y {
|
| 4054 |
-
int T; // error: template-parameter
|
| 4055 |
void f() {
|
| 4056 |
-
char T; // error: template-parameter
|
| 4057 |
}
|
|
|
|
| 4058 |
};
|
| 4059 |
|
| 4060 |
-
template<class X> class X; // error: template-parameter
|
| 4061 |
```
|
| 4062 |
|
| 4063 |
— *end example*]
|
| 4064 |
|
| 4065 |
-
|
| 4066 |
-
|
| 4067 |
-
|
| 4068 |
-
|
| 4069 |
-
|
|
|
|
|
|
|
|
|
|
| 4070 |
|
| 4071 |
[*Example 6*:
|
| 4072 |
|
| 4073 |
``` cpp
|
| 4074 |
-
|
| 4075 |
-
|
|
|
|
|
|
|
| 4076 |
typedef void C;
|
| 4077 |
void f();
|
| 4078 |
template<class U> void g(U);
|
| 4079 |
};
|
| 4080 |
-
|
| 4081 |
-
template<class B> void A<B>::f() {
|
| 4082 |
-
B b; // A's B, not the template parameter
|
| 4083 |
}
|
| 4084 |
|
| 4085 |
-
template<class
|
| 4086 |
-
|
| 4087 |
-
C c; // the template parameter C, not A's C
|
| 4088 |
}
|
| 4089 |
-
```
|
| 4090 |
-
|
| 4091 |
-
— *end example*]
|
| 4092 |
-
|
| 4093 |
-
In the definition of a member of a class template that appears outside
|
| 4094 |
-
of the namespace containing the class template definition, the name of a
|
| 4095 |
-
*template-parameter* hides the name of a member of this namespace.
|
| 4096 |
|
| 4097 |
-
|
| 4098 |
-
|
| 4099 |
-
|
| 4100 |
-
namespace N {
|
| 4101 |
-
class C { };
|
| 4102 |
-
template<class T> class B {
|
| 4103 |
-
void f(T);
|
| 4104 |
-
};
|
| 4105 |
-
}
|
| 4106 |
-
template<class C> void N::B<C>::f(C) {
|
| 4107 |
-
C b; // C is the template parameter, not N::C
|
| 4108 |
}
|
| 4109 |
```
|
| 4110 |
|
| 4111 |
— *end example*]
|
| 4112 |
|
| 4113 |
-
In the definition of a class template or in the definition of a member
|
| 4114 |
-
of such a template that appears outside of the template definition, for
|
| 4115 |
-
each non-dependent base class [[temp.dep.type]], if the name of the base
|
| 4116 |
-
class or the name of a member of the base class is the same as the name
|
| 4117 |
-
of a *template-parameter*, the base class name or member name hides the
|
| 4118 |
-
*template-parameter* name [[basic.scope.hiding]].
|
| 4119 |
-
|
| 4120 |
-
[*Example 8*:
|
| 4121 |
-
|
| 4122 |
-
``` cpp
|
| 4123 |
-
struct A {
|
| 4124 |
-
struct B { ... };
|
| 4125 |
-
int a;
|
| 4126 |
-
int Y;
|
| 4127 |
-
};
|
| 4128 |
-
|
| 4129 |
-
template<class B, class a> struct X : A {
|
| 4130 |
-
B b; // A's B
|
| 4131 |
-
a b; // error: A's a isn't a type name
|
| 4132 |
-
};
|
| 4133 |
-
```
|
| 4134 |
-
|
| 4135 |
-
— *end example*]
|
| 4136 |
-
|
| 4137 |
### Dependent names <a id="temp.dep">[[temp.dep]]</a>
|
| 4138 |
|
|
|
|
|
|
|
| 4139 |
Inside a template, some constructs have semantics which may differ from
|
| 4140 |
one instantiation to another. Such a construct *depends* on the template
|
| 4141 |
parameters. In particular, types and expressions may depend on the type
|
| 4142 |
and/or value of template parameters (as determined by the template
|
| 4143 |
arguments) and this determines the context for name lookup for certain
|
| 4144 |
names. An expression may be *type-dependent* (that is, its type may
|
| 4145 |
depend on a template parameter) or *value-dependent* (that is, its value
|
| 4146 |
when evaluated as a constant expression [[expr.const]] may depend on a
|
| 4147 |
-
template parameter) as described
|
| 4148 |
|
| 4149 |
-
|
|
|
|
| 4150 |
|
| 4151 |
``` bnf
|
| 4152 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 4153 |
```
|
| 4154 |
|
| 4155 |
-
where the *postfix-expression* is an *unqualified-id*
|
| 4156 |
-
*unqualified-id* denotes a *dependent name* if
|
| 4157 |
|
| 4158 |
- any of the expressions in the *expression-list* is a pack expansion
|
| 4159 |
-
[[temp.variadic]],
|
| 4160 |
- any of the expressions or *braced-init-list*s in the *expression-list*
|
| 4161 |
is type-dependent [[temp.dep.expr]], or
|
| 4162 |
- the *unqualified-id* is a *template-id* in which any of the template
|
| 4163 |
arguments depends on a template parameter.
|
| 4164 |
|
| 4165 |
-
|
| 4166 |
-
|
| 4167 |
|
| 4168 |
-
|
| 4169 |
-
|
| 4170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4171 |
[[temp.dep.candidate]]. — *end note*]
|
| 4172 |
|
| 4173 |
[*Example 1*:
|
| 4174 |
|
| 4175 |
``` cpp
|
|
@@ -4185,88 +4096,30 @@ template<class T> struct X : B<T> {
|
|
| 4185 |
The base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 4186 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 4187 |
|
| 4188 |
— *end example*]
|
| 4189 |
|
| 4190 |
-
In the definition of a class or class template, the scope of a dependent
|
| 4191 |
-
base class [[temp.dep.type]] is not examined during unqualified name
|
| 4192 |
-
lookup either at the point of definition of the class template or member
|
| 4193 |
-
or during an instantiation of the class template or member.
|
| 4194 |
-
|
| 4195 |
-
[*Example 2*:
|
| 4196 |
-
|
| 4197 |
-
``` cpp
|
| 4198 |
-
typedef double A;
|
| 4199 |
-
template<class T> class B {
|
| 4200 |
-
typedef int A;
|
| 4201 |
-
};
|
| 4202 |
-
template<class T> struct X : B<T> {
|
| 4203 |
-
A a; // a has type double
|
| 4204 |
-
};
|
| 4205 |
-
```
|
| 4206 |
-
|
| 4207 |
-
The type name `A` in the definition of `X<T>` binds to the typedef name
|
| 4208 |
-
defined in the global namespace scope, not to the typedef name defined
|
| 4209 |
-
in the base class `B<T>`.
|
| 4210 |
-
|
| 4211 |
-
— *end example*]
|
| 4212 |
-
|
| 4213 |
-
[*Example 3*:
|
| 4214 |
-
|
| 4215 |
-
``` cpp
|
| 4216 |
-
struct A {
|
| 4217 |
-
struct B { ... };
|
| 4218 |
-
int a;
|
| 4219 |
-
int Y;
|
| 4220 |
-
};
|
| 4221 |
-
|
| 4222 |
-
int a;
|
| 4223 |
-
|
| 4224 |
-
template<class T> struct Y : T {
|
| 4225 |
-
struct B { ... };
|
| 4226 |
-
B b; // The B defined in Y
|
| 4227 |
-
void f(int i) { a = i; } // ::a
|
| 4228 |
-
Y* p; // Y<T>
|
| 4229 |
-
};
|
| 4230 |
-
|
| 4231 |
-
Y<A> ya;
|
| 4232 |
-
```
|
| 4233 |
-
|
| 4234 |
-
The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
|
| 4235 |
-
not affect the binding of names in `Y<A>`.
|
| 4236 |
-
|
| 4237 |
-
— *end example*]
|
| 4238 |
-
|
| 4239 |
#### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
|
| 4240 |
|
| 4241 |
-
A name refers to the *current instantiation* if it is
|
| 4242 |
|
| 4243 |
- in the definition of a class template, a nested class of a class
|
| 4244 |
template, a member of a class template, or a member of a nested class
|
| 4245 |
of a class template, the injected-class-name [[class.pre]] of the
|
| 4246 |
class template or nested class,
|
| 4247 |
- in the definition of a primary class template or a member of a primary
|
| 4248 |
class template, the name of the class template followed by the
|
| 4249 |
-
template argument list of
|
| 4250 |
-
|
| 4251 |
- in the definition of a nested class of a class template, the name of
|
| 4252 |
the nested class referenced as a member of the current instantiation,
|
| 4253 |
or
|
| 4254 |
-
- in the definition of a partial specialization or a
|
| 4255 |
-
|
| 4256 |
-
template
|
| 4257 |
-
|
| 4258 |
-
|
| 4259 |
-
pack expansion [[temp.variadic]] whose pattern is the name of the
|
| 4260 |
-
template parameter pack.
|
| 4261 |
-
|
| 4262 |
-
The template argument list of a primary template is a template argument
|
| 4263 |
-
list in which the nᵗʰ template argument has the value of the nᵗʰ
|
| 4264 |
-
template parameter of the class template. If the nᵗʰ template parameter
|
| 4265 |
-
is a template parameter pack [[temp.variadic]], the nᵗʰ template
|
| 4266 |
-
argument is a pack expansion [[temp.variadic]] whose pattern is the name
|
| 4267 |
-
of the template parameter pack.
|
| 4268 |
|
| 4269 |
A template argument that is equivalent to a template parameter can be
|
| 4270 |
used in place of that template parameter in a reference to the current
|
| 4271 |
instantiation. For a template *type-parameter*, a template argument is
|
| 4272 |
equivalent to a template parameter if it denotes the same type. For a
|
|
@@ -4348,32 +4201,17 @@ template<class T> struct A<T>::B::C : A<T> {
|
|
| 4348 |
|
| 4349 |
— *end example*]
|
| 4350 |
|
| 4351 |
— *end note*]
|
| 4352 |
|
| 4353 |
-
A name is a *member of
|
|
|
|
| 4354 |
|
| 4355 |
-
-
|
| 4356 |
-
|
| 4357 |
-
|
| 4358 |
-
|
| 4359 |
-
template. — *end note*]
|
| 4360 |
-
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 4361 |
-
current instantiation and that, when looked up, refers to at least one
|
| 4362 |
-
member of a class that is the current instantiation or a non-dependent
|
| 4363 |
-
base class thereof. \[*Note 4*: If no such member is found, and the
|
| 4364 |
-
current instantiation has any dependent base classes, then the
|
| 4365 |
-
*qualified-id* is a member of an unknown specialization; see
|
| 4366 |
-
below. — *end note*]
|
| 4367 |
-
- An *id-expression* denoting the member in a class member access
|
| 4368 |
-
expression [[expr.ref]] for which the type of the object expression is
|
| 4369 |
-
the current instantiation, and the *id-expression*, when looked up
|
| 4370 |
-
[[basic.lookup.classref]], refers to at least one member of a class
|
| 4371 |
-
that is the current instantiation or a non-dependent base class
|
| 4372 |
-
thereof. \[*Note 5*: If no such member is found, and the current
|
| 4373 |
-
instantiation has any dependent base classes, then the *id-expression*
|
| 4374 |
-
is a member of an unknown specialization; see below. — *end note*]
|
| 4375 |
|
| 4376 |
[*Example 3*:
|
| 4377 |
|
| 4378 |
``` cpp
|
| 4379 |
template <class T> class A {
|
|
@@ -4389,66 +4227,51 @@ template <class T> int A<T>::f() {
|
|
| 4389 |
}
|
| 4390 |
```
|
| 4391 |
|
| 4392 |
— *end example*]
|
| 4393 |
|
| 4394 |
-
A name
|
| 4395 |
-
member of the current instantiation that, when
|
| 4396 |
-
|
|
|
|
|
|
|
| 4397 |
|
| 4398 |
-
A name is
|
| 4399 |
|
| 4400 |
-
-
|
| 4401 |
-
dependent
|
| 4402 |
-
-
|
| 4403 |
-
|
| 4404 |
-
|
| 4405 |
-
|
| 4406 |
-
|
| 4407 |
-
|
| 4408 |
-
|
| 4409 |
-
- the type of the object expression is the current instantiation, the
|
| 4410 |
-
current instantiation has at least one dependent base class, and
|
| 4411 |
-
name lookup of the *id-expression* does not find a member of a class
|
| 4412 |
-
that is the current instantiation or a non-dependent base class
|
| 4413 |
-
thereof; or
|
| 4414 |
-
- the type of the object expression is not the current instantiation
|
| 4415 |
-
and the object expression is type-dependent.
|
| 4416 |
-
|
| 4417 |
-
If a *qualified-id* in which the *nested-name-specifier* refers to the
|
| 4418 |
-
current instantiation is not a member of the current instantiation or a
|
| 4419 |
-
member of an unknown specialization, the program is ill-formed even if
|
| 4420 |
-
the template containing the *qualified-id* is not instantiated; no
|
| 4421 |
-
diagnostic required. Similarly, if the *id-expression* in a class member
|
| 4422 |
-
access expression for which the type of the object expression is the
|
| 4423 |
-
current instantiation does not refer to a member of the current
|
| 4424 |
-
instantiation or a member of an unknown specialization, the program is
|
| 4425 |
-
ill-formed even if the template containing the member access expression
|
| 4426 |
-
is not instantiated; no diagnostic required.
|
| 4427 |
|
| 4428 |
[*Example 4*:
|
| 4429 |
|
| 4430 |
``` cpp
|
| 4431 |
-
|
| 4432 |
-
|
| 4433 |
-
|
| 4434 |
-
A<T>::type i; // OK: refers to a member of the current instantiation
|
| 4435 |
-
typename A<T>::other j; // error: neither a member of the current instantiation nor
|
| 4436 |
-
// a member of an unknown specialization
|
| 4437 |
-
}
|
| 4438 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4439 |
```
|
| 4440 |
|
| 4441 |
— *end example*]
|
| 4442 |
|
| 4443 |
If, for a given set of template arguments, a specialization of a
|
| 4444 |
template is instantiated that refers to a member of the current
|
| 4445 |
-
instantiation with a
|
| 4446 |
-
|
| 4447 |
-
|
| 4448 |
-
lookup
|
| 4449 |
-
context, name lookup is ambiguous.
|
| 4450 |
|
| 4451 |
[*Example 5*:
|
| 4452 |
|
| 4453 |
``` cpp
|
| 4454 |
struct A {
|
|
@@ -4464,84 +4287,87 @@ struct C : A, T {
|
|
| 4464 |
int f() { return this->m; } // finds A::m in the template definition context
|
| 4465 |
int g() { return m; } // finds A::m in the template definition context
|
| 4466 |
};
|
| 4467 |
|
| 4468 |
template int C<B>::f(); // error: finds both A::m and B::m
|
| 4469 |
-
template int C<B>::g(); // OK
|
| 4470 |
-
// does not occur in the template definition context; see~[class.mfct.non
|
| 4471 |
```
|
| 4472 |
|
| 4473 |
— *end example*]
|
| 4474 |
|
| 4475 |
A type is dependent if it is
|
| 4476 |
|
| 4477 |
- a template parameter,
|
| 4478 |
-
-
|
| 4479 |
-
- a nested class or enumeration that is a
|
| 4480 |
-
current instantiation,
|
| 4481 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 4482 |
- a compound type constructed from any dependent type,
|
| 4483 |
- an array type whose element type is dependent or whose bound (if any)
|
| 4484 |
is value-dependent,
|
|
|
|
|
|
|
| 4485 |
- a function type whose exception specification is value-dependent,
|
| 4486 |
- denoted by a *simple-template-id* in which either the template name is
|
| 4487 |
a template parameter or any of the template arguments is a dependent
|
| 4488 |
type or an expression that is type-dependent or value-dependent or is
|
| 4489 |
-
a pack expansion
|
| 4490 |
-
[[class.pre]] of a class template used without a
|
| 4491 |
-
*template-argument-list*. — *end note*] , or
|
| 4492 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 4493 |
type-dependent [[temp.dep.expr]].
|
| 4494 |
|
| 4495 |
-
[*Note
|
| 4496 |
simply refer to other types, a name that refers to a typedef that is a
|
| 4497 |
member of the current instantiation is dependent only if the type
|
| 4498 |
referred to is dependent. — *end note*]
|
| 4499 |
|
| 4500 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
| 4501 |
|
| 4502 |
Except as described below, an expression is type-dependent if any
|
| 4503 |
subexpression is type-dependent.
|
| 4504 |
|
| 4505 |
-
`this`
|
| 4506 |
-
|
| 4507 |
-
is type-dependent if the class type of the enclosing member function is
|
| 4508 |
dependent [[temp.dep.type]].
|
| 4509 |
|
| 4510 |
-
An *id-expression* is type-dependent if it is
|
| 4511 |
-
|
| 4512 |
|
| 4513 |
-
-
|
| 4514 |
-
|
| 4515 |
-
-
|
| 4516 |
-
|
| 4517 |
-
|
| 4518 |
-
-
|
| 4519 |
-
|
| 4520 |
-
the initializer is type-dependent,
|
| 4521 |
-
- an *identifier* associated by name lookup with one or more
|
| 4522 |
-
declarations of member functions of the current instantiation declared
|
| 4523 |
-
with a return type that contains a placeholder type,
|
| 4524 |
-
- an *identifier* associated by name lookup with a structured binding
|
| 4525 |
-
declaration [[dcl.struct.bind]] whose *brace-or-equal-initializer* is
|
| 4526 |
type-dependent,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4527 |
- the *identifier* `__func__` [[dcl.fct.def.general]], where any
|
| 4528 |
enclosing function is a template, a member of a class template, or a
|
| 4529 |
generic lambda,
|
| 4530 |
-
- a *template-id* that is dependent,
|
| 4531 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 4532 |
-
-
|
| 4533 |
-
an unknown specialization;
|
| 4534 |
|
| 4535 |
or if it names a dependent member of the current instantiation that is a
|
| 4536 |
static data member of type “array of unknown bound of `T`” for some `T`
|
| 4537 |
[[temp.static]]. Expressions of the following forms are type-dependent
|
| 4538 |
-
only if the type specified by the *type-id*, *simple-type-specifier*
|
| 4539 |
-
*new-type-id* is dependent, even if any
|
|
|
|
| 4540 |
|
| 4541 |
``` bnf
|
| 4542 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
|
|
|
|
|
|
|
|
|
| 4543 |
'::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 4544 |
'::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
| 4545 |
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 4546 |
static_cast '<' type-id '>' '(' expression ')'
|
| 4547 |
const_cast '<' type-id '>' '(' expression ')'
|
|
@@ -4568,21 +4394,19 @@ noexcept '(' expression ')'
|
|
| 4568 |
|
| 4569 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 4570 |
[[support.types]]. — *end note*]
|
| 4571 |
|
| 4572 |
A class member access expression [[expr.ref]] is type-dependent if the
|
|
|
|
| 4573 |
expression refers to a member of the current instantiation and the type
|
| 4574 |
-
of the referenced member is dependent
|
| 4575 |
-
expression refers to a member of an unknown specialization.
|
| 4576 |
|
| 4577 |
[*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
|
| 4578 |
the expression is usually the type of the member `y` of the class of `x`
|
| 4579 |
(or the class pointed to by `xp`). However, if `x` or `xp` refers to a
|
| 4580 |
dependent type that is not the current instantiation, the type of `y` is
|
| 4581 |
-
always dependent.
|
| 4582 |
-
refers to the current instantiation, the type of `y` is the type of the
|
| 4583 |
-
class member access expression. — *end note*]
|
| 4584 |
|
| 4585 |
A *braced-init-list* is type-dependent if any element is type-dependent
|
| 4586 |
or is a pack expansion.
|
| 4587 |
|
| 4588 |
A *fold-expression* is type-dependent.
|
|
@@ -4659,38 +4483,12 @@ the constant expression it specifies is value-dependent.
|
|
| 4659 |
Furthermore, a non-type *template-argument* is dependent if the
|
| 4660 |
corresponding non-type *template-parameter* is of reference or pointer
|
| 4661 |
type and the *template-argument* designates or points to a member of the
|
| 4662 |
current instantiation or a member of a dependent type.
|
| 4663 |
|
| 4664 |
-
A template *template-
|
| 4665 |
-
*template-parameter* or
|
| 4666 |
-
an unknown specialization.
|
| 4667 |
-
|
| 4668 |
-
### Non-dependent names <a id="temp.nondep">[[temp.nondep]]</a>
|
| 4669 |
-
|
| 4670 |
-
Non-dependent names used in a template definition are found using the
|
| 4671 |
-
usual name lookup and bound at the point they are used.
|
| 4672 |
-
|
| 4673 |
-
[*Example 1*:
|
| 4674 |
-
|
| 4675 |
-
``` cpp
|
| 4676 |
-
void g(double);
|
| 4677 |
-
void h();
|
| 4678 |
-
|
| 4679 |
-
template<class T> class Z {
|
| 4680 |
-
public:
|
| 4681 |
-
void f() {
|
| 4682 |
-
g(1); // calls g(double)
|
| 4683 |
-
h++; // ill-formed: cannot increment function; this could be diagnosed
|
| 4684 |
-
// either here or at the point of instantiation
|
| 4685 |
-
}
|
| 4686 |
-
};
|
| 4687 |
-
|
| 4688 |
-
void g(int); // not in scope at the point of the template definition, not considered for the call g(1)
|
| 4689 |
-
```
|
| 4690 |
-
|
| 4691 |
-
— *end example*]
|
| 4692 |
|
| 4693 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 4694 |
|
| 4695 |
#### Point of instantiation <a id="temp.point">[[temp.point]]</a>
|
| 4696 |
|
|
@@ -4762,26 +4560,17 @@ If two different points of instantiation give a template specialization
|
|
| 4762 |
different meanings according to the one-definition rule
|
| 4763 |
[[basic.def.odr]], the program is ill-formed, no diagnostic required.
|
| 4764 |
|
| 4765 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 4766 |
|
| 4767 |
-
|
| 4768 |
-
|
| 4769 |
-
|
| 4770 |
-
|
| 4771 |
-
|
| 4772 |
-
[
|
| 4773 |
-
|
| 4774 |
-
instantiation context are found by this lookup, as described in
|
| 4775 |
-
[[basic.lookup.argdep]]. — *end note*]
|
| 4776 |
-
|
| 4777 |
-
If the call would be ill-formed or would find a better match had the
|
| 4778 |
-
lookup within the associated namespaces considered all the function
|
| 4779 |
-
declarations with external linkage introduced in those namespaces in all
|
| 4780 |
-
translation units, not just considering those declarations found in the
|
| 4781 |
-
template definition and template instantiation contexts, then the
|
| 4782 |
-
program has undefined behavior.
|
| 4783 |
|
| 4784 |
[*Example 1*:
|
| 4785 |
|
| 4786 |
Source file \`"X.h"\`
|
| 4787 |
|
|
@@ -4881,11 +4670,11 @@ Module interface unit of \`M\`
|
|
| 4881 |
module;
|
| 4882 |
#include "X.h"
|
| 4883 |
export module M;
|
| 4884 |
import F;
|
| 4885 |
void g(X x) {
|
| 4886 |
-
f(x); // OK
|
| 4887 |
// operator+ is visible in instantiation context
|
| 4888 |
}
|
| 4889 |
```
|
| 4890 |
|
| 4891 |
— *end example*]
|
|
@@ -4977,43 +4766,14 @@ void k() {
|
|
| 4977 |
}
|
| 4978 |
```
|
| 4979 |
|
| 4980 |
— *end example*]
|
| 4981 |
|
| 4982 |
-
### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
|
| 4983 |
-
|
| 4984 |
-
Friend classes or functions can be declared within a class template.
|
| 4985 |
-
When a template is instantiated, the names of its friends are treated as
|
| 4986 |
-
if the specialization had been explicitly declared at its point of
|
| 4987 |
-
instantiation.
|
| 4988 |
-
|
| 4989 |
-
As with non-template classes, the names of namespace-scope friend
|
| 4990 |
-
functions of a class template specialization are not visible during an
|
| 4991 |
-
ordinary lookup unless explicitly declared at namespace scope
|
| 4992 |
-
[[class.friend]]. Such names may be found under the rules for associated
|
| 4993 |
-
classes [[basic.lookup.argdep]].[^11]
|
| 4994 |
-
|
| 4995 |
-
[*Example 1*:
|
| 4996 |
-
|
| 4997 |
-
``` cpp
|
| 4998 |
-
template<typename T> struct number {
|
| 4999 |
-
number(int);
|
| 5000 |
-
friend number gcd(number x, number y) { return 0; };
|
| 5001 |
-
};
|
| 5002 |
-
|
| 5003 |
-
void g() {
|
| 5004 |
-
number<double> a(3), b(4);
|
| 5005 |
-
a = gcd(a,b); // finds gcd because number<double> is an associated class,
|
| 5006 |
-
// making gcd visible in its namespace (global scope)
|
| 5007 |
-
b = gcd(3,4); // error: gcd is not visible
|
| 5008 |
-
}
|
| 5009 |
-
```
|
| 5010 |
-
|
| 5011 |
-
— *end example*]
|
| 5012 |
-
|
| 5013 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 5014 |
|
|
|
|
|
|
|
| 5015 |
The act of instantiating a function, a variable, a class, a member of a
|
| 5016 |
class template, or a member template is referred to as *template
|
| 5017 |
instantiation*.
|
| 5018 |
|
| 5019 |
A function instantiated from a function template is called an
|
|
@@ -5032,16 +4792,16 @@ instantiated static data member.
|
|
| 5032 |
|
| 5033 |
An explicit specialization may be declared for a function template, a
|
| 5034 |
variable template, a class template, a member of a class template, or a
|
| 5035 |
member template. An explicit specialization declaration is introduced by
|
| 5036 |
`template<>`. In an explicit specialization declaration for a variable
|
| 5037 |
-
template, a class template, a member of a class template or a class
|
| 5038 |
-
member template, the
|
| 5039 |
-
|
| 5040 |
specialization declaration for a function template or a member function
|
| 5041 |
-
template, the
|
| 5042 |
-
|
| 5043 |
|
| 5044 |
[*Example 1*:
|
| 5045 |
|
| 5046 |
``` cpp
|
| 5047 |
template<class T = int> struct A {
|
|
@@ -5077,23 +4837,24 @@ For a given template and a given set of *template-argument*s,
|
|
| 5077 |
program,
|
| 5078 |
- an explicit specialization shall be defined at most once in a program,
|
| 5079 |
as specified in [[basic.def.odr]], and
|
| 5080 |
- both an explicit instantiation and a declaration of an explicit
|
| 5081 |
specialization shall not appear in a program unless the explicit
|
| 5082 |
-
|
| 5083 |
|
| 5084 |
-
An implementation is not required to diagnose a violation of this rule
|
|
|
|
| 5085 |
|
| 5086 |
The usual access checking rules do not apply to names in a declaration
|
| 5087 |
of an explicit instantiation or explicit specialization, with the
|
| 5088 |
exception of names appearing in a function body, default argument,
|
| 5089 |
-
base-clause, member-specification, enumerator-list, or static data
|
| 5090 |
member or variable template initializer.
|
| 5091 |
|
| 5092 |
[*Note 1*: In particular, the template arguments and names used in the
|
| 5093 |
function declarator (including parameter types, return types and
|
| 5094 |
-
exception specifications)
|
| 5095 |
normally not be accessible. — *end note*]
|
| 5096 |
|
| 5097 |
Each class template specialization instantiated from a template has its
|
| 5098 |
own copy of any static members.
|
| 5099 |
|
|
@@ -5179,13 +4940,14 @@ void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
|
| 5179 |
}
|
| 5180 |
```
|
| 5181 |
|
| 5182 |
— *end example*]
|
| 5183 |
|
| 5184 |
-
If
|
| 5185 |
-
|
| 5186 |
-
|
|
|
|
| 5187 |
|
| 5188 |
[*Example 2*:
|
| 5189 |
|
| 5190 |
``` cpp
|
| 5191 |
template<class T> class X;
|
|
@@ -5231,21 +4993,21 @@ template<> void C<int>::g() { } // error: redefinition of C<int>::g
|
|
| 5231 |
|
| 5232 |
— *end example*]
|
| 5233 |
|
| 5234 |
However, for the purpose of determining whether an instantiated
|
| 5235 |
redeclaration is valid according to [[basic.def.odr]] and
|
| 5236 |
-
[[class.mem]],
|
| 5237 |
-
template is considered to be a definition.
|
| 5238 |
|
| 5239 |
[*Example 4*:
|
| 5240 |
|
| 5241 |
``` cpp
|
| 5242 |
template<class T, class U>
|
| 5243 |
struct Outer {
|
| 5244 |
template<class X, class Y> struct Inner;
|
| 5245 |
template<class Y> struct Inner<T, Y>; // #1a
|
| 5246 |
-
template<class Y> struct Inner<T, Y> { }; // #1b; OK
|
| 5247 |
template<class Y> struct Inner<U, Y> { }; // #2
|
| 5248 |
};
|
| 5249 |
|
| 5250 |
Outer<int, int> outer; // error at #2
|
| 5251 |
```
|
|
@@ -5264,19 +5026,18 @@ Friendly<char> fc;
|
|
| 5264 |
Friendly<float> ff; // error: produces second definition of f(U)
|
| 5265 |
```
|
| 5266 |
|
| 5267 |
— *end example*]
|
| 5268 |
|
| 5269 |
-
Unless a member of a class
|
| 5270 |
-
specialization
|
| 5271 |
-
|
| 5272 |
-
|
| 5273 |
-
|
| 5274 |
-
|
| 5275 |
-
static data member
|
| 5276 |
-
|
| 5277 |
-
member to exist.
|
| 5278 |
|
| 5279 |
Unless a function template specialization is a declared specialization,
|
| 5280 |
the function template specialization is implicitly instantiated when the
|
| 5281 |
specialization is referenced in a context that requires a function
|
| 5282 |
definition to exist or if the existence of the definition affects the
|
|
@@ -5291,11 +5052,11 @@ template is implicitly instantiated when the function is called in a
|
|
| 5291 |
context that requires the value of the default argument.
|
| 5292 |
|
| 5293 |
[*Note 4*: An inline function that is the subject of an explicit
|
| 5294 |
instantiation declaration is not a declared specialization; the intent
|
| 5295 |
is that it still be implicitly instantiated when odr-used
|
| 5296 |
-
[[
|
| 5297 |
that no out-of-line copy of it be generated in the translation
|
| 5298 |
unit. — *end note*]
|
| 5299 |
|
| 5300 |
[*Example 5*:
|
| 5301 |
|
|
@@ -5379,11 +5140,11 @@ If a function template or a member function template specialization is
|
|
| 5379 |
used in a way that involves overload resolution, a declaration of the
|
| 5380 |
specialization is implicitly instantiated [[temp.over]].
|
| 5381 |
|
| 5382 |
An implementation shall not implicitly instantiate a function template,
|
| 5383 |
a variable template, a member template, a non-virtual member function, a
|
| 5384 |
-
member class
|
| 5385 |
substatement of a constexpr if statement [[stmt.if]], unless such
|
| 5386 |
instantiation is required.
|
| 5387 |
|
| 5388 |
[*Note 5*: The instantiation of a generic lambda does not require
|
| 5389 |
instantiation of substatements of a constexpr if statement within its
|
|
@@ -5391,67 +5152,36 @@ instantiation of substatements of a constexpr if statement within its
|
|
| 5391 |
instantiated. — *end note*]
|
| 5392 |
|
| 5393 |
It is unspecified whether or not an implementation implicitly
|
| 5394 |
instantiates a virtual member function of a class template if the
|
| 5395 |
virtual member function would not otherwise be instantiated. The use of
|
| 5396 |
-
a template specialization in a default argument
|
| 5397 |
-
|
| 5398 |
-
|
| 5399 |
-
|
| 5400 |
function call causes specializations in the default argument to be
|
| 5401 |
-
implicitly instantiated.
|
|
|
|
|
|
|
|
|
|
| 5402 |
|
| 5403 |
-
|
| 5404 |
-
specializations are placed in the namespace where the template is
|
| 5405 |
-
defined. Implicitly instantiated specializations for members of a class
|
| 5406 |
-
template are placed in the namespace where the enclosing class template
|
| 5407 |
-
is defined. Implicitly instantiated member templates are placed in the
|
| 5408 |
-
namespace where the enclosing class or class template is defined.
|
| 5409 |
-
|
| 5410 |
-
[*Example 8*:
|
| 5411 |
-
|
| 5412 |
-
``` cpp
|
| 5413 |
-
namespace N {
|
| 5414 |
-
template<class T> class List {
|
| 5415 |
-
public:
|
| 5416 |
-
T* get();
|
| 5417 |
-
};
|
| 5418 |
-
}
|
| 5419 |
-
|
| 5420 |
-
template<class K, class V> class Map {
|
| 5421 |
-
public:
|
| 5422 |
-
N::List<V> lt;
|
| 5423 |
-
V get(K);
|
| 5424 |
-
};
|
| 5425 |
-
|
| 5426 |
-
void g(Map<const char*,int>& m) {
|
| 5427 |
-
int i = m.get("Nicholas");
|
| 5428 |
-
}
|
| 5429 |
-
```
|
| 5430 |
-
|
| 5431 |
-
A call of `lt.get()` from `Map<const char*,int>::get()` would place
|
| 5432 |
-
`List<int>::get()` in the namespace `N` rather than in the global
|
| 5433 |
-
namespace.
|
| 5434 |
-
|
| 5435 |
-
— *end example*]
|
| 5436 |
-
|
| 5437 |
-
If a function template `f` is called in a way that requires a default
|
| 5438 |
argument to be used, the dependent names are looked up, the semantics
|
| 5439 |
constraints are checked, and the instantiation of any template used in
|
| 5440 |
the default argument is done as if the default argument had been an
|
| 5441 |
initializer used in a function template specialization with the same
|
| 5442 |
scope, the same template parameters and the same access as that of the
|
| 5443 |
function template `f` used at that point, except that the scope in which
|
| 5444 |
-
a closure type is declared [[expr.prim.lambda.closure]]
|
| 5445 |
-
its associated namespaces
|
| 5446 |
definition for the default argument. This analysis is called *default
|
| 5447 |
argument instantiation*. The instantiated default argument is then used
|
| 5448 |
as the argument of `f`.
|
| 5449 |
|
| 5450 |
Each default argument is instantiated independently.
|
| 5451 |
|
| 5452 |
-
[*Example
|
| 5453 |
|
| 5454 |
``` cpp
|
| 5455 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 5456 |
|
| 5457 |
class A { };
|
|
@@ -5482,11 +5212,11 @@ template specialization. — *end note*]
|
|
| 5482 |
There is an *implementation-defined* quantity that specifies the limit
|
| 5483 |
on the total depth of recursive instantiations [[implimits]], which
|
| 5484 |
could involve more than one template. The result of an infinite
|
| 5485 |
recursion in instantiation is undefined.
|
| 5486 |
|
| 5487 |
-
[*Example
|
| 5488 |
|
| 5489 |
``` cpp
|
| 5490 |
template<class T> class X {
|
| 5491 |
X<T>* p; // OK
|
| 5492 |
X<T*> a; // implicit generation of X<T> requires
|
|
@@ -5508,11 +5238,11 @@ declarations.
|
|
| 5508 |
|
| 5509 |
[*Note 7*: The satisfaction of constraints is determined during
|
| 5510 |
template argument deduction [[temp.deduct]] and overload resolution
|
| 5511 |
[[over.match]]. — *end note*]
|
| 5512 |
|
| 5513 |
-
[*Example
|
| 5514 |
|
| 5515 |
``` cpp
|
| 5516 |
template<typename T> concept C = sizeof(T) > 2;
|
| 5517 |
template<typename T> concept D = C<T> && sizeof(T) > 4;
|
| 5518 |
|
|
@@ -5530,11 +5260,11 @@ specialization. Their constraints are not satisfied, and they suppress
|
|
| 5530 |
the implicit declaration of a default constructor for `S<char>`
|
| 5531 |
[[class.default.ctor]], so there is no viable constructor for `s1`.
|
| 5532 |
|
| 5533 |
— *end example*]
|
| 5534 |
|
| 5535 |
-
[*Example
|
| 5536 |
|
| 5537 |
``` cpp
|
| 5538 |
template<typename T> struct S1 {
|
| 5539 |
template<typename U>
|
| 5540 |
requires false
|
|
@@ -5584,33 +5314,12 @@ appertain to an explicit instantiation.
|
|
| 5584 |
If the explicit instantiation is for a class or member class, the
|
| 5585 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 5586 |
*simple-template-id*; otherwise, the *declaration* shall be a
|
| 5587 |
*simple-declaration* whose *init-declarator-list* comprises a single
|
| 5588 |
*init-declarator* that does not have an *initializer*. If the explicit
|
| 5589 |
-
instantiation is for a
|
| 5590 |
-
in the *declarator* shall be
|
| 5591 |
-
template arguments can be deduced, a *template-name* or
|
| 5592 |
-
*operator-function-id*.
|
| 5593 |
-
|
| 5594 |
-
[*Note 1*: The declaration may declare a *qualified-id*, in which case
|
| 5595 |
-
the *unqualified-id* of the *qualified-id* must be a
|
| 5596 |
-
*template-id*. — *end note*]
|
| 5597 |
-
|
| 5598 |
-
If the explicit instantiation is for a member function, a member class
|
| 5599 |
-
or a static data member of a class template specialization, the name of
|
| 5600 |
-
the class template specialization in the *qualified-id* for the member
|
| 5601 |
-
name shall be a *simple-template-id*. If the explicit instantiation is
|
| 5602 |
-
for a variable template specialization, the *unqualified-id* in the
|
| 5603 |
-
*declarator* shall be a *simple-template-id*. An explicit instantiation
|
| 5604 |
-
shall appear in an enclosing namespace of its template. If the name
|
| 5605 |
-
declared in the explicit instantiation is an unqualified name, the
|
| 5606 |
-
explicit instantiation shall appear in the namespace where its template
|
| 5607 |
-
is declared or, if that namespace is inline [[namespace.def]], any
|
| 5608 |
-
namespace from its enclosing namespace set.
|
| 5609 |
-
|
| 5610 |
-
[*Note 2*: Regarding qualified names in declarators, see
|
| 5611 |
-
[[dcl.meaning]]. — *end note*]
|
| 5612 |
|
| 5613 |
[*Example 1*:
|
| 5614 |
|
| 5615 |
``` cpp
|
| 5616 |
template<class T> class Array { void mf(); };
|
|
@@ -5626,26 +5335,28 @@ namespace N {
|
|
| 5626 |
template void N::f<int>(int&);
|
| 5627 |
```
|
| 5628 |
|
| 5629 |
— *end example*]
|
| 5630 |
|
| 5631 |
-
|
| 5632 |
-
|
| 5633 |
-
template
|
| 5634 |
-
|
| 5635 |
-
|
| 5636 |
-
class template
|
| 5637 |
-
|
| 5638 |
-
|
| 5639 |
-
|
| 5640 |
-
|
|
|
|
|
|
|
| 5641 |
|
| 5642 |
The *declaration* in an *explicit-instantiation* and the *declaration*
|
| 5643 |
produced by the corresponding substitution into the templated function,
|
| 5644 |
variable, or class are two declarations of the same entity.
|
| 5645 |
|
| 5646 |
-
[*Note
|
| 5647 |
|
| 5648 |
These declarations are required to have matching types as specified in
|
| 5649 |
[[basic.link]], except as specified in [[except.spec]].
|
| 5650 |
|
| 5651 |
[*Example 2*:
|
|
@@ -5679,41 +5390,17 @@ that template, the explicit instantiation has no effect. Otherwise, for
|
|
| 5679 |
an explicit instantiation definition, the definition of a function
|
| 5680 |
template, a variable template, a member function template, or a member
|
| 5681 |
function or static data member of a class template shall be present in
|
| 5682 |
every translation unit in which it is explicitly instantiated.
|
| 5683 |
|
| 5684 |
-
An explicit instantiation of a class, function template, or variable
|
| 5685 |
-
template specialization is placed in the namespace in which the template
|
| 5686 |
-
is defined. An explicit instantiation for a member of a class template
|
| 5687 |
-
is placed in the namespace where the enclosing class template is
|
| 5688 |
-
defined. An explicit instantiation for a member template is placed in
|
| 5689 |
-
the namespace where the enclosing class or class template is defined.
|
| 5690 |
-
|
| 5691 |
-
[*Example 3*:
|
| 5692 |
-
|
| 5693 |
-
``` cpp
|
| 5694 |
-
namespace N {
|
| 5695 |
-
template<class T> class Y { void mf() { } };
|
| 5696 |
-
}
|
| 5697 |
-
|
| 5698 |
-
template class Y<int>; // error: class template Y not visible in the global namespace
|
| 5699 |
-
|
| 5700 |
-
using N::Y;
|
| 5701 |
-
template class Y<int>; // error: explicit instantiation outside of the namespace of the template
|
| 5702 |
-
|
| 5703 |
-
template class N::Y<char*>; // OK: explicit instantiation in namespace N
|
| 5704 |
-
template void N::Y<double>::mf(); // OK: explicit instantiation in namespace N
|
| 5705 |
-
```
|
| 5706 |
-
|
| 5707 |
-
— *end example*]
|
| 5708 |
-
|
| 5709 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 5710 |
instantiation of a function template specialization or of a member
|
| 5711 |
-
function template specialization provided it can be deduced
|
| 5712 |
-
|
|
|
|
| 5713 |
|
| 5714 |
-
[*Example
|
| 5715 |
|
| 5716 |
``` cpp
|
| 5717 |
template<class T> class Array { ... };
|
| 5718 |
template<class T> void sort(Array<T>& v) { ... }
|
| 5719 |
|
|
@@ -5721,50 +5408,49 @@ template<class T> void sort(Array<T>& v) { ... }
|
|
| 5721 |
template void sort<>(Array<int>&);
|
| 5722 |
```
|
| 5723 |
|
| 5724 |
— *end example*]
|
| 5725 |
|
| 5726 |
-
[*Note
|
| 5727 |
required to satisfy that template’s associated constraints
|
| 5728 |
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 5729 |
forming the template name of an explicit instantiation in which all
|
| 5730 |
template arguments are specified [[temp.names]], or, for explicit
|
| 5731 |
instantiations of function templates, during template argument deduction
|
| 5732 |
[[temp.deduct.decl]] when one or more trailing template arguments are
|
| 5733 |
left unspecified. — *end note*]
|
| 5734 |
|
| 5735 |
An explicit instantiation that names a class template specialization is
|
| 5736 |
also an explicit instantiation of the same kind (declaration or
|
| 5737 |
-
definition) of each of its
|
| 5738 |
-
base classes and members that are templates) that has not been
|
| 5739 |
previously explicitly specialized in the translation unit containing the
|
| 5740 |
explicit instantiation, provided that the associated constraints, if
|
| 5741 |
any, of that member are satisfied by the template arguments of the
|
| 5742 |
-
explicit instantiation
|
| 5743 |
except as described below.
|
| 5744 |
|
| 5745 |
-
[*Note
|
| 5746 |
of certain implementation-dependent data about the class. — *end note*]
|
| 5747 |
|
| 5748 |
An explicit instantiation definition that names a class template
|
| 5749 |
specialization explicitly instantiates the class template specialization
|
| 5750 |
and is an explicit instantiation definition of only those members that
|
| 5751 |
have been defined at the point of instantiation.
|
| 5752 |
|
| 5753 |
An explicit instantiation of a prospective destructor [[class.dtor]]
|
| 5754 |
-
shall
|
| 5755 |
|
| 5756 |
If an entity is the subject of both an explicit instantiation
|
| 5757 |
declaration and an explicit instantiation definition in the same
|
| 5758 |
translation unit, the definition shall follow the declaration. An entity
|
| 5759 |
that is the subject of an explicit instantiation declaration and that is
|
| 5760 |
also used in a way that would otherwise cause an implicit instantiation
|
| 5761 |
[[temp.inst]] in the translation unit shall be the subject of an
|
| 5762 |
explicit instantiation definition somewhere in the program; otherwise
|
| 5763 |
the program is ill-formed, no diagnostic required.
|
| 5764 |
|
| 5765 |
-
[*Note
|
| 5766 |
explicit instantiation declaration of such an entity has no other
|
| 5767 |
normative effect. This is needed to ensure that if the address of an
|
| 5768 |
inline function is taken in a translation unit in which the
|
| 5769 |
implementation chose to suppress the out-of-line body, another
|
| 5770 |
translation unit will supply the body. — *end note*]
|
|
@@ -5773,11 +5459,11 @@ An explicit instantiation declaration shall not name a specialization of
|
|
| 5773 |
a template with internal linkage.
|
| 5774 |
|
| 5775 |
An explicit instantiation does not constitute a use of a default
|
| 5776 |
argument, so default argument instantiation is not done.
|
| 5777 |
|
| 5778 |
-
[*Example
|
| 5779 |
|
| 5780 |
``` cpp
|
| 5781 |
char* p = 0;
|
| 5782 |
template<class T> T g(T x = &p) { return x; }
|
| 5783 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
@@ -5826,65 +5512,67 @@ specializations instantiated from the class template. Similarly,
|
|
| 5826 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 5827 |
generated from the template.
|
| 5828 |
|
| 5829 |
— *end example*]
|
| 5830 |
|
| 5831 |
-
|
| 5832 |
-
|
|
|
|
| 5833 |
|
| 5834 |
An explicit specialization may be declared in any scope in which the
|
| 5835 |
-
corresponding primary template may be defined
|
| 5836 |
-
[[class.mem]], [[temp.mem]]
|
| 5837 |
|
| 5838 |
-
|
| 5839 |
-
|
| 5840 |
-
|
|
|
|
| 5841 |
|
| 5842 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 5843 |
required. — *end note*]
|
| 5844 |
|
| 5845 |
-
The definition of a class or class template shall
|
| 5846 |
declaration of an explicit specialization for a member template of the
|
| 5847 |
class or class template.
|
| 5848 |
|
| 5849 |
[*Example 2*:
|
| 5850 |
|
| 5851 |
``` cpp
|
| 5852 |
template<> class X<int> { ... }; // error: X not a template
|
| 5853 |
|
| 5854 |
template<class T> class X;
|
| 5855 |
|
| 5856 |
-
template<> class X<char*> { ... }; // OK
|
| 5857 |
```
|
| 5858 |
|
| 5859 |
— *end example*]
|
| 5860 |
|
| 5861 |
A member function, a member function template, a member class, a member
|
| 5862 |
enumeration, a member class template, a static data member, or a static
|
| 5863 |
data member template of a class template may be explicitly specialized
|
| 5864 |
for a class specialization that is implicitly instantiated; in this
|
| 5865 |
-
case, the definition of the class template shall
|
| 5866 |
-
specialization for the member of the class template. If such an
|
| 5867 |
-
specialization for the member of a class template names an
|
| 5868 |
implicitly-declared special member function [[special]], the program is
|
| 5869 |
ill-formed.
|
| 5870 |
|
| 5871 |
A member of an explicitly specialized class is not implicitly
|
| 5872 |
instantiated from the member declaration of the class template; instead,
|
| 5873 |
the member of the class template specialization shall itself be
|
| 5874 |
-
explicitly defined if its definition is required.
|
| 5875 |
-
|
| 5876 |
-
|
| 5877 |
-
|
| 5878 |
-
|
| 5879 |
-
|
| 5880 |
-
|
| 5881 |
-
|
| 5882 |
-
|
| 5883 |
-
|
| 5884 |
-
|
| 5885 |
-
|
| 5886 |
|
| 5887 |
[*Example 3*:
|
| 5888 |
|
| 5889 |
``` cpp
|
| 5890 |
template<class T> struct A {
|
|
@@ -5930,20 +5618,20 @@ template<class U> void A<short>::C<U>::f() { ... } // error: template<> requi
|
|
| 5930 |
```
|
| 5931 |
|
| 5932 |
— *end example*]
|
| 5933 |
|
| 5934 |
If a template, a member template or a member of a class template is
|
| 5935 |
-
explicitly specialized
|
| 5936 |
-
|
| 5937 |
-
instantiation to take place, in every translation unit in which
|
| 5938 |
-
use occurs; no diagnostic is required. If the program does not
|
| 5939 |
-
definition for an explicit specialization and either the
|
| 5940 |
-
is used in a way that would cause an implicit
|
| 5941 |
-
place or the member is a virtual member function,
|
| 5942 |
-
ill-formed, no diagnostic required. An implicit
|
| 5943 |
-
generated for an explicit specialization that is
|
| 5944 |
-
defined.
|
| 5945 |
|
| 5946 |
[*Example 4*:
|
| 5947 |
|
| 5948 |
``` cpp
|
| 5949 |
class String { };
|
|
@@ -5953,11 +5641,11 @@ template<class T> void sort(Array<T>& v) { ... }
|
|
| 5953 |
void f(Array<String>& v) {
|
| 5954 |
sort(v); // use primary template sort(Array<T>&), T is String
|
| 5955 |
}
|
| 5956 |
|
| 5957 |
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
| 5958 |
-
template<> void sort<>(Array<char*>& v); // OK
|
| 5959 |
template<class T> struct A {
|
| 5960 |
enum E : T;
|
| 5961 |
enum class S : T;
|
| 5962 |
};
|
| 5963 |
template<> enum A<int>::E : int { eint }; // OK
|
|
@@ -5988,52 +5676,32 @@ can affect whether a program is well-formed according to the relative
|
|
| 5988 |
positioning of the explicit specialization declarations and their points
|
| 5989 |
of instantiation in the translation unit as specified above and below.
|
| 5990 |
When writing a specialization, be careful about its location; or to make
|
| 5991 |
it compile will be such a trial as to kindle its self-immolation.
|
| 5992 |
|
| 5993 |
-
A template explicit specialization is in the scope of the namespace in
|
| 5994 |
-
which the template was defined.
|
| 5995 |
-
|
| 5996 |
-
[*Example 5*:
|
| 5997 |
-
|
| 5998 |
-
``` cpp
|
| 5999 |
-
namespace N {
|
| 6000 |
-
template<class T> class X { ... };
|
| 6001 |
-
template<class T> class Y { ... };
|
| 6002 |
-
|
| 6003 |
-
template<> class X<int> { ... }; // OK: specialization in same namespace
|
| 6004 |
-
template<> class Y<double>; // forward-declare intent to specialize for double
|
| 6005 |
-
}
|
| 6006 |
-
|
| 6007 |
-
template<> class N::Y<double> { ... }; // OK: specialization in enclosing namespace
|
| 6008 |
-
template<> class N::Y<short> { ... }; // OK: specialization in enclosing namespace
|
| 6009 |
-
```
|
| 6010 |
-
|
| 6011 |
-
— *end example*]
|
| 6012 |
-
|
| 6013 |
A *simple-template-id* that names a class template explicit
|
| 6014 |
specialization that has been declared but not defined can be used
|
| 6015 |
exactly like the names of other incompletely-defined classes
|
| 6016 |
[[basic.types]].
|
| 6017 |
|
| 6018 |
-
[*Example
|
| 6019 |
|
| 6020 |
``` cpp
|
| 6021 |
template<class T> class X; // X is a class template
|
| 6022 |
template<> class X<int>;
|
| 6023 |
|
| 6024 |
-
X<int>* p; // OK
|
| 6025 |
X<int> x; // error: object of incomplete class X<int>
|
| 6026 |
```
|
| 6027 |
|
| 6028 |
— *end example*]
|
| 6029 |
|
| 6030 |
A trailing *template-argument* can be left unspecified in the
|
| 6031 |
*template-id* naming an explicit function template specialization
|
| 6032 |
-
provided it can be deduced
|
| 6033 |
|
| 6034 |
-
[*Example
|
| 6035 |
|
| 6036 |
``` cpp
|
| 6037 |
template<class T> class Array { ... };
|
| 6038 |
template<class T> void sort(Array<T>& v);
|
| 6039 |
|
|
@@ -6056,22 +5724,29 @@ arguments are left unspecified. — *end note*]
|
|
| 6056 |
A function with the same name as a template and a type that exactly
|
| 6057 |
matches that of a template specialization is not an explicit
|
| 6058 |
specialization [[temp.fct]].
|
| 6059 |
|
| 6060 |
Whether an explicit specialization of a function or variable template is
|
| 6061 |
-
inline, constexpr, or
|
| 6062 |
-
|
| 6063 |
-
template
|
|
|
|
| 6064 |
|
| 6065 |
-
[*Example
|
| 6066 |
|
| 6067 |
``` cpp
|
| 6068 |
template<class T> void f(T) { ... }
|
| 6069 |
template<class T> inline T g(T) { ... }
|
| 6070 |
|
| 6071 |
-
template<> inline void f<>(int) { ... } // OK
|
| 6072 |
-
template<> int g<>(int) { ... } // OK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6073 |
```
|
| 6074 |
|
| 6075 |
— *end example*]
|
| 6076 |
|
| 6077 |
An explicit specialization of a static data member of a template or an
|
|
@@ -6079,28 +5754,29 @@ explicit specialization of a static data member template is a definition
|
|
| 6079 |
if the declaration includes an initializer; otherwise, it is a
|
| 6080 |
declaration.
|
| 6081 |
|
| 6082 |
[*Note 3*:
|
| 6083 |
|
| 6084 |
-
The definition of a static data member of a template
|
| 6085 |
-
default-initialization
|
|
|
|
| 6086 |
|
| 6087 |
``` cpp
|
| 6088 |
template<> X Q<int>::x; // declaration
|
| 6089 |
template<> X Q<int>::x (); // error: declares a function
|
| 6090 |
-
template<> X Q<int>::x
|
| 6091 |
```
|
| 6092 |
|
| 6093 |
— *end note*]
|
| 6094 |
|
| 6095 |
A member or a member template of a class template may be explicitly
|
| 6096 |
specialized for a given implicit instantiation of the class template,
|
| 6097 |
even if the member or member template is defined in the class template
|
| 6098 |
definition. An explicit specialization of a member or member template is
|
| 6099 |
specified using the syntax for explicit specialization.
|
| 6100 |
|
| 6101 |
-
[*Example
|
| 6102 |
|
| 6103 |
``` cpp
|
| 6104 |
template<class T> struct A {
|
| 6105 |
void f(T);
|
| 6106 |
template<class X1> void g1(T, X1);
|
|
@@ -6132,11 +5808,11 @@ template<> void A<int>::h(int) { }
|
|
| 6132 |
A member or a member template may be nested within many enclosing class
|
| 6133 |
templates. In an explicit specialization for such a member, the member
|
| 6134 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 6135 |
template that is explicitly specialized.
|
| 6136 |
|
| 6137 |
-
[*Example
|
| 6138 |
|
| 6139 |
``` cpp
|
| 6140 |
template<class T1> class A {
|
| 6141 |
template<class T2> class B {
|
| 6142 |
void mf();
|
|
@@ -6158,11 +5834,11 @@ declaration, the keyword `template` followed by a
|
|
| 6158 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 6159 |
preceding the explicit specialization declaration of the member. The
|
| 6160 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 6161 |
shall be the same as those specified in the primary template definition.
|
| 6162 |
|
| 6163 |
-
[*Example
|
| 6164 |
|
| 6165 |
``` cpp
|
| 6166 |
template <class T1> class A {
|
| 6167 |
template<class T2> class B {
|
| 6168 |
template<class T3> void mf1(T3);
|
|
@@ -6195,16 +5871,18 @@ definition for one of the following explicit specializations:
|
|
| 6195 |
- the explicit specialization of a function template;
|
| 6196 |
- the explicit specialization of a member function template;
|
| 6197 |
- the explicit specialization of a member function of a class template
|
| 6198 |
where the class template specialization to which the member function
|
| 6199 |
specialization belongs is implicitly instantiated. \[*Note 4*: Default
|
| 6200 |
-
function arguments
|
| 6201 |
of a member function of a class template specialization that is
|
| 6202 |
explicitly specialized. — *end note*]
|
| 6203 |
|
| 6204 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
| 6205 |
|
|
|
|
|
|
|
| 6206 |
A function instantiated from a function template is called a function
|
| 6207 |
template specialization; so is an explicit specialization of a function
|
| 6208 |
template. Template arguments can be explicitly specified when naming the
|
| 6209 |
function template specialization, deduced from the context (e.g.,
|
| 6210 |
deduced from the function arguments in a call to the function template
|
|
@@ -6262,12 +5940,11 @@ void g(double d) {
|
|
| 6262 |
```
|
| 6263 |
|
| 6264 |
— *end example*]
|
| 6265 |
|
| 6266 |
Template arguments shall not be specified when referring to a
|
| 6267 |
-
specialization of a constructor template
|
| 6268 |
-
[[class.qual]]).
|
| 6269 |
|
| 6270 |
A template argument list may be specified when referring to a
|
| 6271 |
specialization of a function template
|
| 6272 |
|
| 6273 |
- when a function is called,
|
|
@@ -6277,20 +5954,19 @@ specialization of a function template
|
|
| 6277 |
- in an explicit instantiation, or
|
| 6278 |
- in a friend declaration.
|
| 6279 |
|
| 6280 |
Trailing template arguments that can be deduced [[temp.deduct]] or
|
| 6281 |
obtained from default *template-argument*s may be omitted from the list
|
| 6282 |
-
of explicit *template-argument*s.
|
| 6283 |
-
|
| 6284 |
-
|
| 6285 |
-
deduced
|
| 6286 |
-
|
| 6287 |
-
|
| 6288 |
-
|
| 6289 |
-
template
|
| 6290 |
-
|
| 6291 |
-
template specialization.
|
| 6292 |
|
| 6293 |
[*Example 2*:
|
| 6294 |
|
| 6295 |
``` cpp
|
| 6296 |
template<class X, class Y> X f(Y);
|
|
@@ -6306,11 +5982,11 @@ void h() {
|
|
| 6306 |
}
|
| 6307 |
```
|
| 6308 |
|
| 6309 |
— *end example*]
|
| 6310 |
|
| 6311 |
-
[*Note
|
| 6312 |
|
| 6313 |
An empty template argument list can be used to indicate that a given use
|
| 6314 |
refers to a specialization of a function template even when a
|
| 6315 |
non-template function [[dcl.fct]] is visible that would otherwise be
|
| 6316 |
used. For example:
|
|
@@ -6349,11 +6025,11 @@ void g() {
|
|
| 6349 |
Implicit conversions [[conv]] will be performed on a function argument
|
| 6350 |
to convert it to the type of the corresponding function parameter if the
|
| 6351 |
parameter type contains no *template-parameter*s that participate in
|
| 6352 |
template argument deduction.
|
| 6353 |
|
| 6354 |
-
[*Note
|
| 6355 |
|
| 6356 |
Template parameters do not participate in template argument deduction if
|
| 6357 |
they are explicitly specified. For example,
|
| 6358 |
|
| 6359 |
``` cpp
|
|
@@ -6368,11 +6044,11 @@ void g() {
|
|
| 6368 |
}
|
| 6369 |
```
|
| 6370 |
|
| 6371 |
— *end note*]
|
| 6372 |
|
| 6373 |
-
[*Note
|
| 6374 |
function template name, and because constructor templates [[class.ctor]]
|
| 6375 |
are named without using a function name [[class.qual]], there is no way
|
| 6376 |
to provide an explicit template argument list for these function
|
| 6377 |
templates. — *end note*]
|
| 6378 |
|
|
@@ -6392,10 +6068,12 @@ void g() {
|
|
| 6392 |
|
| 6393 |
— *end example*]
|
| 6394 |
|
| 6395 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 6396 |
|
|
|
|
|
|
|
| 6397 |
When a function template specialization is referenced, all of the
|
| 6398 |
template arguments shall have values. The values can be explicitly
|
| 6399 |
specified or, in some cases, be deduced from the use or obtained from
|
| 6400 |
default *template-argument*s.
|
| 6401 |
|
|
@@ -6490,45 +6168,90 @@ void g() {
|
|
| 6490 |
|
| 6491 |
— *end example*]
|
| 6492 |
|
| 6493 |
When all template arguments have been deduced or obtained from default
|
| 6494 |
template arguments, all uses of template parameters in the template
|
| 6495 |
-
parameter list of the template
|
| 6496 |
-
|
| 6497 |
-
|
| 6498 |
-
|
| 6499 |
-
[[temp.constr.
|
| 6500 |
-
|
| 6501 |
-
deduction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6502 |
|
| 6503 |
At certain points in the template argument deduction process it is
|
| 6504 |
necessary to take a function type that makes use of template parameters
|
| 6505 |
and replace those template parameters with the corresponding template
|
| 6506 |
arguments. This is done at the beginning of template argument deduction
|
| 6507 |
when any explicitly specified template arguments are substituted into
|
| 6508 |
the function type, and again at the end of template argument deduction
|
| 6509 |
when any template arguments that were deduced or obtained from default
|
| 6510 |
arguments are substituted.
|
| 6511 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6512 |
The substitution occurs in all types and expressions that are used in
|
| 6513 |
-
the
|
| 6514 |
-
|
| 6515 |
-
|
| 6516 |
-
|
| 6517 |
-
|
| 6518 |
-
|
| 6519 |
-
|
| 6520 |
-
|
| 6521 |
-
|
| 6522 |
-
|
| 6523 |
|
| 6524 |
-
[*Note
|
| 6525 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 6526 |
a program is ill-formed if the substitution results in an invalid type
|
| 6527 |
or expression. — *end note*]
|
| 6528 |
|
| 6529 |
-
[*Example
|
| 6530 |
|
| 6531 |
``` cpp
|
| 6532 |
template <class T> struct A { using X = typename T::X; };
|
| 6533 |
template <class T> typename T::X f(typename A<T>::X);
|
| 6534 |
template <class T> void f(...) { }
|
|
@@ -6547,38 +6270,37 @@ void x() {
|
|
| 6547 |
|
| 6548 |
— *end example*]
|
| 6549 |
|
| 6550 |
If a substitution results in an invalid type or expression, type
|
| 6551 |
deduction fails. An invalid type or expression is one that would be
|
| 6552 |
-
ill-formed, with a diagnostic required, if written
|
| 6553 |
-
arguments.
|
| 6554 |
|
| 6555 |
-
[*Note
|
| 6556 |
ill-formed. Access checking is done as part of the substitution
|
| 6557 |
process. — *end note*]
|
| 6558 |
|
| 6559 |
-
|
| 6560 |
-
|
| 6561 |
-
*explicit-specifier* can result in a deduction failure.
|
| 6562 |
|
| 6563 |
-
[*Note
|
| 6564 |
effects such as the instantiation of class template specializations
|
| 6565 |
and/or function template specializations, the generation of
|
| 6566 |
implicitly-defined functions, etc. Such effects are not in the
|
| 6567 |
“immediate context” and can result in the program being
|
| 6568 |
ill-formed. — *end note*]
|
| 6569 |
|
| 6570 |
A *lambda-expression* appearing in a function type or a template
|
| 6571 |
parameter is not considered part of the immediate context for the
|
| 6572 |
purposes of template argument deduction.
|
| 6573 |
|
| 6574 |
-
[*Note
|
| 6575 |
|
| 6576 |
The intent is to avoid requiring implementations to deal with
|
| 6577 |
substitution failure involving arbitrary statements.
|
| 6578 |
|
| 6579 |
-
[*Example
|
| 6580 |
|
| 6581 |
``` cpp
|
| 6582 |
template <class T>
|
| 6583 |
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 6584 |
void f(...);
|
|
@@ -6607,11 +6329,11 @@ j(0); // deduction fails on #1, calls #2
|
|
| 6607 |
|
| 6608 |
— *end example*]
|
| 6609 |
|
| 6610 |
— *end note*]
|
| 6611 |
|
| 6612 |
-
[*Example
|
| 6613 |
|
| 6614 |
``` cpp
|
| 6615 |
struct X { };
|
| 6616 |
struct Y {
|
| 6617 |
Y(X) {}
|
|
@@ -6624,30 +6346,30 @@ X x1, x2;
|
|
| 6624 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 6625 |
```
|
| 6626 |
|
| 6627 |
— *end example*]
|
| 6628 |
|
| 6629 |
-
[*Note
|
| 6630 |
|
| 6631 |
-
Type deduction
|
| 6632 |
|
| 6633 |
- Attempting to instantiate a pack expansion containing multiple packs
|
| 6634 |
of differing lengths.
|
| 6635 |
- Attempting to create an array with an element type that is `void`, a
|
| 6636 |
function type, or a reference type, or attempting to create an array
|
| 6637 |
with a size that is zero or negative.
|
| 6638 |
-
\[*Example
|
| 6639 |
``` cpp
|
| 6640 |
template <class T> int f(T[5]);
|
| 6641 |
int I = f<int>(0);
|
| 6642 |
int j = f<void>(0); // invalid array
|
| 6643 |
```
|
| 6644 |
|
| 6645 |
— *end example*]
|
| 6646 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 6647 |
qualified name.
|
| 6648 |
-
\[*Example
|
| 6649 |
``` cpp
|
| 6650 |
template <class T> int f(typename T::B*);
|
| 6651 |
int i = f<int>(0);
|
| 6652 |
```
|
| 6653 |
|
|
@@ -6658,17 +6380,17 @@ Type deduction may fail for the following reasons:
|
|
| 6658 |
- the specified member is not a type where a type is required, or
|
| 6659 |
- the specified member is not a template where a template is required,
|
| 6660 |
or
|
| 6661 |
- the specified member is not a non-type where a non-type is required.
|
| 6662 |
|
| 6663 |
-
\[*Example
|
| 6664 |
``` cpp
|
| 6665 |
template <int I> struct X { };
|
| 6666 |
template <template <class T> class> struct Z { };
|
| 6667 |
template <class T> void f(typename T::Y*) {}
|
| 6668 |
template <class T> void g(X<T::N>*) {}
|
| 6669 |
-
template <class T> void h(Z<T::
|
| 6670 |
struct A {};
|
| 6671 |
struct B { int Y; };
|
| 6672 |
struct C {
|
| 6673 |
typedef int N;
|
| 6674 |
};
|
|
@@ -6688,44 +6410,46 @@ Type deduction may fail for the following reasons:
|
|
| 6688 |
— *end example*]
|
| 6689 |
- Attempting to create a pointer to reference type.
|
| 6690 |
- Attempting to create a reference to `void`.
|
| 6691 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 6692 |
class type.
|
| 6693 |
-
\[*Example
|
| 6694 |
``` cpp
|
| 6695 |
template <class T> int f(int T::*);
|
| 6696 |
int i = f<int>(0);
|
| 6697 |
```
|
| 6698 |
|
| 6699 |
— *end example*]
|
| 6700 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 6701 |
-
\[*Example
|
| 6702 |
``` cpp
|
| 6703 |
template <class T, T> struct S {};
|
| 6704 |
-
template <class T> int f(S<T, T
|
| 6705 |
-
|
| 6706 |
-
|
|
|
|
|
|
|
| 6707 |
```
|
| 6708 |
|
| 6709 |
— *end example*]
|
| 6710 |
- Attempting to perform an invalid conversion in either a template
|
| 6711 |
argument expression, or an expression used in the function
|
| 6712 |
declaration.
|
| 6713 |
-
\[*Example
|
| 6714 |
``` cpp
|
| 6715 |
template <class T, T*> int f(int);
|
| 6716 |
-
int i2 = f<int,1>(0); // can't
|
| 6717 |
```
|
| 6718 |
|
| 6719 |
— *end example*]
|
| 6720 |
- Attempting to create a function type in which a parameter has a type
|
| 6721 |
of `void`, or in which the return type is a function type or array
|
| 6722 |
type.
|
| 6723 |
|
| 6724 |
— *end note*]
|
| 6725 |
|
| 6726 |
-
[*Example
|
| 6727 |
|
| 6728 |
In the following example, assuming a `signed char` cannot represent the
|
| 6729 |
value 1000, a narrowing conversion [[dcl.init.list]] would be required
|
| 6730 |
to convert the *template-argument* of type `int` to `signed char`,
|
| 6731 |
therefore substitution fails for the second template
|
|
@@ -6911,15 +6635,17 @@ that allow a difference:
|
|
| 6911 |
template <typename... T> struct X;
|
| 6912 |
template <> struct X<> {};
|
| 6913 |
template <typename T, typename... Ts>
|
| 6914 |
struct X<T, Ts...> : X<Ts...> {};
|
| 6915 |
struct D : X<int> {};
|
|
|
|
| 6916 |
|
| 6917 |
template <typename... T>
|
| 6918 |
int f(const X<T...>&);
|
| 6919 |
int x = f(D()); // calls f<int>, not f<>
|
| 6920 |
// B is X<>, C is X<int>
|
|
|
|
| 6921 |
```
|
| 6922 |
|
| 6923 |
— *end example*]
|
| 6924 |
|
| 6925 |
These alternatives are considered only if type deduction would otherwise
|
|
@@ -6978,62 +6704,33 @@ template <class T> T g(T);
|
|
| 6978 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 6979 |
```
|
| 6980 |
|
| 6981 |
— *end example*]
|
| 6982 |
|
| 6983 |
-
If deduction succeeds for all parameters that contain
|
| 6984 |
-
*template-parameter*s that participate in template argument deduction,
|
| 6985 |
-
and all template arguments are explicitly specified, deduced, or
|
| 6986 |
-
obtained from default template arguments, remaining parameters are then
|
| 6987 |
-
compared with the corresponding arguments. For each remaining parameter
|
| 6988 |
-
`P` with a type that was non-dependent before substitution of any
|
| 6989 |
-
explicitly-specified template arguments, if the corresponding argument
|
| 6990 |
-
`A` cannot be implicitly converted to `P`, deduction fails.
|
| 6991 |
-
|
| 6992 |
-
[*Note 2*: Parameters with dependent types in which no
|
| 6993 |
-
*template-parameter*s participate in template argument deduction, and
|
| 6994 |
-
parameters that became non-dependent due to substitution of
|
| 6995 |
-
explicitly-specified template arguments, will be checked during overload
|
| 6996 |
-
resolution. — *end note*]
|
| 6997 |
-
|
| 6998 |
-
[*Example 9*:
|
| 6999 |
-
|
| 7000 |
-
``` cpp
|
| 7001 |
-
template <class T> struct Z {
|
| 7002 |
-
typedef typename T::x xx;
|
| 7003 |
-
};
|
| 7004 |
-
template <class T> typename Z<T>::xx f(void *, T); // #1
|
| 7005 |
-
template <class T> void f(int, T); // #2
|
| 7006 |
-
struct A {} a;
|
| 7007 |
-
int main() {
|
| 7008 |
-
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 7009 |
-
}
|
| 7010 |
-
```
|
| 7011 |
-
|
| 7012 |
-
— *end example*]
|
| 7013 |
-
|
| 7014 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 7015 |
|
| 7016 |
Template arguments can be deduced from the type specified when taking
|
| 7017 |
-
the address of an
|
| 7018 |
-
|
| 7019 |
-
|
| 7020 |
-
|
| 7021 |
-
|
| 7022 |
|
| 7023 |
A placeholder type [[dcl.spec.auto]] in the return type of a function
|
| 7024 |
template is a non-deduced context. If template argument deduction
|
| 7025 |
succeeds for such a function, the return type is determined from
|
| 7026 |
instantiation of the function body.
|
| 7027 |
|
| 7028 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 7029 |
|
| 7030 |
Template argument deduction is done by comparing the return type of the
|
| 7031 |
-
conversion function template (call it `P`) with the type
|
| 7032 |
-
|
| 7033 |
-
|
| 7034 |
-
|
|
|
|
|
|
|
| 7035 |
|
| 7036 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 7037 |
of `P` for type deduction and for any further references to or
|
| 7038 |
transformations of `P` in the remainder of this subclause.
|
| 7039 |
|
|
@@ -7051,26 +6748,24 @@ If `A` is not a reference type:
|
|
| 7051 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 7052 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 7053 |
referred to by `A` is used for type deduction.
|
| 7054 |
|
| 7055 |
In general, the deduction process attempts to find template argument
|
| 7056 |
-
values that will make the deduced `A` identical to `A`. However,
|
| 7057 |
-
|
| 7058 |
|
| 7059 |
-
- If the original `A` is a reference type,
|
| 7060 |
-
|
| 7061 |
-
- If the original `A` is a function pointer
|
| 7062 |
-
function
|
| 7063 |
-
|
| 7064 |
-
|
| 7065 |
-
|
| 7066 |
-
- The deduced `A` can be another pointer or pointer-to-member type that
|
| 7067 |
-
can be converted to `A` via a qualification conversion.
|
| 7068 |
|
| 7069 |
-
These
|
| 7070 |
-
fail. If
|
| 7071 |
-
deduction fails.
|
| 7072 |
|
| 7073 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 7074 |
|
| 7075 |
Template argument deduction is done by comparing certain types
|
| 7076 |
associated with the two function templates being compared.
|
|
@@ -7092,11 +6787,11 @@ as the parameter template.
|
|
| 7092 |
|
| 7093 |
The types used to determine the ordering depend on the context in which
|
| 7094 |
the partial ordering is done:
|
| 7095 |
|
| 7096 |
- In the context of a function call, the types used are those function
|
| 7097 |
-
parameter types for which the function call has arguments.[^
|
| 7098 |
- In the context of a call to a conversion function, the return types of
|
| 7099 |
the conversion function templates are used.
|
| 7100 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 7101 |
type is used.
|
| 7102 |
|
|
@@ -7237,12 +6932,12 @@ deduction fails. The type of a type parameter is only deduced from an
|
|
| 7237 |
array bound if it is not otherwise deduced.
|
| 7238 |
|
| 7239 |
A given type `P` can be composed from a number of other types,
|
| 7240 |
templates, and non-type values:
|
| 7241 |
|
| 7242 |
-
- A function type includes the types of each of the function parameters
|
| 7243 |
-
|
| 7244 |
- A pointer-to-member type includes the type of the class object pointed
|
| 7245 |
to and the type of the member pointed to.
|
| 7246 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 7247 |
includes the types, templates, and non-type values referenced by the
|
| 7248 |
template argument list of the specialization.
|
|
@@ -7315,14 +7010,14 @@ inconsistent template argument deductions:
|
|
| 7315 |
``` cpp
|
| 7316 |
template<class T> void f(T x, T y) { ... }
|
| 7317 |
struct A { ... };
|
| 7318 |
struct B : A { ... };
|
| 7319 |
void g(A a, B b) {
|
| 7320 |
-
f(a,b); // error: T
|
| 7321 |
-
f(b,a); // error: T
|
| 7322 |
-
f(a,a); // OK
|
| 7323 |
-
f(b,b); // OK
|
| 7324 |
}
|
| 7325 |
```
|
| 7326 |
|
| 7327 |
Here is an example where two template arguments are deduced from a
|
| 7328 |
single function parameter/argument pair. This can lead to conflicts that
|
|
@@ -7334,13 +7029,32 @@ template <class T, class U> void f( T (*)( T, U, U ) );
|
|
| 7334 |
int g1( int, float, float);
|
| 7335 |
char g2( int, float, float);
|
| 7336 |
int g3( int, char, float);
|
| 7337 |
|
| 7338 |
void r() {
|
| 7339 |
-
f(g1); // OK
|
| 7340 |
-
f(g2); // error: T
|
| 7341 |
-
f(g3); // error: U
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7342 |
}
|
| 7343 |
```
|
| 7344 |
|
| 7345 |
Here is an example where a qualification conversion applies between the
|
| 7346 |
argument type on the function call and the deduced template argument
|
|
@@ -7370,49 +7084,50 @@ void t() {
|
|
| 7370 |
}
|
| 7371 |
```
|
| 7372 |
|
| 7373 |
— *end example*]
|
| 7374 |
|
| 7375 |
-
A template type argument `T`, a template template argument `TT` or a
|
| 7376 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 7377 |
the following forms:
|
| 7378 |
|
| 7379 |
``` cpp
|
| 7380 |
-
T
|
| 7381 |
-
cv T
|
| 7382 |
T*
|
| 7383 |
T&
|
| 7384 |
T&&
|
| 7385 |
-
T[
|
| 7386 |
-
|
| 7387 |
-
|
| 7388 |
-
T
|
| 7389 |
-
|
| 7390 |
-
|
| 7391 |
-
|
| 7392 |
-
T T::*
|
| 7393 |
-
T (type::*)()
|
| 7394 |
-
type (T::*)()
|
| 7395 |
-
type (type::*)(T)
|
| 7396 |
-
type (T::*)(T)
|
| 7397 |
-
T (type::*)(T)
|
| 7398 |
-
T (T::*)()
|
| 7399 |
-
T (T::*)(T)
|
| 7400 |
-
type[i]
|
| 7401 |
-
template-name<i> (where template-name refers to a class template)
|
| 7402 |
-
TT<T>
|
| 7403 |
-
TT<i>
|
| 7404 |
-
TT<>
|
| 7405 |
```
|
| 7406 |
|
| 7407 |
-
where
|
| 7408 |
-
|
| 7409 |
-
|
| 7410 |
-
|
| 7411 |
-
|
| 7412 |
-
|
| 7413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7414 |
|
| 7415 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 7416 |
the respective template argument list of `P` is compared with the
|
| 7417 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 7418 |
`A`. If the template argument list of `P` contains a pack expansion that
|
|
@@ -7456,11 +7171,11 @@ parameters of the top-level parameter-type-list of `P` and `A`,
|
|
| 7456 |
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 7457 |
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 7458 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 7459 |
is changed to simply `T`).
|
| 7460 |
|
| 7461 |
-
[*Note
|
| 7462 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 7463 |
`X&`. — *end note*]
|
| 7464 |
|
| 7465 |
[*Example 5*:
|
| 7466 |
|
|
@@ -7563,23 +7278,40 @@ using V = decltype(sizeof 0);
|
|
| 7563 |
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 7564 |
```
|
| 7565 |
|
| 7566 |
— *end example*]
|
| 7567 |
|
|
|
|
|
|
|
|
|
|
| 7568 |
[*Example 10*:
|
| 7569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7570 |
``` cpp
|
| 7571 |
template<class T, T i> void f(int (&a)[i]);
|
| 7572 |
int v[10];
|
| 7573 |
void g() {
|
| 7574 |
-
f(v); // OK
|
| 7575 |
}
|
| 7576 |
```
|
| 7577 |
|
| 7578 |
— *end example*]
|
| 7579 |
|
| 7580 |
-
[*Note
|
| 7581 |
|
| 7582 |
Except for reference and pointer types, a major array bound is not part
|
| 7583 |
of a function parameter type and cannot be deduced from an argument:
|
| 7584 |
|
| 7585 |
``` cpp
|
|
@@ -7587,28 +7319,28 @@ template<int i> void f1(int a[10][i]);
|
|
| 7587 |
template<int i> void f2(int a[i][20]);
|
| 7588 |
template<int i> void f3(int (&a)[i][20]);
|
| 7589 |
|
| 7590 |
void g() {
|
| 7591 |
int v[10][20];
|
| 7592 |
-
f1(v); // OK
|
| 7593 |
f1<20>(v); // OK
|
| 7594 |
f2(v); // error: cannot deduce template-argument i
|
| 7595 |
f2<10>(v); // OK
|
| 7596 |
-
f3(v); // OK
|
| 7597 |
}
|
| 7598 |
```
|
| 7599 |
|
| 7600 |
— *end note*]
|
| 7601 |
|
| 7602 |
-
[*Note
|
| 7603 |
|
| 7604 |
If, in the declaration of a function template with a non-type template
|
| 7605 |
parameter, the non-type template parameter is used in a subexpression in
|
| 7606 |
the function parameter list, the expression is a non-deduced context as
|
| 7607 |
specified above.
|
| 7608 |
|
| 7609 |
-
[*Example
|
| 7610 |
|
| 7611 |
``` cpp
|
| 7612 |
template <int i> class A { ... };
|
| 7613 |
template <int i> void g(A<i+1>);
|
| 7614 |
template <int i> void f(A<i>, A<i+1>);
|
|
@@ -7623,11 +7355,11 @@ void k() {
|
|
| 7623 |
|
| 7624 |
— *end example*]
|
| 7625 |
|
| 7626 |
— *end note*]
|
| 7627 |
|
| 7628 |
-
[*Note
|
| 7629 |
|
| 7630 |
Template parameters do not participate in template argument deduction if
|
| 7631 |
they are used only in non-deduced contexts. For example,
|
| 7632 |
|
| 7633 |
``` cpp
|
|
@@ -7647,13 +7379,16 @@ int x = deduce<77>(a.xm, 62, b.ym);
|
|
| 7647 |
|
| 7648 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 7649 |
from the type of the corresponding template parameter of the template
|
| 7650 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 7651 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 7652 |
-
type, deduction fails.[^
|
| 7653 |
|
| 7654 |
-
|
|
|
|
|
|
|
|
|
|
| 7655 |
|
| 7656 |
``` cpp
|
| 7657 |
template<int i> class A { ... };
|
| 7658 |
template<short s> void f(A<s>);
|
| 7659 |
void k1() {
|
|
@@ -7664,20 +7399,20 @@ void k1() {
|
|
| 7664 |
|
| 7665 |
template<const short cs> class B { };
|
| 7666 |
template<short s> void g(B<s>);
|
| 7667 |
void k2() {
|
| 7668 |
B<1> b;
|
| 7669 |
-
g(b); // OK
|
| 7670 |
}
|
| 7671 |
```
|
| 7672 |
|
| 7673 |
— *end example*]
|
| 7674 |
|
| 7675 |
A *template-argument* can be deduced from a function, pointer to
|
| 7676 |
function, or pointer-to-member-function type.
|
| 7677 |
|
| 7678 |
-
[*Example
|
| 7679 |
|
| 7680 |
``` cpp
|
| 7681 |
template<class T> void f(void(*)(T,int));
|
| 7682 |
template<class T> void foo(T,int);
|
| 7683 |
void g(int,int);
|
|
@@ -7685,38 +7420,38 @@ void g(char,int);
|
|
| 7685 |
|
| 7686 |
void h(int,int,int);
|
| 7687 |
void h(char,int);
|
| 7688 |
int m() {
|
| 7689 |
f(&g); // error: ambiguous
|
| 7690 |
-
f(&h); // OK
|
| 7691 |
f(&foo); // error: type deduction fails because foo is a template
|
| 7692 |
}
|
| 7693 |
```
|
| 7694 |
|
| 7695 |
— *end example*]
|
| 7696 |
|
| 7697 |
A template *type-parameter* cannot be deduced from the type of a
|
| 7698 |
function default argument.
|
| 7699 |
|
| 7700 |
-
[*Example
|
| 7701 |
|
| 7702 |
``` cpp
|
| 7703 |
template <class T> void f(T = 5, T = 7);
|
| 7704 |
void g() {
|
| 7705 |
-
f(1); // OK
|
| 7706 |
f(); // error: cannot deduce T
|
| 7707 |
-
f<int>(); // OK
|
| 7708 |
}
|
| 7709 |
```
|
| 7710 |
|
| 7711 |
— *end example*]
|
| 7712 |
|
| 7713 |
The *template-argument* corresponding to a template *template-parameter*
|
| 7714 |
is deduced from the type of the *template-argument* of a class template
|
| 7715 |
specialization used in the argument list of a function call.
|
| 7716 |
|
| 7717 |
-
[*Example
|
| 7718 |
|
| 7719 |
``` cpp
|
| 7720 |
template <template <class T> class X> struct A { };
|
| 7721 |
template <template <class T> class X> void f(A<X>) { }
|
| 7722 |
template<class T> struct B { };
|
|
@@ -7724,15 +7459,15 @@ A<B> ab;
|
|
| 7724 |
f(ab); // calls f(A<B>)
|
| 7725 |
```
|
| 7726 |
|
| 7727 |
— *end example*]
|
| 7728 |
|
| 7729 |
-
[*Note
|
| 7730 |
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 7731 |
pack. — *end note*]
|
| 7732 |
|
| 7733 |
-
[*Example
|
| 7734 |
|
| 7735 |
``` cpp
|
| 7736 |
template<class> struct X { };
|
| 7737 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 7738 |
template<class ... Types> struct Y { };
|
|
@@ -7742,11 +7477,11 @@ template<class ... Types> int f(void (*)(Types ...));
|
|
| 7742 |
void g(int, float);
|
| 7743 |
|
| 7744 |
X<int> x1; // uses primary template
|
| 7745 |
X<int(int, float, double)> x2; // uses partial specialization; ArgTypes contains float, double
|
| 7746 |
X<int(float, int)> x3; // uses primary template
|
| 7747 |
-
Y<> y1; //
|
| 7748 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 7749 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 7750 |
int fv = f(g); // OK; Types contains int, float
|
| 7751 |
```
|
| 7752 |
|
|
@@ -7758,12 +7493,12 @@ In a declaration whose *declarator-id* refers to a specialization of a
|
|
| 7758 |
function template, template argument deduction is performed to identify
|
| 7759 |
the specialization to which the declaration refers. Specifically, this
|
| 7760 |
is done for explicit instantiations [[temp.explicit]], explicit
|
| 7761 |
specializations [[temp.expl.spec]], and certain friend declarations
|
| 7762 |
[[temp.friend]]. This is also done to determine whether a deallocation
|
| 7763 |
-
function template specialization matches a placement `operator new`
|
| 7764 |
-
[[basic.stc.dynamic.deallocation]], [[expr.new]]
|
| 7765 |
`P` is the type of the function template being considered as a potential
|
| 7766 |
match and `A` is either the function type from the declaration or the
|
| 7767 |
type of the deallocation function that would match the placement
|
| 7768 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 7769 |
described in [[temp.deduct.type]].
|
|
@@ -7773,29 +7508,30 @@ match or more than one match after partial ordering has been considered
|
|
| 7773 |
[[temp.func.order]], deduction fails and, in the declaration cases, the
|
| 7774 |
program is ill-formed.
|
| 7775 |
|
| 7776 |
### Overload resolution <a id="temp.over">[[temp.over]]</a>
|
| 7777 |
|
| 7778 |
-
When a call
|
| 7779 |
-
|
| 7780 |
-
|
| 7781 |
-
|
| 7782 |
-
|
| 7783 |
-
|
| 7784 |
-
|
|
|
|
| 7785 |
the argument deduction and checking succeeds, the *template-argument*s
|
| 7786 |
(deduced and/or explicit) are used to synthesize the declaration of a
|
| 7787 |
single function template specialization which is added to the candidate
|
| 7788 |
functions set to be used in overload resolution. If, for a given
|
| 7789 |
function template, argument deduction fails or the synthesized function
|
| 7790 |
template specialization would be ill-formed, no such function is added
|
| 7791 |
to the set of candidate functions for that template. The complete set of
|
| 7792 |
candidate functions includes all the synthesized declarations and all of
|
| 7793 |
-
the non-template
|
| 7794 |
declarations are treated like any other functions in the remainder of
|
| 7795 |
overload resolution, except as explicitly noted in
|
| 7796 |
-
[[over.match.best]].[^
|
| 7797 |
|
| 7798 |
[*Example 1*:
|
| 7799 |
|
| 7800 |
``` cpp
|
| 7801 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
@@ -7812,11 +7548,11 @@ Adding the non-template function
|
|
| 7812 |
``` cpp
|
| 7813 |
int max(int,int);
|
| 7814 |
```
|
| 7815 |
|
| 7816 |
to the example above would resolve the third call, by providing a
|
| 7817 |
-
function that
|
| 7818 |
conversion of `char` to `int` for `c`.
|
| 7819 |
|
| 7820 |
— *end example*]
|
| 7821 |
|
| 7822 |
[*Example 2*:
|
|
@@ -7866,46 +7602,42 @@ a template specialization is a candidate.
|
|
| 7866 |
|
| 7867 |
``` cpp
|
| 7868 |
template<class T> void f(T); // declaration
|
| 7869 |
|
| 7870 |
void g() {
|
| 7871 |
-
f("Annemarie"); //
|
| 7872 |
}
|
| 7873 |
```
|
| 7874 |
|
| 7875 |
-
The call
|
| 7876 |
and not defined at the point of the call. The program will be ill-formed
|
| 7877 |
-
unless a specialization for `f<const char*>`
|
| 7878 |
-
|
| 7879 |
|
| 7880 |
— *end example*]
|
| 7881 |
|
| 7882 |
<!-- Link reference definitions -->
|
| 7883 |
[basic.def]: basic.md#basic.def
|
| 7884 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 7885 |
[basic.link]: basic.md#basic.link
|
| 7886 |
[basic.lookup]: basic.md#basic.lookup
|
| 7887 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 7888 |
-
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 7889 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 7890 |
-
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 7891 |
-
[basic.scope]: basic.md#basic.scope
|
| 7892 |
-
[basic.scope.hiding]: basic.md#basic.scope.hiding
|
| 7893 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
|
|
|
| 7894 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 7895 |
[basic.types]: basic.md#basic.types
|
| 7896 |
[class.access]: class.md#class.access
|
| 7897 |
[class.base.init]: class.md#class.base.init
|
| 7898 |
[class.conv.fct]: class.md#class.conv.fct
|
| 7899 |
[class.ctor]: class.md#class.ctor
|
| 7900 |
[class.default.ctor]: class.md#class.default.ctor
|
| 7901 |
[class.derived]: class.md#class.derived
|
| 7902 |
[class.dtor]: class.md#class.dtor
|
| 7903 |
-
[class.friend]: class.md#class.friend
|
| 7904 |
[class.local]: class.md#class.local
|
| 7905 |
[class.mem]: class.md#class.mem
|
| 7906 |
-
[class.member.lookup]:
|
| 7907 |
[class.pre]: class.md#class.pre
|
| 7908 |
[class.qual]: basic.md#class.qual
|
| 7909 |
[class.temporary]: basic.md#class.temporary
|
| 7910 |
[conv]: expr.md#conv
|
| 7911 |
[conv.array]: expr.md#conv.array
|
|
@@ -7914,11 +7646,10 @@ explicitly generated, is present in some translation unit.
|
|
| 7914 |
[conv.lval]: expr.md#conv.lval
|
| 7915 |
[conv.qual]: expr.md#conv.qual
|
| 7916 |
[dcl.align]: dcl.md#dcl.align
|
| 7917 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 7918 |
[dcl.decl]: dcl.md#dcl.decl
|
| 7919 |
-
[dcl.enum]: dcl.md#dcl.enum
|
| 7920 |
[dcl.fct]: dcl.md#dcl.fct
|
| 7921 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 7922 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 7923 |
[dcl.init]: dcl.md#dcl.init
|
| 7924 |
[dcl.init.list]: dcl.md#dcl.init.list
|
|
@@ -7928,106 +7659,119 @@ explicitly generated, is present in some translation unit.
|
|
| 7928 |
[dcl.stc]: dcl.md#dcl.stc
|
| 7929 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 7930 |
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 7931 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 7932 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
|
|
|
| 7933 |
[except.spec]: except.md#except.spec
|
| 7934 |
[expr.const]: expr.md#expr.const
|
| 7935 |
[expr.context]: expr.md#expr.context
|
| 7936 |
[expr.log.and]: expr.md#expr.log.and
|
| 7937 |
[expr.log.or]: expr.md#expr.log.or
|
| 7938 |
[expr.new]: expr.md#expr.new
|
| 7939 |
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 7940 |
[expr.prim.id]: expr.md#expr.prim.id
|
|
|
|
| 7941 |
[expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
|
| 7942 |
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 7943 |
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
|
|
|
| 7944 |
[expr.ref]: expr.md#expr.ref
|
| 7945 |
[expr.sizeof]: expr.md#expr.sizeof
|
|
|
|
| 7946 |
[expr.typeid]: expr.md#expr.typeid
|
| 7947 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 7948 |
[implimits]: limits.md#implimits
|
| 7949 |
[intro.defs]: intro.md#intro.defs
|
| 7950 |
[intro.object]: basic.md#intro.object
|
| 7951 |
[lex.string]: lex.md#lex.string
|
| 7952 |
-
[
|
| 7953 |
-
[namespace.memdef]: dcl.md#namespace.memdef
|
| 7954 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 7955 |
-
[over.ics.rank]: over.md#over.ics.rank
|
| 7956 |
[over.match]: over.md#over.match
|
| 7957 |
[over.match.best]: over.md#over.match.best
|
| 7958 |
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 7959 |
-
[over.match.
|
| 7960 |
[over.match.oper]: over.md#over.match.oper
|
| 7961 |
-
[over.match.ref]: over.md#over.match.ref
|
| 7962 |
[over.match.viable]: over.md#over.match.viable
|
| 7963 |
[over.over]: over.md#over.over
|
| 7964 |
[special]: class.md#special
|
| 7965 |
[stmt.if]: stmt.md#stmt.if
|
| 7966 |
[support.types]: support.md#support.types
|
| 7967 |
[temp]: #temp
|
| 7968 |
[temp.alias]: #temp.alias
|
| 7969 |
[temp.arg]: #temp.arg
|
| 7970 |
[temp.arg.explicit]: #temp.arg.explicit
|
|
|
|
| 7971 |
[temp.arg.nontype]: #temp.arg.nontype
|
| 7972 |
[temp.arg.template]: #temp.arg.template
|
| 7973 |
[temp.arg.type]: #temp.arg.type
|
| 7974 |
[temp.class]: #temp.class
|
| 7975 |
-
[temp.class.
|
| 7976 |
-
[temp.class.spec]: #temp.class.spec
|
| 7977 |
-
[temp.class.spec.match]: #temp.class.spec.match
|
| 7978 |
-
[temp.class.spec.mfunc]: #temp.class.spec.mfunc
|
| 7979 |
[temp.concept]: #temp.concept
|
| 7980 |
[temp.constr]: #temp.constr
|
| 7981 |
[temp.constr.atomic]: #temp.constr.atomic
|
| 7982 |
[temp.constr.constr]: #temp.constr.constr
|
|
|
|
| 7983 |
[temp.constr.decl]: #temp.constr.decl
|
|
|
|
| 7984 |
[temp.constr.normal]: #temp.constr.normal
|
| 7985 |
[temp.constr.op]: #temp.constr.op
|
| 7986 |
[temp.constr.order]: #temp.constr.order
|
| 7987 |
[temp.decls]: #temp.decls
|
|
|
|
| 7988 |
[temp.deduct]: #temp.deduct
|
| 7989 |
[temp.deduct.call]: #temp.deduct.call
|
| 7990 |
[temp.deduct.conv]: #temp.deduct.conv
|
| 7991 |
[temp.deduct.decl]: #temp.deduct.decl
|
| 7992 |
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
|
|
|
| 7993 |
[temp.deduct.guide]: #temp.deduct.guide
|
| 7994 |
[temp.deduct.partial]: #temp.deduct.partial
|
| 7995 |
[temp.deduct.type]: #temp.deduct.type
|
| 7996 |
[temp.dep]: #temp.dep
|
| 7997 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 7998 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 7999 |
[temp.dep.expr]: #temp.dep.expr
|
|
|
|
| 8000 |
[temp.dep.res]: #temp.dep.res
|
| 8001 |
[temp.dep.temp]: #temp.dep.temp
|
| 8002 |
[temp.dep.type]: #temp.dep.type
|
| 8003 |
[temp.expl.spec]: #temp.expl.spec
|
| 8004 |
[temp.explicit]: #temp.explicit
|
| 8005 |
[temp.fct]: #temp.fct
|
|
|
|
| 8006 |
[temp.fct.spec]: #temp.fct.spec
|
|
|
|
| 8007 |
[temp.fold.empty]: #temp.fold.empty
|
| 8008 |
[temp.friend]: #temp.friend
|
| 8009 |
[temp.func.order]: #temp.func.order
|
| 8010 |
-
[temp.inject]: #temp.inject
|
| 8011 |
[temp.inst]: #temp.inst
|
| 8012 |
[temp.local]: #temp.local
|
| 8013 |
[temp.mem]: #temp.mem
|
| 8014 |
[temp.mem.class]: #temp.mem.class
|
| 8015 |
[temp.mem.enum]: #temp.mem.enum
|
| 8016 |
[temp.mem.func]: #temp.mem.func
|
| 8017 |
[temp.names]: #temp.names
|
| 8018 |
-
[temp.nondep]: #temp.nondep
|
| 8019 |
[temp.over]: #temp.over
|
| 8020 |
[temp.over.link]: #temp.over.link
|
| 8021 |
[temp.param]: #temp.param
|
| 8022 |
[temp.point]: #temp.point
|
| 8023 |
[temp.pre]: #temp.pre
|
| 8024 |
[temp.res]: #temp.res
|
|
|
|
| 8025 |
[temp.spec]: #temp.spec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8026 |
[temp.static]: #temp.static
|
| 8027 |
[temp.type]: #temp.type
|
| 8028 |
[temp.variadic]: #temp.variadic
|
|
|
|
|
|
|
| 8029 |
|
| 8030 |
[^1]: Since template *template-parameter*s and template
|
| 8031 |
*template-argument*s are treated as types for descriptive purposes,
|
| 8032 |
the terms *non-type parameter* and *non-type argument* are used to
|
| 8033 |
refer to non-type, non-template parameters and arguments.
|
|
@@ -8041,51 +7785,50 @@ explicitly generated, is present in some translation unit.
|
|
| 8041 |
because the form of the *template-parameter* determines the
|
| 8042 |
allowable forms of the *template-argument*.
|
| 8043 |
|
| 8044 |
[^4]: A constraint is in disjunctive normal form when it is a
|
| 8045 |
disjunction of clauses where each clause is a conjunction of atomic
|
| 8046 |
-
constraints.
|
| 8047 |
-
|
| 8048 |
-
\[*Example 5*: For atomic constraints A, B, and C, the disjunctive
|
| 8049 |
normal form of the constraint A ∧ (B ∨ C) is (A ∧ B) ∨ (A ∧ C). Its
|
| 8050 |
-
disjunctive clauses are (A ∧ B) and (A ∧ C).
|
| 8051 |
|
| 8052 |
[^5]: A constraint is in conjunctive normal form when it is a
|
| 8053 |
conjunction of clauses where each clause is a disjunction of atomic
|
| 8054 |
-
constraints.
|
| 8055 |
-
|
| 8056 |
-
\[*Example 6*: For atomic constraints A, B, and C, the constraint
|
| 8057 |
A ∧ (B ∨ C) is in conjunctive normal form. Its conjunctive clauses
|
| 8058 |
-
are A and (B ∨ C).
|
| 8059 |
|
| 8060 |
[^6]: The identity of enumerators is not preserved.
|
| 8061 |
|
| 8062 |
[^7]: An array as a *template-parameter* decays to a pointer.
|
| 8063 |
|
| 8064 |
-
[^8]: There is no
|
| 8065 |
|
| 8066 |
[^9]: That is, declarations of non-template functions do not merely
|
| 8067 |
guide overload resolution of function template specializations with
|
| 8068 |
the same name. If such a non-template function is odr-used
|
| 8069 |
-
[[
|
| 8070 |
implicitly instantiated using the function template definition.
|
| 8071 |
|
| 8072 |
[^10]: This includes friend function declarations.
|
| 8073 |
|
| 8074 |
-
[^11]:
|
| 8075 |
-
|
| 8076 |
|
| 8077 |
-
[^12]:
|
|
|
|
|
|
|
|
|
|
| 8078 |
context; they only become arguments after a function has been
|
| 8079 |
selected.
|
| 8080 |
|
| 8081 |
-
[^
|
| 8082 |
-
*template-parameter* of type `bool`
|
| 8083 |
bound, the resulting value will always be `true` because the array
|
| 8084 |
bound will be nonzero.
|
| 8085 |
|
| 8086 |
-
[^
|
| 8087 |
template parameter types. The set of conversions allowed on deduced
|
| 8088 |
arguments is limited, because the argument deduction process
|
| 8089 |
produces function templates with parameters that either match the
|
| 8090 |
call arguments exactly or differ only in ways that can be bridged by
|
| 8091 |
the allowed limited conversions. Non-deduced arguments allow the
|
|
|
|
| 38 |
primary-expression
|
| 39 |
constraint-logical-and-expression '&&' primary-expression
|
| 40 |
```
|
| 41 |
|
| 42 |
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 43 |
+
*template-declaration* can be the product of replacing a `>>` token by
|
| 44 |
two consecutive `>` tokens [[temp.names]]. — *end note*]
|
| 45 |
|
| 46 |
The *declaration* in a *template-declaration* (if any) shall
|
| 47 |
|
| 48 |
- declare or define a function, a class, or a variable, or
|
|
|
|
| 78 |
};
|
| 79 |
```
|
| 80 |
|
| 81 |
— *end example*]
|
| 82 |
|
| 83 |
+
[*Note 2*: A *template-declaration* can appear only as a namespace
|
| 84 |
+
scope or class scope declaration. — *end note*]
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
Its *declaration* shall not be an *export-declaration*. In a function
|
| 87 |
+
template declaration, the *unqualified-id* of the *declarator-id* shall
|
| 88 |
+
be a name.
|
| 89 |
+
|
| 90 |
+
[*Note 3*: A class or variable template declaration of a
|
| 91 |
+
*simple-template-id* declares a partial specialization
|
| 92 |
+
[[temp.spec.partial]]. — *end note*]
|
| 93 |
|
| 94 |
In a *template-declaration*, explicit specialization, or explicit
|
| 95 |
instantiation the *init-declarator-list* in the declaration shall
|
| 96 |
contain at most one declarator. When such a declaration is used to
|
| 97 |
declare a class template, no declarator is permitted.
|
| 98 |
|
| 99 |
+
A specialization (explicit or implicit) of one template is distinct from
|
| 100 |
+
all specializations of any other template. A template, an explicit
|
| 101 |
+
specialization [[temp.expl.spec]], and a partial specialization shall
|
| 102 |
+
not have C language linkage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
[*Note 4*: Default arguments for function templates and for member
|
| 105 |
functions of class templates are considered definitions for the purpose
|
| 106 |
+
of template instantiation [[temp.decls]] and must obey the
|
| 107 |
+
one-definition rule [[basic.def.odr]]. — *end note*]
|
| 108 |
|
| 109 |
+
[*Note 5*: A template cannot have the same name as any other name bound
|
| 110 |
+
in the same scope [[basic.scope.scope]], except that a function template
|
| 111 |
+
can share a name with non-template functions [[dcl.fct]] and/or function
|
| 112 |
+
templates [[temp.over]]. Specializations, including partial
|
| 113 |
+
specializations [[temp.spec.partial]], do not reintroduce or bind names.
|
| 114 |
+
Their target scope is the target scope of the primary template, so all
|
| 115 |
+
specializations of a template belong to the same scope as it
|
| 116 |
+
does. — *end note*]
|
| 117 |
|
| 118 |
An entity is *templated* if it is
|
| 119 |
|
| 120 |
- a template,
|
| 121 |
- an entity defined [[basic.def]] or created [[class.temporary]] in a
|
|
|
|
| 123 |
- a member of a templated entity,
|
| 124 |
- an enumerator for an enumeration that is a templated entity, or
|
| 125 |
- the closure type of a *lambda-expression* [[expr.prim.lambda.closure]]
|
| 126 |
appearing in the declaration of a templated entity.
|
| 127 |
|
| 128 |
+
[*Note 6*: A local class, a local or block variable, or a friend
|
| 129 |
+
function defined in a templated entity is a templated
|
| 130 |
+
entity. — *end note*]
|
| 131 |
+
|
| 132 |
+
A *templated function* is a function template or a function that is
|
| 133 |
+
templated. A *templated class* is a class template or a class that is
|
| 134 |
+
templated. A *templated variable* is a variable template or a variable
|
| 135 |
+
that is templated.
|
| 136 |
|
| 137 |
A *template-declaration* is written in terms of its template parameters.
|
| 138 |
The optional *requires-clause* following a *template-parameter-list*
|
| 139 |
allows the specification of constraints [[temp.constr.decl]] on template
|
| 140 |
arguments [[temp.arg]]. The *requires-clause* introduces the
|
| 141 |
*constraint-expression* that results from interpreting the
|
| 142 |
*constraint-logical-or-expression* as a *constraint-expression*. The
|
| 143 |
*constraint-logical-or-expression* of a *requires-clause* is an
|
| 144 |
unevaluated operand [[expr.context]].
|
| 145 |
|
| 146 |
+
[*Note 7*:
|
| 147 |
|
| 148 |
The expression in a *requires-clause* uses a restricted grammar to avoid
|
| 149 |
ambiguities. Parentheses can be used to specify arbitrary expressions in
|
| 150 |
a *requires-clause*.
|
| 151 |
|
|
|
|
| 197 |
type-constraint:
|
| 198 |
nested-name-specifierₒₚₜ concept-name
|
| 199 |
nested-name-specifierₒₚₜ concept-name '<' template-argument-listₒₚₜ '>'
|
| 200 |
```
|
| 201 |
|
| 202 |
+
The component names of a *type-constraint* are its *concept-name* and
|
| 203 |
+
those of its *nested-name-specifier* (if any).
|
| 204 |
+
|
| 205 |
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 206 |
+
*type-parameter* can be the product of replacing a `>>` token by two
|
| 207 |
consecutive `>` tokens [[temp.names]]. — *end note*]
|
| 208 |
|
| 209 |
There is no semantic difference between `class` and `typename` in a
|
| 210 |
*type-parameter-key*. `typename` followed by an *unqualified-id* names a
|
| 211 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 212 |
+
the type in a non-type[^1]
|
| 213 |
+
|
| 214 |
+
*parameter-declaration*. A *template-parameter* of the form `class`
|
| 215 |
+
*identifier* is a *type-parameter*.
|
| 216 |
|
| 217 |
[*Example 1*:
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
class T { ... };
|
|
|
|
| 233 |
|
| 234 |
A storage class shall not be specified in a *template-parameter*
|
| 235 |
declaration. Types shall not be defined in a *template-parameter*
|
| 236 |
declaration.
|
| 237 |
|
| 238 |
+
The *identifier* in a *type-parameter* is not looked up. A
|
| 239 |
+
*type-parameter* whose *identifier* does not follow an ellipsis defines
|
| 240 |
its *identifier* to be a *typedef-name* (if declared without `template`)
|
| 241 |
or *template-name* (if declared with `template`) in the scope of the
|
| 242 |
template declaration.
|
| 243 |
|
| 244 |
[*Note 2*:
|
| 245 |
|
| 246 |
+
A template argument can be a class template or alias template. For
|
| 247 |
example,
|
| 248 |
|
| 249 |
``` cpp
|
| 250 |
template<class T> class myarray { ... };
|
| 251 |
|
|
|
|
| 303 |
- an lvalue reference type, or
|
| 304 |
- a literal class type with the following properties:
|
| 305 |
- all base classes and non-static data members are public and
|
| 306 |
non-mutable and
|
| 307 |
- the types of all bases classes and non-static data members are
|
| 308 |
+
structural types or (possibly multidimensional) array thereof.
|
| 309 |
|
| 310 |
An *id-expression* naming a non-type *template-parameter* of class type
|
| 311 |
`T` denotes a static storage duration object of type `const T`, known as
|
| 312 |
a *template parameter object*, whose value is that of the corresponding
|
| 313 |
template argument after it has been converted to the type of the
|
|
|
|
| 330 |
i++; // error: change of template-parameter value
|
| 331 |
|
| 332 |
&x; // OK
|
| 333 |
&i; // error: address of non-reference template-parameter
|
| 334 |
&a; // OK
|
| 335 |
+
int& ri = i; // error: attempt to bind non-const reference to temporary
|
| 336 |
+
const int& cri = i; // OK, const reference binds to temporary
|
| 337 |
+
const A& ra = a; // OK, const reference binds to a template parameter object
|
| 338 |
}
|
| 339 |
```
|
| 340 |
|
| 341 |
— *end example*]
|
| 342 |
|
|
|
|
| 386 |
parameter pack [[temp.variadic]]. A default *template-argument* may be
|
| 387 |
specified in a template declaration. A default *template-argument* shall
|
| 388 |
not be specified in the *template-parameter-list*s of the definition of
|
| 389 |
a member of a class template that appears outside of the member’s class.
|
| 390 |
A default *template-argument* shall not be specified in a friend class
|
| 391 |
+
template declaration. If a friend function template declaration D
|
| 392 |
specifies a default *template-argument*, that declaration shall be a
|
| 393 |
+
definition and there shall be no other declaration of the function
|
| 394 |
+
template which is reachable from D or from which D is reachable.
|
| 395 |
|
| 396 |
The set of default *template-argument*s available for use is obtained by
|
| 397 |
merging the default arguments from all prior declarations of the
|
| 398 |
template in the same way default function arguments are
|
| 399 |
[[dcl.fct.default]].
|
|
|
|
| 437 |
template<class... T, class U> void g() { } // error
|
| 438 |
```
|
| 439 |
|
| 440 |
— *end example*]
|
| 441 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
When parsing a default *template-argument* for a non-type
|
| 443 |
*template-parameter*, the first non-nested `>` is taken as the end of
|
| 444 |
the *template-parameter-list* rather than a greater-than operator.
|
| 445 |
|
| 446 |
+
[*Example 8*:
|
| 447 |
|
| 448 |
``` cpp
|
| 449 |
template<int i = 3 > 4 > // syntax error
|
| 450 |
class X { ... };
|
| 451 |
|
|
|
|
| 458 |
A *template-parameter* of a template *template-parameter* is permitted
|
| 459 |
to have a default *template-argument*. When such default arguments are
|
| 460 |
specified, they apply to the template *template-parameter* in the scope
|
| 461 |
of the template *template-parameter*.
|
| 462 |
|
| 463 |
+
[*Example 9*:
|
| 464 |
|
| 465 |
``` cpp
|
| 466 |
template <template <class TT = float> class T> struct A {
|
| 467 |
inline void f();
|
| 468 |
inline void g();
|
|
|
|
| 488 |
*type-constraint* that contains an unexpanded parameter pack is a pack
|
| 489 |
expansion. A template parameter pack that is a pack expansion shall not
|
| 490 |
expand a template parameter pack declared in the same
|
| 491 |
*template-parameter-list*.
|
| 492 |
|
| 493 |
+
[*Example 10*:
|
| 494 |
|
| 495 |
``` cpp
|
| 496 |
template <class... Types> // Types is a template type parameter pack
|
| 497 |
class Tuple; // but not a pack expansion
|
| 498 |
|
|
|
|
| 543 |
constant-expression
|
| 544 |
type-id
|
| 545 |
id-expression
|
| 546 |
```
|
| 547 |
|
| 548 |
+
The component name of a *simple-template-id*, *template-id*, or
|
| 549 |
+
*template-name* is the first name in it.
|
| 550 |
+
|
| 551 |
+
A `<` is interpreted as the delimiter of a *template-argument-list* if
|
| 552 |
+
it follows a name that is not a *conversion-function-id* and
|
| 553 |
+
|
| 554 |
+
- that follows the keyword `template` or a `~` after a
|
| 555 |
+
*nested-name-specifier* or in a class member access expression, or
|
| 556 |
+
- for which name lookup finds the injected-class-name of a class
|
| 557 |
+
template or finds any declaration of a template, or
|
| 558 |
+
- that is an unqualified name for which name lookup either finds one or
|
| 559 |
+
more functions or finds nothing, or
|
| 560 |
+
- that is a terminal name in a *using-declarator* [[namespace.udecl]],
|
| 561 |
+
in a *declarator-id* [[dcl.meaning]], or in a type-only context other
|
| 562 |
+
than a *nested-name-specifier* [[temp.res]].
|
| 563 |
+
|
| 564 |
+
[*Note 1*: If the name is an *identifier*, it is then interpreted as a
|
| 565 |
+
*template-name*. The keyword `template` is used to indicate that a
|
| 566 |
+
dependent qualified name [[temp.dep.type]] denotes a template where an
|
| 567 |
+
expression might appear. — *end note*]
|
| 568 |
+
|
| 569 |
+
[*Example 1*:
|
| 570 |
+
|
| 571 |
+
``` cpp
|
| 572 |
+
struct X {
|
| 573 |
+
template<std::size_t> X* alloc();
|
| 574 |
+
template<std::size_t> static X* adjust();
|
| 575 |
+
};
|
| 576 |
+
template<class T> void f(T* p) {
|
| 577 |
+
T* p1 = p->alloc<200>(); // error: < means less than
|
| 578 |
+
T* p2 = p->template alloc<200>(); // OK, < starts template argument list
|
| 579 |
+
T::adjust<100>(); // error: < means less than
|
| 580 |
+
T::template adjust<100>(); // OK, < starts template argument list
|
| 581 |
+
}
|
| 582 |
+
```
|
| 583 |
+
|
| 584 |
+
— *end example*]
|
| 585 |
+
|
| 586 |
+
When parsing a *template-argument-list*, the first non-nested `>`[^2]
|
| 587 |
+
|
| 588 |
+
is taken as the ending delimiter rather than a greater-than operator.
|
| 589 |
+
Similarly, the first non-nested `>>` is treated as two consecutive but
|
| 590 |
distinct `>` tokens, the first of which is taken as the end of the
|
| 591 |
*template-argument-list* and completes the *template-id*.
|
| 592 |
|
| 593 |
+
[*Note 2*: The second `>` token produced by this replacement rule can
|
| 594 |
+
terminate an enclosing *template-id* construct or it can be part of a
|
| 595 |
different construct (e.g., a cast). — *end note*]
|
| 596 |
|
| 597 |
+
[*Example 2*:
|
| 598 |
|
| 599 |
``` cpp
|
| 600 |
template<int i> class X { ... };
|
| 601 |
|
| 602 |
X< 1>2 > x1; // syntax error
|
|
|
|
| 608 |
Y<X<(6>>1)>> x5; // OK
|
| 609 |
```
|
| 610 |
|
| 611 |
— *end example*]
|
| 612 |
|
| 613 |
+
The keyword `template` shall not appear immediately after a declarative
|
| 614 |
+
*nested-name-specifier* [[expr.prim.id.qual]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
+
A name prefixed by the keyword `template` shall be followed by a
|
| 617 |
+
template argument list or refer to a class template or an alias
|
| 618 |
+
template. The latter case is deprecated [[depr.template.template]]. The
|
| 619 |
+
keyword `template` shall not appear immediately before a `~` token (as
|
| 620 |
+
to name a destructor).
|
| 621 |
|
| 622 |
+
[*Note 3*: The keyword `template` cannot be applied to non-template
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
members of class templates. — *end note*]
|
| 624 |
|
| 625 |
+
[*Note 4*: As is the case with the `typename` prefix, the `template`
|
| 626 |
+
prefix is allowed even when lookup for the name would already find a
|
| 627 |
+
template. — *end note*]
|
|
|
|
|
|
|
| 628 |
|
| 629 |
[*Example 3*:
|
| 630 |
|
| 631 |
``` cpp
|
| 632 |
template <class T> struct A {
|
|
|
|
| 634 |
template <class U> void f(U);
|
| 635 |
};
|
| 636 |
|
| 637 |
template <class T> void f(T t) {
|
| 638 |
A<T> a;
|
| 639 |
+
a.template f<>(t); // OK, calls template
|
| 640 |
a.template f(t); // error: not a template-id
|
| 641 |
}
|
| 642 |
|
| 643 |
template <class T> struct B {
|
| 644 |
template <class T2> struct C { };
|
| 645 |
};
|
| 646 |
|
| 647 |
+
// deprecated: T::C is assumed to name a class template:
|
| 648 |
template <class T, template <class X> class TT = T::template C> struct D { };
|
| 649 |
D<B<int> > db;
|
| 650 |
```
|
| 651 |
|
| 652 |
— *end example*]
|
|
|
|
| 683 |
|
| 684 |
— *end example*]
|
| 685 |
|
| 686 |
When the *template-name* of a *simple-template-id* names a constrained
|
| 687 |
non-function template or a constrained template *template-parameter*,
|
| 688 |
+
and all *template-argument*s in the *simple-template-id* are
|
| 689 |
+
non-dependent [[temp.dep.temp]], the associated constraints
|
|
|
|
| 690 |
[[temp.constr.decl]] of the constrained template shall be satisfied
|
| 691 |
[[temp.constr.constr]].
|
| 692 |
|
| 693 |
[*Example 5*:
|
| 694 |
|
|
|
|
| 729 |
name a template specialization. A concept-id evaluates to `true` if the
|
| 730 |
concept’s normalized *constraint-expression* [[temp.constr.decl]] is
|
| 731 |
satisfied [[temp.constr.constr]] by the specified template arguments and
|
| 732 |
`false` otherwise.
|
| 733 |
|
| 734 |
+
[*Note 5*: Since a *constraint-expression* is an unevaluated operand, a
|
| 735 |
concept-id appearing in a *constraint-expression* is not evaluated
|
| 736 |
except as necessary to determine whether the normalized constraints are
|
| 737 |
satisfied. — *end note*]
|
| 738 |
|
| 739 |
[*Example 6*:
|
|
|
|
| 745 |
|
| 746 |
— *end example*]
|
| 747 |
|
| 748 |
## Template arguments <a id="temp.arg">[[temp.arg]]</a>
|
| 749 |
|
| 750 |
+
### General <a id="temp.arg.general">[[temp.arg.general]]</a>
|
| 751 |
+
|
| 752 |
There are three forms of *template-argument*, corresponding to the three
|
| 753 |
forms of *template-parameter*: type, non-type and template. The type and
|
| 754 |
form of each *template-argument* specified in a *template-id* shall
|
| 755 |
match the type and form specified for the corresponding parameter
|
| 756 |
declared by the template in its *template-parameter-list*. When the
|
|
|
|
| 781 |
}
|
| 782 |
```
|
| 783 |
|
| 784 |
— *end example*]
|
| 785 |
|
| 786 |
+
The template argument list of a *template-head* is a template argument
|
| 787 |
+
list in which the nᵗʰ template argument has the value of the nᵗʰ
|
| 788 |
+
template parameter of the *template-head*. If the nᵗʰ template parameter
|
| 789 |
+
is a template parameter pack [[temp.variadic]], the nᵗʰ template
|
| 790 |
+
argument is a pack expansion whose pattern is the name of the template
|
| 791 |
+
parameter pack.
|
| 792 |
+
|
| 793 |
In a *template-argument*, an ambiguity between a *type-id* and an
|
| 794 |
expression is resolved to a *type-id*, regardless of the form of the
|
| 795 |
corresponding *template-parameter*.[^3]
|
| 796 |
|
| 797 |
[*Example 2*:
|
|
|
|
| 805 |
}
|
| 806 |
```
|
| 807 |
|
| 808 |
— *end example*]
|
| 809 |
|
| 810 |
+
[*Note 1*: Names used in a *template-argument* are subject to access
|
| 811 |
+
control where they appear. Because a *template-parameter* is not a class
|
| 812 |
+
member, no access control applies. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
[*Example 3*:
|
| 815 |
|
| 816 |
``` cpp
|
| 817 |
template<class T> class X {
|
|
|
|
| 819 |
};
|
| 820 |
|
| 821 |
class Y {
|
| 822 |
private:
|
| 823 |
struct S { ... };
|
| 824 |
+
X<S> x; // OK, S is accessible
|
| 825 |
// X<Y::S> has a static member of type Y::S
|
| 826 |
+
// OK, even though Y::S is private
|
| 827 |
};
|
| 828 |
|
| 829 |
X<Y::S> y; // error: S not accessible
|
| 830 |
```
|
| 831 |
|
|
|
|
| 858 |
|
| 859 |
[*Example 5*:
|
| 860 |
|
| 861 |
``` cpp
|
| 862 |
template<class T = char> class String;
|
| 863 |
+
String<>* p; // OK, String<char>
|
| 864 |
String* q; // syntax error
|
| 865 |
template<class ... Elements> class Tuple;
|
| 866 |
+
Tuple<>* t; // OK, Elements is empty
|
| 867 |
Tuple* u; // syntax error
|
| 868 |
```
|
| 869 |
|
| 870 |
— *end example*]
|
| 871 |
|
|
|
|
| 878 |
``` cpp
|
| 879 |
template<class T> struct A {
|
| 880 |
~A();
|
| 881 |
};
|
| 882 |
void f(A<int>* p, A<int>* q) {
|
| 883 |
+
p->A<int>::~A(); // OK, destructor call
|
| 884 |
+
q->A<int>::~A<int>(); // OK, destructor call
|
| 885 |
}
|
| 886 |
```
|
| 887 |
|
| 888 |
— *end example*]
|
| 889 |
|
| 890 |
If the use of a *template-argument* gives rise to an ill-formed
|
| 891 |
construct in the instantiation of a template specialization, the program
|
| 892 |
is ill-formed.
|
| 893 |
|
| 894 |
+
When name lookup for the component name of a *template-id* finds an
|
| 895 |
+
overload set, both non-template functions in the overload set and
|
| 896 |
+
function templates in the overload set for which the
|
| 897 |
+
*template-argument*s do not match the *template-parameter*s are ignored.
|
| 898 |
+
|
| 899 |
+
[*Note 2*: If none of the function templates have matching
|
| 900 |
+
*template-parameter*s, the program is ill-formed. — *end note*]
|
| 901 |
|
| 902 |
When a *simple-template-id* does not name a function, a default
|
| 903 |
*template-argument* is implicitly instantiated [[temp.inst]] when the
|
| 904 |
value of that default argument is needed.
|
| 905 |
|
|
|
|
| 944 |
}
|
| 945 |
```
|
| 946 |
|
| 947 |
— *end example*]
|
| 948 |
|
| 949 |
+
[*Note 1*: A template type argument can be an incomplete type
|
| 950 |
+
[[term.incomplete.type]]. — *end note*]
|
| 951 |
|
| 952 |
### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
|
| 953 |
|
| 954 |
If the type `T` of a *template-parameter* [[temp.param]] contains a
|
| 955 |
placeholder type [[dcl.spec.auto]] or a placeholder for a deduced class
|
|
|
|
| 1071 |
|
| 1072 |
### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
|
| 1073 |
|
| 1074 |
A *template-argument* for a template *template-parameter* shall be the
|
| 1075 |
name of a class template or an alias template, expressed as
|
| 1076 |
+
*id-expression*. Only primary templates are considered when matching the
|
| 1077 |
+
template template argument with the corresponding parameter; partial
|
|
|
|
| 1078 |
specializations are not considered even if their parameter lists match
|
| 1079 |
that of the template template parameter.
|
| 1080 |
|
| 1081 |
+
Any partial specializations [[temp.spec.partial]] associated with the
|
| 1082 |
+
primary template are considered when a specialization based on the
|
| 1083 |
+
template *template-parameter* is instantiated. If a specialization is
|
| 1084 |
+
not reachable from the point of instantiation, and it would have been
|
| 1085 |
+
selected had it been reachable, the program is ill-formed, no diagnostic
|
| 1086 |
+
required.
|
| 1087 |
|
| 1088 |
[*Example 1*:
|
| 1089 |
|
| 1090 |
``` cpp
|
| 1091 |
template<class T> class A { // primary template
|
|
|
|
| 1155 |
template <class T1, class T2> struct B;
|
| 1156 |
template <int N> struct C;
|
| 1157 |
template <class T1, int N> struct D;
|
| 1158 |
template <class T1, class T2, int N = 17> struct E;
|
| 1159 |
|
| 1160 |
+
eval<A<int>> eA; // OK, matches partial specialization of eval
|
| 1161 |
+
eval<B<int, float>> eB; // OK, matches partial specialization of eval
|
| 1162 |
eval<C<17>> eC; // error: C does not match TT in partial specialization
|
| 1163 |
eval<D<int, 17>> eD; // error: D does not match TT in partial specialization
|
| 1164 |
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
|
| 1165 |
```
|
| 1166 |
|
|
|
|
| 1208 |
If the rewrite produces an invalid type, then `P` is not at least as
|
| 1209 |
specialized as `A`.
|
| 1210 |
|
| 1211 |
## Template constraints <a id="temp.constr">[[temp.constr]]</a>
|
| 1212 |
|
| 1213 |
+
### General <a id="temp.constr.general">[[temp.constr.general]]</a>
|
| 1214 |
+
|
| 1215 |
+
[*Note 1*: Subclause [[temp.constr]] defines the meaning of constraints
|
| 1216 |
+
on template arguments. The abstract syntax and satisfaction rules are
|
| 1217 |
defined in [[temp.constr.constr]]. Constraints are associated with
|
| 1218 |
declarations in [[temp.constr.decl]]. Declarations are partially ordered
|
| 1219 |
by their associated constraints [[temp.constr.order]]. — *end note*]
|
| 1220 |
|
| 1221 |
### Constraints <a id="temp.constr.constr">[[temp.constr.constr]]</a>
|
| 1222 |
|
| 1223 |
+
#### General <a id="temp.constr.constr.general">[[temp.constr.constr.general]]</a>
|
| 1224 |
+
|
| 1225 |
A *constraint* is a sequence of logical operations and operands that
|
| 1226 |
specifies requirements on template arguments. The operands of a logical
|
| 1227 |
operation are constraints. There are three different kinds of
|
| 1228 |
constraints:
|
| 1229 |
|
|
|
|
| 1274 |
requires (sizeof(T) > 1) && (get_value<T>())
|
| 1275 |
void f(T); // has associated constraint sizeof(T) > 1 ∧ get_value<T>()
|
| 1276 |
|
| 1277 |
void f(int);
|
| 1278 |
|
| 1279 |
+
f('a'); // OK, calls f(int)
|
| 1280 |
```
|
| 1281 |
|
| 1282 |
In the satisfaction of the associated constraints [[temp.constr.decl]]
|
| 1283 |
of `f`, the constraint `sizeof(char) > 1` is not satisfied; the second
|
| 1284 |
operand is not checked for satisfaction.
|
|
|
|
| 1385 |
}
|
| 1386 |
```
|
| 1387 |
|
| 1388 |
— *end example*]
|
| 1389 |
|
| 1390 |
+
As specified in [[temp.over.link]], if the validity or meaning of the
|
| 1391 |
+
program depends on whether two constructs are equivalent, and they are
|
| 1392 |
+
functionally equivalent but not equivalent, the program is ill-formed,
|
| 1393 |
+
no diagnostic required.
|
| 1394 |
|
| 1395 |
[*Example 2*:
|
| 1396 |
|
| 1397 |
``` cpp
|
| 1398 |
template <unsigned N> void f2()
|
|
|
|
| 1593 |
|
| 1594 |
``` cpp
|
| 1595 |
template<typename T> concept C1 = sizeof(T) == 1;
|
| 1596 |
template<typename T> concept C2 = C1<T> && 1 == 2;
|
| 1597 |
template<typename T> concept C3 = requires { typename T::type; };
|
| 1598 |
+
template<typename T> concept C4 = requires (T x) { ++x; };
|
| 1599 |
|
| 1600 |
template<C2 U> void f1(U); // #1
|
| 1601 |
template<C3 U> void f2(U); // #2
|
| 1602 |
template<C4 U> void f3(U); // #3
|
| 1603 |
```
|
|
|
|
| 1612 |
— *end example*]
|
| 1613 |
|
| 1614 |
### Partial ordering by constraints <a id="temp.constr.order">[[temp.constr.order]]</a>
|
| 1615 |
|
| 1616 |
A constraint P *subsumes* a constraint Q if and only if, for every
|
| 1617 |
+
disjunctive clause Pᵢ in the disjunctive normal form[^4]
|
| 1618 |
+
|
| 1619 |
+
of P, Pᵢ subsumes every conjunctive clause Qⱼ in the conjunctive normal
|
| 1620 |
+
form[^5]
|
| 1621 |
+
|
| 1622 |
of Q, where
|
| 1623 |
|
| 1624 |
- a disjunctive clause Pᵢ subsumes a conjunctive clause Qⱼ if and only
|
| 1625 |
if there exists an atomic constraint Pᵢₐ in Pᵢ for which there exists
|
| 1626 |
an atomic constraint $Q_{jb}$ in Qⱼ such that Pᵢₐ subsumes $Q_{jb}$,
|
|
|
|
| 1642 |
- the best viable candidate of non-template functions
|
| 1643 |
[[over.match.best]],
|
| 1644 |
- the address of a non-template function [[over.over]],
|
| 1645 |
- the matching of template template arguments [[temp.arg.template]],
|
| 1646 |
- the partial ordering of class template specializations
|
| 1647 |
+
[[temp.spec.partial.order]], and
|
| 1648 |
- the partial ordering of function templates [[temp.func.order]].
|
| 1649 |
|
| 1650 |
— *end note*]
|
| 1651 |
|
| 1652 |
A declaration `D1` is *at least as constrained* as a declaration `D2` if
|
|
|
|
| 1750 |
If an expression e is type-dependent [[temp.dep.expr]], `decltype(e)`
|
| 1751 |
denotes a unique dependent type. Two such *decltype-specifier*s refer to
|
| 1752 |
the same type only if their *expression*s are equivalent
|
| 1753 |
[[temp.over.link]].
|
| 1754 |
|
| 1755 |
+
[*Note 1*: However, such a type might be aliased, e.g., by a
|
| 1756 |
*typedef-name*. — *end note*]
|
| 1757 |
|
| 1758 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 1759 |
|
| 1760 |
+
### General <a id="temp.decls.general">[[temp.decls.general]]</a>
|
| 1761 |
+
|
| 1762 |
+
The template parameters of a template are specified in the angle bracket
|
| 1763 |
+
enclosed list that immediately follows the keyword `template`.
|
| 1764 |
+
|
| 1765 |
+
A *primary template* declaration is one in which the name of the
|
| 1766 |
+
template is not followed by a *template-argument-list*. The template
|
| 1767 |
+
argument list of a primary template is the template argument list of its
|
| 1768 |
+
*template-head* [[temp.arg]]. A template declaration in which the name
|
| 1769 |
+
of the template is followed by a *template-argument-list* is a partial
|
| 1770 |
+
specialization [[temp.spec.partial]] of the template named in the
|
| 1771 |
+
declaration, which shall be a class or variable template.
|
|
|
|
|
|
|
|
|
|
| 1772 |
|
| 1773 |
For purposes of name lookup and instantiation, default arguments,
|
| 1774 |
*type-constraint*s, *requires-clause*s [[temp.pre]], and
|
| 1775 |
*noexcept-specifier*s of function templates and of member functions of
|
| 1776 |
class templates are considered definitions; each default argument,
|
| 1777 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 1778 |
separate definition which is unrelated to the templated function
|
| 1779 |
+
definition or to any other default arguments, *type-constraint*s,
|
| 1780 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 1781 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 1782 |
are considered definitions.
|
| 1783 |
|
| 1784 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 1785 |
possible to partially or explicitly specialize an alias template.
|
| 1786 |
|
| 1787 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 1788 |
|
| 1789 |
+
#### General <a id="temp.class.general">[[temp.class.general]]</a>
|
| 1790 |
+
|
| 1791 |
A *class template* defines the layout and operations for an unbounded
|
| 1792 |
set of related types.
|
| 1793 |
|
| 1794 |
[*Example 1*:
|
| 1795 |
|
| 1796 |
+
It is possible for a single class template `List` to provide an
|
| 1797 |
+
unbounded set of class definitions: one class `List<T>` for every type
|
| 1798 |
+
`T`, each describing a linked list of elements of type `T`. Similarly, a
|
| 1799 |
+
class template `Array` describing a contiguous, dynamic array can be
|
| 1800 |
+
defined like this:
|
| 1801 |
|
| 1802 |
``` cpp
|
| 1803 |
template<class T> class Array {
|
| 1804 |
T* v;
|
| 1805 |
int sz;
|
|
|
|
| 1809 |
T& elem(int i) { return v[i]; }
|
| 1810 |
};
|
| 1811 |
```
|
| 1812 |
|
| 1813 |
The prefix `template<class T>` specifies that a template is being
|
| 1814 |
+
declared and that a *type-name* `T` can be used in the declaration. In
|
| 1815 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 1816 |
|
| 1817 |
— *end example*]
|
| 1818 |
|
| 1819 |
+
[*Note 1*:
|
| 1820 |
+
|
| 1821 |
+
When a member of a class template is defined outside of the class
|
| 1822 |
+
template definition, the member definition is defined as a template
|
| 1823 |
+
definition with the *template-head* equivalent to that of the class
|
| 1824 |
+
template. The names of the template parameters used in the definition of
|
| 1825 |
+
the member can differ from the template parameter names used in the
|
| 1826 |
+
class template definition. The class template name in the member
|
| 1827 |
+
definition is followed by the template argument list of the
|
| 1828 |
+
*template-head* [[temp.arg]].
|
|
|
|
| 1829 |
|
| 1830 |
[*Example 2*:
|
| 1831 |
|
| 1832 |
``` cpp
|
| 1833 |
template<class T1, class T2> struct A {
|
|
|
|
| 1858 |
void g();
|
| 1859 |
void h();
|
| 1860 |
template<D U> struct Inner;
|
| 1861 |
};
|
| 1862 |
|
| 1863 |
+
template<C A> void S<A>::f() { } // OK, template-head{s} match
|
| 1864 |
template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
|
| 1865 |
|
| 1866 |
template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
|
| 1867 |
void S<T>::h() { } // functionally equivalent but not equivalent
|
| 1868 |
|
|
|
|
| 1870 |
struct S<X>::Inner { }; // OK
|
| 1871 |
```
|
| 1872 |
|
| 1873 |
— *end example*]
|
| 1874 |
|
| 1875 |
+
— *end note*]
|
| 1876 |
+
|
| 1877 |
+
In a partial specialization, explicit specialization or explicit
|
| 1878 |
+
instantiation of a class template, the *class-key* shall agree in kind
|
| 1879 |
+
with the original class template declaration [[dcl.type.elab]].
|
| 1880 |
|
| 1881 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 1882 |
|
| 1883 |
A member function of a class template may be defined outside of the
|
| 1884 |
class template definition in which it is declared.
|
|
|
|
| 1895 |
T& elem(int i) { return v[i]; }
|
| 1896 |
};
|
| 1897 |
```
|
| 1898 |
|
| 1899 |
declares three member functions of a class template. The subscript
|
| 1900 |
+
function can be defined like this:
|
| 1901 |
|
| 1902 |
``` cpp
|
| 1903 |
template<class T> T& Array<T>::operator[](int i) {
|
| 1904 |
if (i<0 || sz<=i) error("Array: range error");
|
| 1905 |
return v[i];
|
|
|
|
| 1948 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 1949 |
|
| 1950 |
Deduction guides are used when a *template-name* appears as a type
|
| 1951 |
specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
|
| 1952 |
guides are not found by name lookup. Instead, when performing class
|
| 1953 |
+
template argument deduction [[over.match.class.deduct]], all reachable
|
| 1954 |
+
deduction guides declared for the class template are considered.
|
| 1955 |
|
| 1956 |
``` bnf
|
| 1957 |
deduction-guide:
|
| 1958 |
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 1959 |
```
|
|
|
|
| 1979 |
|
| 1980 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 1981 |
deduction guide as in a function declaration [[dcl.fct]]. The
|
| 1982 |
*simple-template-id* shall name a class template specialization. The
|
| 1983 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 1984 |
+
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 1985 |
+
which the corresponding class template belongs and, for a member class
|
| 1986 |
+
template, have the same access. Two deduction guide declarations for the
|
| 1987 |
+
same class template shall not have equivalent
|
| 1988 |
+
*parameter-declaration-clause*s if either is reachable from the other.
|
| 1989 |
|
| 1990 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 1991 |
|
| 1992 |
A member class of a class template may be defined outside the class
|
| 1993 |
template definition in which it is declared.
|
|
|
|
| 1999 |
|
| 2000 |
``` cpp
|
| 2001 |
template<class T> struct A {
|
| 2002 |
class B;
|
| 2003 |
};
|
| 2004 |
+
A<int>::B* b1; // OK, requires A to be defined but not A::B
|
| 2005 |
template<class T> class A<T>::B { };
|
| 2006 |
+
A<int>::B b2; // OK, requires A::B to be defined
|
| 2007 |
```
|
| 2008 |
|
| 2009 |
— *end note*]
|
| 2010 |
|
| 2011 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
|
|
|
| 2041 |
``` cpp
|
| 2042 |
template <class T> struct A {
|
| 2043 |
static int i[];
|
| 2044 |
};
|
| 2045 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 2046 |
+
template <> int A<int>::i[] = { 1 }; // OK, 1 element
|
| 2047 |
```
|
| 2048 |
|
| 2049 |
— *end example*]
|
| 2050 |
|
| 2051 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
|
|
|
| 2137 |
}
|
| 2138 |
```
|
| 2139 |
|
| 2140 |
— *end example*]
|
| 2141 |
|
| 2142 |
+
A member function template shall not be declared `virtual`.
|
| 2143 |
|
| 2144 |
[*Example 4*:
|
| 2145 |
|
| 2146 |
``` cpp
|
| 2147 |
template <class T> struct AA {
|
|
|
|
| 2162 |
virtual void f(int);
|
| 2163 |
};
|
| 2164 |
|
| 2165 |
class D : public B {
|
| 2166 |
template <class T> void f(T); // does not override B::f(int)
|
| 2167 |
+
void f(int i) { f<>(i); } // overriding function that calls the function template specialization
|
| 2168 |
};
|
| 2169 |
```
|
| 2170 |
|
| 2171 |
— *end example*]
|
| 2172 |
|
| 2173 |
+
[*Note 1*:
|
| 2174 |
+
|
| 2175 |
A specialization of a conversion function template is referenced in the
|
| 2176 |
same way as a non-template conversion function that converts to the same
|
| 2177 |
+
type [[class.conv.fct]].
|
| 2178 |
|
| 2179 |
[*Example 6*:
|
| 2180 |
|
| 2181 |
``` cpp
|
| 2182 |
struct A {
|
|
|
|
| 2193 |
}
|
| 2194 |
```
|
| 2195 |
|
| 2196 |
— *end example*]
|
| 2197 |
|
| 2198 |
+
There is no syntax to form a *template-id* [[temp.names]] by providing
|
| 2199 |
+
an explicit template argument list [[temp.arg.explicit]] for a
|
| 2200 |
+
conversion function template.
|
| 2201 |
|
| 2202 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2203 |
|
| 2204 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 2205 |
|
| 2206 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 2207 |
more template arguments.
|
|
|
|
| 2330 |
typedef Tuple<Pair<Args1, Args2> ... > type;
|
| 2331 |
};
|
| 2332 |
};
|
| 2333 |
|
| 2334 |
typedef zip<short, int>::with<unsigned short, unsigned>::type T1;
|
| 2335 |
+
// T1 is Tuple<Pair<short, unsigned short>, Pair<int, unsigned>>
|
| 2336 |
typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
| 2337 |
// error: different number of arguments specified for Args1 and Args2
|
| 2338 |
|
| 2339 |
template<class ... Args>
|
| 2340 |
+
void g(Args ... args) { // OK, Args is expanded by the function parameter pack args
|
| 2341 |
+
f(const_cast<const Args*>(&args)...); // OK, ``Args'' and ``args'' are expanded
|
| 2342 |
f(5 ...); // error: pattern does not contain any packs
|
| 2343 |
f(args); // error: pack ``args'' is not expanded
|
| 2344 |
+
f(h(args ...) + args ...); // OK, first ``args'' expanded within h,
|
| 2345 |
// second ``args'' expanded within f
|
| 2346 |
}
|
| 2347 |
```
|
| 2348 |
|
| 2349 |
— *end example*]
|
| 2350 |
|
| 2351 |
+
The instantiation of a pack expansion considers items
|
| 2352 |
+
`E₁`, `E₂`, …, `E_N`, where N is the number of elements in the pack
|
| 2353 |
+
expansion parameters. Each `Eᵢ` is generated by instantiating the
|
| 2354 |
+
pattern and replacing each pack expansion parameter with its iᵗʰ
|
| 2355 |
+
element. Such an element, in the context of the instantiation, is
|
| 2356 |
+
interpreted as follows:
|
|
|
|
| 2357 |
|
| 2358 |
+
- if the pack is a template parameter pack, the element is an
|
| 2359 |
+
*id-expression* (for a non-type template parameter pack), a
|
| 2360 |
+
*typedef-name* (for a type template parameter pack declared without
|
| 2361 |
+
`template`), or a *template-name* (for a type template parameter pack
|
| 2362 |
+
declared with `template`), designating the iᵗʰ corresponding type or
|
| 2363 |
+
value template argument;
|
| 2364 |
- if the pack is a function parameter pack, the element is an
|
| 2365 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 2366 |
from instantiation of the function parameter pack declaration;
|
| 2367 |
otherwise
|
| 2368 |
- if the pack is an *init-capture* pack, the element is an
|
| 2369 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 2370 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 2371 |
+
pack declaration.
|
| 2372 |
|
| 2373 |
+
When N is zero, the instantiation of a pack expansion does not alter the
|
| 2374 |
+
syntactic interpretation of the enclosing construct, even in cases where
|
| 2375 |
+
omitting the pack expansion entirely would otherwise be ill-formed or
|
| 2376 |
+
would result in an ambiguity in the grammar.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2377 |
|
| 2378 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 2379 |
+
an integral constant with value N.
|
|
|
|
| 2380 |
|
| 2381 |
+
The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
| 2382 |
|
| 2383 |
+
- `(` `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for a unary
|
| 2384 |
+
left fold,
|
| 2385 |
+
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))`
|
| 2386 |
+
`)` for a unary right fold,
|
| 2387 |
+
- `(` `(((`E *op* E₁`)` *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for
|
| 2388 |
+
a binary left fold, and
|
| 2389 |
+
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* `(`$\mathtt{E}_{N}$
|
| 2390 |
+
*op* E`)))` `)` for a binary right fold.
|
| 2391 |
|
| 2392 |
+
In each case, *op* is the *fold-operator*. For a binary fold, E is
|
| 2393 |
+
generated by instantiating the *cast-expression* that did not contain an
|
| 2394 |
+
unexpanded pack.
|
|
|
|
|
|
|
|
|
|
| 2395 |
|
| 2396 |
+
[*Example 6*:
|
| 2397 |
|
| 2398 |
``` cpp
|
| 2399 |
template<typename ...Args>
|
| 2400 |
bool all(Args ...args) { return (... && args); }
|
| 2401 |
|
|
|
|
| 2405 |
Within the instantiation of `all`, the returned expression expands to
|
| 2406 |
`((true && true) && true) && false`, which evaluates to `false`.
|
| 2407 |
|
| 2408 |
— *end example*]
|
| 2409 |
|
| 2410 |
+
If N is zero for a unary fold, the value of the expression is shown in
|
| 2411 |
+
[[temp.fold.empty]]; if the operator is not listed in
|
| 2412 |
[[temp.fold.empty]], the instantiation is ill-formed.
|
| 2413 |
|
| 2414 |
**Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
|
| 2415 |
|
| 2416 |
| Operator | Value when pack is empty |
|
|
|
|
| 2418 |
| `&&` | `true` |
|
| 2419 |
| `||` | `false` |
|
| 2420 |
| `,` | `void()` |
|
| 2421 |
|
| 2422 |
|
| 2423 |
+
The instantiation of any other pack expansion produces a list of
|
| 2424 |
+
elements `E₁`, `E₂`, …, `E_N`.
|
| 2425 |
+
|
| 2426 |
+
[*Note 1*: The variety of list varies with the context:
|
| 2427 |
+
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 2428 |
+
etc. — *end note*]
|
| 2429 |
+
|
| 2430 |
+
When N is zero, the instantiation of the expansion produces an empty
|
| 2431 |
+
list.
|
| 2432 |
+
|
| 2433 |
+
[*Example 7*:
|
| 2434 |
+
|
| 2435 |
+
``` cpp
|
| 2436 |
+
template<class... T> struct X : T... { };
|
| 2437 |
+
template<class... T> void f(T... values) {
|
| 2438 |
+
X<T...> x(values...);
|
| 2439 |
+
}
|
| 2440 |
+
|
| 2441 |
+
template void f<>(); // OK, X<> has no base classes
|
| 2442 |
+
// x is a variable of type X<> that is value-initialized
|
| 2443 |
+
```
|
| 2444 |
+
|
| 2445 |
+
— *end example*]
|
| 2446 |
+
|
| 2447 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 2448 |
|
| 2449 |
A friend of a class or class template can be a function template or
|
| 2450 |
class template, a specialization of a function template or class
|
| 2451 |
+
template, or a non-template function or class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2452 |
|
| 2453 |
[*Example 1*:
|
| 2454 |
|
| 2455 |
``` cpp
|
| 2456 |
template<class T> class task;
|
|
|
|
| 2480 |
template has the class template specialization `task<int>` as a friend,
|
| 2481 |
and has all specializations of the class template `frd` as friends.
|
| 2482 |
|
| 2483 |
— *end example*]
|
| 2484 |
|
| 2485 |
+
Friend classes, class templates, functions, or function templates can be
|
| 2486 |
+
declared within a class template. When a template is instantiated, its
|
| 2487 |
+
friend declarations are found by name lookup as if the specialization
|
| 2488 |
+
had been explicitly declared at its point of instantiation.
|
| 2489 |
+
|
| 2490 |
+
[*Note 1*: They can introduce entities that belong to an enclosing
|
| 2491 |
+
namespace scope [[dcl.meaning]], in which case they are attached to the
|
| 2492 |
+
same module as the class template [[module.unit]]. — *end note*]
|
| 2493 |
+
|
| 2494 |
A friend template may be declared within a class or class template. A
|
| 2495 |
friend function template may be defined within a class or class
|
| 2496 |
template, but a friend class template may not be defined in a class or
|
| 2497 |
class template. In these cases, all specializations of the friend class
|
| 2498 |
or friend function template are friends of the class or class template
|
|
|
|
| 2509 |
|
| 2510 |
— *end example*]
|
| 2511 |
|
| 2512 |
A template friend declaration specifies that all specializations of that
|
| 2513 |
template, whether they are implicitly instantiated [[temp.inst]],
|
| 2514 |
+
partially specialized [[temp.spec.partial]] or explicitly specialized
|
| 2515 |
[[temp.expl.spec]], are friends of the class containing the template
|
| 2516 |
friend declaration.
|
| 2517 |
|
| 2518 |
[*Example 3*:
|
| 2519 |
|
|
|
|
| 2532 |
A template friend declaration may declare a member of a dependent type
|
| 2533 |
to be a friend. The friend declaration shall declare a function or
|
| 2534 |
specify a type with an *elaborated-type-specifier*, in either case with
|
| 2535 |
a *nested-name-specifier* ending with a *simple-template-id*, *C*, whose
|
| 2536 |
*template-name* names a class template. The template parameters of the
|
| 2537 |
+
template friend declaration shall be deducible from *C*
|
| 2538 |
+
[[temp.deduct.type]]. In this case, a member of a specialization *S* of
|
| 2539 |
the class template is a friend of the class granting friendship if
|
| 2540 |
deduction of the template parameters of *C* from *S* succeeds, and
|
| 2541 |
substituting the deduced template arguments into the friend declaration
|
| 2542 |
+
produces a declaration that corresponds to the member of the
|
| 2543 |
+
specialization.
|
| 2544 |
|
| 2545 |
[*Example 4*:
|
| 2546 |
|
| 2547 |
``` cpp
|
| 2548 |
template<class T> struct A {
|
|
|
|
| 2578 |
}; // of those function templates
|
| 2579 |
```
|
| 2580 |
|
| 2581 |
— *end example*]
|
| 2582 |
|
|
|
|
|
|
|
|
|
|
| 2583 |
A friend template shall not be declared in a local class.
|
| 2584 |
|
| 2585 |
Friend declarations shall not declare partial specializations.
|
| 2586 |
|
| 2587 |
[*Example 5*:
|
|
|
|
| 2605 |
a template parameter from an enclosing template shall be a definition.
|
| 2606 |
Such a constrained friend function or function template declaration does
|
| 2607 |
not declare the same function or function template as a declaration in
|
| 2608 |
any other scope.
|
| 2609 |
|
| 2610 |
+
### Partial specialization <a id="temp.spec.partial">[[temp.spec.partial]]</a>
|
| 2611 |
|
| 2612 |
+
#### General <a id="temp.spec.partial.general">[[temp.spec.partial.general]]</a>
|
| 2613 |
+
|
| 2614 |
+
A partial specialization of a template provides an alternative
|
|
|
|
|
|
|
| 2615 |
definition of the template that is used instead of the primary
|
| 2616 |
definition when the arguments in a specialization match those given in
|
| 2617 |
+
the partial specialization [[temp.spec.partial.match]]. A declaration of
|
| 2618 |
+
the primary template shall precede any partial specialization of that
|
| 2619 |
+
template. A partial specialization shall be reachable from any use of a
|
| 2620 |
+
template specialization that would make use of the partial
|
| 2621 |
+
specialization as the result of an implicit or explicit instantiation;
|
| 2622 |
+
no diagnostic is required.
|
|
|
|
| 2623 |
|
| 2624 |
+
Two partial specialization declarations declare the same entity if they
|
| 2625 |
+
are partial specializations of the same template and have equivalent
|
| 2626 |
+
*template-head*s and template argument lists [[temp.over.link]]. Each
|
| 2627 |
+
partial specialization is a distinct template.
|
| 2628 |
|
| 2629 |
[*Example 1*:
|
| 2630 |
|
| 2631 |
``` cpp
|
| 2632 |
template<class T1, class T2, int I> class A { };
|
|
|
|
| 2640 |
template. The second and subsequent declarations declare partial
|
| 2641 |
specializations of the primary template.
|
| 2642 |
|
| 2643 |
— *end example*]
|
| 2644 |
|
| 2645 |
+
A partial specialization may be constrained [[temp.constr]].
|
| 2646 |
|
| 2647 |
[*Example 2*:
|
| 2648 |
|
| 2649 |
``` cpp
|
| 2650 |
template<typename T> concept C = true;
|
|
|
|
| 2661 |
specialized because the template arguments are equivalent, but the
|
| 2662 |
partial specialization is more constrained [[temp.constr.order]].
|
| 2663 |
|
| 2664 |
— *end example*]
|
| 2665 |
|
| 2666 |
+
The template argument list of a partial specialization is the
|
| 2667 |
+
*template-argument-list* following the name of the template.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2668 |
|
| 2669 |
+
A partial specialization may be declared in any scope in which the
|
| 2670 |
+
corresponding primary template may be defined
|
| 2671 |
+
[[dcl.meaning]], [[class.mem]], [[temp.mem]].
|
| 2672 |
|
| 2673 |
+
[*Example 3*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2674 |
|
| 2675 |
``` cpp
|
| 2676 |
template<class T> struct A {
|
| 2677 |
struct C {
|
| 2678 |
template<class T2> struct B { };
|
|
|
|
| 2687 |
A<short>::C::B<int*> absip; // uses partial specialization #2
|
| 2688 |
```
|
| 2689 |
|
| 2690 |
— *end example*]
|
| 2691 |
|
| 2692 |
+
Partial specialization declarations do not introduce a name. Instead,
|
| 2693 |
+
when the primary template name is used, any reachable partial
|
| 2694 |
+
specializations of the primary template are also considered.
|
|
|
|
|
|
|
|
|
|
| 2695 |
|
| 2696 |
+
[*Note 1*: One consequence is that a *using-declaration* which refers
|
| 2697 |
+
to a class template does not restrict the set of partial specializations
|
| 2698 |
+
that are found through the *using-declaration*. — *end note*]
|
| 2699 |
+
|
| 2700 |
+
[*Example 4*:
|
| 2701 |
|
| 2702 |
``` cpp
|
| 2703 |
namespace N {
|
| 2704 |
template<class T1, class T2> class A { }; // primary template
|
| 2705 |
}
|
|
|
|
| 2717 |
— *end example*]
|
| 2718 |
|
| 2719 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 2720 |
parameter. All other non-type arguments are specialized.
|
| 2721 |
|
| 2722 |
+
Within the argument list of a partial specialization, the following
|
| 2723 |
+
restrictions apply:
|
| 2724 |
|
| 2725 |
- The type of a template parameter corresponding to a specialized
|
| 2726 |
+
non-type argument shall not be dependent on a parameter of the partial
|
| 2727 |
specialization.
|
| 2728 |
+
\[*Example 5*:
|
| 2729 |
``` cpp
|
| 2730 |
template <class T, T t> struct C {};
|
| 2731 |
template <class T> struct C<T, 1>; // error
|
| 2732 |
|
| 2733 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 2734 |
int array[5];
|
| 2735 |
template< int X > class A<X,&array> { }; // error
|
| 2736 |
```
|
| 2737 |
|
| 2738 |
— *end example*]
|
| 2739 |
+
- The partial specialization shall be more specialized than the primary
|
| 2740 |
+
template [[temp.spec.partial.order]].
|
| 2741 |
+
- The template parameter list of a partial specialization shall not
|
| 2742 |
+
contain default template argument values.[^8]
|
| 2743 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 2744 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 2745 |
template argument list.
|
| 2746 |
|
| 2747 |
The usual access checking rules do not apply to non-dependent names used
|
| 2748 |
to specify template arguments of the *simple-template-id* of the partial
|
| 2749 |
specialization.
|
| 2750 |
|
| 2751 |
+
[*Note 2*: The template arguments can be private types or objects that
|
| 2752 |
would normally not be accessible. Dependent names cannot be checked when
|
| 2753 |
declaring the partial specialization, but will be checked when
|
| 2754 |
substituting into the partial specialization. — *end note*]
|
| 2755 |
|
| 2756 |
+
#### Matching of partial specializations <a id="temp.spec.partial.match">[[temp.spec.partial.match]]</a>
|
| 2757 |
|
| 2758 |
+
When a template is used in a context that requires an instantiation of
|
| 2759 |
+
the template, it is necessary to determine whether the instantiation is
|
| 2760 |
+
to be generated using the primary template or one of the partial
|
| 2761 |
+
specializations. This is done by matching the template arguments of the
|
| 2762 |
+
template specialization with the template argument lists of the partial
|
| 2763 |
+
specializations.
|
| 2764 |
|
| 2765 |
+
- If exactly one matching partial specialization is found, the
|
| 2766 |
+
instantiation is generated from that partial specialization.
|
| 2767 |
+
- If more than one matching partial specialization is found, the partial
|
| 2768 |
+
order rules [[temp.spec.partial.order]] are used to determine whether
|
| 2769 |
+
one of the partial specializations is more specialized than the
|
| 2770 |
+
others. If such a partial specialization exists, the instantiation is
|
| 2771 |
+
generated from that partial specialization; otherwise, the use of the
|
| 2772 |
+
template is ambiguous and the program is ill-formed.
|
| 2773 |
- If no matches are found, the instantiation is generated from the
|
| 2774 |
primary template.
|
| 2775 |
|
| 2776 |
A partial specialization matches a given actual template argument list
|
| 2777 |
if the template arguments of the partial specialization can be deduced
|
|
|
|
| 2829 |
template <int I> struct B<I, I*2, 2> {}; // OK
|
| 2830 |
```
|
| 2831 |
|
| 2832 |
— *end example*]
|
| 2833 |
|
| 2834 |
+
In a name that refers to a specialization of a class or variable
|
| 2835 |
+
template (e.g., `A<int, int, 1>`), the argument list shall match the
|
| 2836 |
+
template parameter list of the primary template. The template arguments
|
| 2837 |
+
of a partial specialization are deduced from the arguments of the
|
| 2838 |
+
primary template.
|
| 2839 |
|
| 2840 |
+
#### Partial ordering of partial specializations <a id="temp.spec.partial.order">[[temp.spec.partial.order]]</a>
|
| 2841 |
|
| 2842 |
+
For two partial specializations, the first is *more specialized* than
|
| 2843 |
+
the second if, given the following rewrite to two function templates,
|
| 2844 |
+
the first function template is more specialized than the second
|
| 2845 |
+
according to the ordering rules for function templates
|
| 2846 |
[[temp.func.order]]:
|
| 2847 |
|
| 2848 |
- Each of the two function templates has the same template parameters
|
| 2849 |
and associated constraints [[temp.constr.decl]] as the corresponding
|
| 2850 |
partial specialization.
|
|
|
|
| 2898 |
The partial specialization \#2 is more specialized than \#1 because `B`
|
| 2899 |
is more specialized than `A`.
|
| 2900 |
|
| 2901 |
— *end example*]
|
| 2902 |
|
| 2903 |
+
#### Members of class template partial specializations <a id="temp.spec.partial.member">[[temp.spec.partial.member]]</a>
|
| 2904 |
|
| 2905 |
+
The members of the class template partial specialization are unrelated
|
| 2906 |
+
to the members of the primary template. Class template partial
|
| 2907 |
+
specialization members that are used in a way that requires a definition
|
| 2908 |
+
shall be defined; the definitions of members of the primary template are
|
| 2909 |
+
never used as definitions for members of a class template partial
|
| 2910 |
+
specialization. An explicit specialization of a member of a class
|
| 2911 |
+
template partial specialization is declared in the same way as an
|
| 2912 |
+
explicit specialization of a member of the primary template.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2913 |
|
| 2914 |
[*Example 1*:
|
| 2915 |
|
| 2916 |
``` cpp
|
| 2917 |
// primary class template
|
|
|
|
| 2948 |
— *end example*]
|
| 2949 |
|
| 2950 |
If a member template of a class template is partially specialized, the
|
| 2951 |
member template partial specializations are member templates of the
|
| 2952 |
enclosing class template; if the enclosing class template is
|
| 2953 |
+
instantiated [[temp.inst]], [[temp.explicit]], a declaration for every
|
| 2954 |
+
member template partial specialization is also instantiated as part of
|
| 2955 |
+
creating the members of the class template specialization. If the
|
| 2956 |
+
primary member template is explicitly specialized for a given (implicit)
|
| 2957 |
+
specialization of the enclosing class template, the partial
|
| 2958 |
specializations of the member template are ignored for this
|
| 2959 |
specialization of the enclosing class template. If a partial
|
| 2960 |
specialization of the member template is explicitly specialized for a
|
| 2961 |
given (implicit) specialization of the enclosing class template, the
|
| 2962 |
primary member template and its other partial specializations are still
|
|
|
|
| 2979 |
|
| 2980 |
— *end example*]
|
| 2981 |
|
| 2982 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 2983 |
|
| 2984 |
+
#### General <a id="temp.fct.general">[[temp.fct.general]]</a>
|
| 2985 |
+
|
| 2986 |
A function template defines an unbounded set of related functions.
|
| 2987 |
|
| 2988 |
[*Example 1*:
|
| 2989 |
|
| 2990 |
+
A family of sort functions can be declared like this:
|
| 2991 |
|
| 2992 |
``` cpp
|
| 2993 |
template<class T> class Array { };
|
| 2994 |
template<class T> void sort(Array<T>&);
|
| 2995 |
```
|
| 2996 |
|
| 2997 |
— *end example*]
|
| 2998 |
|
| 2999 |
+
[*Note 1*: A function template can have the same name as other function
|
| 3000 |
+
templates and non-template functions [[dcl.fct]] in the same
|
| 3001 |
+
scope. — *end note*]
|
| 3002 |
+
|
| 3003 |
+
A non-template function is not related to a function template (i.e., it
|
| 3004 |
+
is never considered to be a specialization), even if it has the same
|
| 3005 |
+
name and type as a potentially generated function template
|
| 3006 |
+
specialization.[^9]
|
| 3007 |
|
| 3008 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 3009 |
|
| 3010 |
It is possible to overload function templates so that two different
|
| 3011 |
function template specializations have the same type.
|
|
|
|
| 3040 |
the relationship between the template parameters and the rest of the
|
| 3041 |
signature.
|
| 3042 |
|
| 3043 |
[*Note 1*:
|
| 3044 |
|
| 3045 |
+
Two distinct function templates can have identical function return types
|
| 3046 |
and function parameter lists, even if overload resolution alone cannot
|
| 3047 |
distinguish them.
|
| 3048 |
|
| 3049 |
``` cpp
|
| 3050 |
template<class T> void f();
|
| 3051 |
+
template<int I> void f(); // OK, overloads the first template
|
| 3052 |
// distinguishable with an explicit template argument list
|
| 3053 |
```
|
| 3054 |
|
| 3055 |
— *end note*]
|
| 3056 |
|
|
|
|
| 3087 |
expression. Two unevaluated operands that do not involve template
|
| 3088 |
parameters are considered equivalent if two function definitions
|
| 3089 |
containing the expressions would satisfy the one-definition rule, except
|
| 3090 |
that the tokens used to name types and declarations may differ as long
|
| 3091 |
as they name the same entities, and the tokens used to form concept-ids
|
| 3092 |
+
[[temp.names]] may differ as long as the two *template-id*s are the same
|
| 3093 |
+
[[temp.type]].
|
| 3094 |
|
| 3095 |
[*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
|
| 3096 |
type. — *end note*]
|
| 3097 |
|
| 3098 |
Two *lambda-expression*s are never considered equivalent.
|
| 3099 |
|
| 3100 |
[*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
|
| 3101 |
signature of a function template with external linkage. — *end note*]
|
| 3102 |
|
| 3103 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 3104 |
+
only the name itself is considered, not the result of name lookup.
|
| 3105 |
+
|
| 3106 |
+
[*Note 5*: If such a dependent name is unqualified, it is looked up
|
| 3107 |
+
from the first declaration of the function template
|
| 3108 |
+
[[temp.dep.candidate]]. — *end note*]
|
| 3109 |
|
| 3110 |
[*Example 3*:
|
| 3111 |
|
| 3112 |
``` cpp
|
| 3113 |
template <int I, int J> void f(A<I+J>); // #1
|
|
|
|
| 3116 |
template <class T> decltype(g(T())) h();
|
| 3117 |
int g(int);
|
| 3118 |
template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup…
|
| 3119 |
{ return g(T()); } // …{} although the lookup here does find g(int)
|
| 3120 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 3121 |
+
// not considered at the first declaration of h()
|
| 3122 |
|
| 3123 |
// ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
|
| 3124 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 3125 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 3126 |
|
|
|
|
| 3137 |
same value. Two unevaluated operands that are not equivalent are
|
| 3138 |
functionally equivalent if, for any given set of template arguments, the
|
| 3139 |
expressions perform the same operations in the same order with the same
|
| 3140 |
entities.
|
| 3141 |
|
| 3142 |
+
[*Note 6*: For instance, one could have redundant
|
| 3143 |
parentheses. — *end note*]
|
| 3144 |
|
| 3145 |
+
[*Example 4*:
|
| 3146 |
+
|
| 3147 |
+
``` cpp
|
| 3148 |
+
template<int I> concept C = true;
|
| 3149 |
+
template<typename T> struct A {
|
| 3150 |
+
void f() requires C<42>; // #1
|
| 3151 |
+
void f() requires true; // OK, different functions
|
| 3152 |
+
};
|
| 3153 |
+
```
|
| 3154 |
+
|
| 3155 |
+
— *end example*]
|
| 3156 |
+
|
| 3157 |
Two *template-head*s are *equivalent* if their
|
| 3158 |
*template-parameter-list*s have the same length, corresponding
|
| 3159 |
*template-parameter*s are equivalent and are both declared with
|
| 3160 |
*type-constraint*s that are equivalent if either *template-parameter* is
|
| 3161 |
declared with a *type-constraint*, and if either *template-head* has a
|
|
|
|
| 3175 |
rules above are used to compare expressions involving template
|
| 3176 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 3177 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 3178 |
template argument lists.
|
| 3179 |
|
| 3180 |
+
If the validity or meaning of the program depends on whether two
|
| 3181 |
+
constructs are equivalent, and they are functionally equivalent but not
|
| 3182 |
+
equivalent, the program is ill-formed, no diagnostic required.
|
| 3183 |
+
Furthermore, if two function templates that do not correspond
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3184 |
|
| 3185 |
+
- have the same name,
|
| 3186 |
+
- have corresponding signatures [[basic.scope.scope]],
|
| 3187 |
+
- would declare the same entity [[basic.link]] considering them to
|
| 3188 |
+
correspond, and
|
| 3189 |
+
- accept and are satisfied by the same set of template argument lists,
|
| 3190 |
+
|
| 3191 |
+
the program is ill-formed, no diagnostic required.
|
| 3192 |
+
|
| 3193 |
+
[*Note 7*:
|
| 3194 |
|
| 3195 |
This rule guarantees that equivalent declarations will be linked with
|
| 3196 |
one another, while not requiring implementations to use heroic efforts
|
| 3197 |
to guarantee that functionally equivalent declarations will be treated
|
| 3198 |
as distinct. For example, the last two declarations are functionally
|
|
|
|
| 3214 |
|
| 3215 |
— *end note*]
|
| 3216 |
|
| 3217 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 3218 |
|
| 3219 |
+
If multiple function templates share a name, the use of that name can be
|
| 3220 |
+
ambiguous because template argument deduction [[temp.deduct]] may
|
| 3221 |
+
identify a specialization for more than one function template. *Partial
|
| 3222 |
+
ordering* of overloaded function template declarations is used in the
|
| 3223 |
+
following contexts to select the function template to which a function
|
| 3224 |
+
template specialization refers:
|
|
|
|
| 3225 |
|
| 3226 |
- during overload resolution for a call to a function template
|
| 3227 |
specialization [[over.match.best]];
|
| 3228 |
- when the address of a function template specialization is taken;
|
| 3229 |
- when a placement operator delete that is a function template
|
| 3230 |
+
specialization is selected to match a placement operator new
|
| 3231 |
+
[[basic.stc.dynamic.deallocation]], [[expr.new]];
|
| 3232 |
- when a friend function declaration [[temp.friend]], an explicit
|
| 3233 |
instantiation [[temp.explicit]] or an explicit specialization
|
| 3234 |
[[temp.expl.spec]] refers to a function template specialization.
|
| 3235 |
|
| 3236 |
Partial ordering selects which of two function templates is more
|
|
|
|
| 3380 |
template<class T > void f(T); // #2
|
| 3381 |
template<class T, class... U> void g(T*, U...); // #3
|
| 3382 |
template<class T > void g(T); // #4
|
| 3383 |
|
| 3384 |
void h(int i) {
|
| 3385 |
+
f(&i); // OK, calls #2
|
| 3386 |
+
g(&i); // OK, calls #3
|
| 3387 |
}
|
| 3388 |
```
|
| 3389 |
|
| 3390 |
— *end example*]
|
| 3391 |
|
|
|
|
| 3435 |
|
| 3436 |
void f(C auto &, auto &) = delete;
|
| 3437 |
template <C Q> void f(Q &, C auto &);
|
| 3438 |
|
| 3439 |
void g(struct A *ap, struct B *bp) {
|
| 3440 |
+
f(*ap, *bp); // OK, can use different methods to produce template parameters
|
| 3441 |
}
|
| 3442 |
|
| 3443 |
template <typename T, typename U> struct X {};
|
| 3444 |
|
| 3445 |
template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
|
| 3446 |
template <C T, C U, C V> bool operator==(T, X<U, V>);
|
| 3447 |
|
| 3448 |
void h() {
|
| 3449 |
+
X<void *, int>{} == 0; // OK, correspondence of [T, U, V] and [U, V, T]
|
| 3450 |
}
|
| 3451 |
```
|
| 3452 |
|
| 3453 |
— *end example*]
|
| 3454 |
|
|
|
|
| 3469 |
[*Example 1*:
|
| 3470 |
|
| 3471 |
``` cpp
|
| 3472 |
template<class T> struct Alloc { ... };
|
| 3473 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 3474 |
+
Vec<int> v; // same as vector<int, Alloc<int>> v;
|
| 3475 |
|
| 3476 |
template<class T>
|
| 3477 |
void process(Vec<T>& v)
|
| 3478 |
{ ... }
|
| 3479 |
|
|
|
|
| 3486 |
|
| 3487 |
f(v); // error: Vec not deduced
|
| 3488 |
|
| 3489 |
template<template<class,class> class TT>
|
| 3490 |
void g(TT<int, Alloc<int>>);
|
| 3491 |
+
g(v); // OK, TT = vector
|
| 3492 |
```
|
| 3493 |
|
| 3494 |
— *end example*]
|
| 3495 |
|
| 3496 |
However, if the *template-id* is dependent, subsequent template argument
|
|
|
|
| 3542 |
A *concept* is a template that defines constraints on its template
|
| 3543 |
arguments.
|
| 3544 |
|
| 3545 |
``` bnf
|
| 3546 |
concept-definition:
|
| 3547 |
+
concept concept-name attribute-specifier-seqₒₚₜ '=' constraint-expression ';'
|
| 3548 |
```
|
| 3549 |
|
| 3550 |
``` bnf
|
| 3551 |
concept-name:
|
| 3552 |
identifier
|
| 3553 |
```
|
| 3554 |
|
| 3555 |
A *concept-definition* declares a concept. Its *identifier* becomes a
|
| 3556 |
+
*concept-name* referring to that concept within its scope. The optional
|
| 3557 |
+
*attribute-specifier-seq* appertains to the concept.
|
| 3558 |
|
| 3559 |
[*Example 1*:
|
| 3560 |
|
| 3561 |
``` cpp
|
| 3562 |
template<typename T>
|
|
|
|
| 3572 |
T f2(T x) { return x; }
|
| 3573 |
```
|
| 3574 |
|
| 3575 |
— *end example*]
|
| 3576 |
|
| 3577 |
+
A *concept-definition* shall inhabit a namespace scope
|
| 3578 |
[[basic.scope.namespace]].
|
| 3579 |
|
| 3580 |
A concept shall not have associated constraints [[temp.constr.decl]].
|
| 3581 |
|
| 3582 |
A concept is not instantiated [[temp.spec]].
|
| 3583 |
|
| 3584 |
[*Note 1*: A concept-id [[temp.names]] is evaluated as an expression. A
|
| 3585 |
concept cannot be explicitly instantiated [[temp.explicit]], explicitly
|
| 3586 |
+
specialized [[temp.expl.spec]], or partially specialized
|
| 3587 |
+
[[temp.spec.partial]]. — *end note*]
|
| 3588 |
|
| 3589 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 3590 |
operand [[expr.context]].
|
| 3591 |
|
| 3592 |
The first declared template parameter of a concept definition is its
|
| 3593 |
*prototype parameter*. A *type concept* is a concept whose prototype
|
| 3594 |
parameter is a type *template-parameter*.
|
| 3595 |
|
| 3596 |
## Name resolution <a id="temp.res">[[temp.res]]</a>
|
| 3597 |
|
| 3598 |
+
### General <a id="temp.res.general">[[temp.res.general]]</a>
|
| 3599 |
|
| 3600 |
+
A name that appears in a declaration D of a template T is looked up from
|
| 3601 |
+
where it appears in an unspecified declaration of T that either is D
|
| 3602 |
+
itself or is reachable from D and from which no other declaration of T
|
| 3603 |
+
that contains the usage of the name is reachable. If the name is
|
| 3604 |
+
dependent (as specified in [[temp.dep]]), it is looked up for each
|
| 3605 |
+
specialization (after substitution) because the lookup depends on a
|
| 3606 |
+
template parameter.
|
| 3607 |
|
| 3608 |
+
[*Note 1*: Some dependent names are also looked up during parsing to
|
| 3609 |
+
determine that they are dependent or to interpret following `<` tokens.
|
| 3610 |
+
Uses of other names might be type-dependent or value-dependent
|
| 3611 |
+
[[temp.dep.expr]], [[temp.dep.constexpr]]. A *using-declarator* is never
|
| 3612 |
+
dependent in a specialization and is therefore replaced during lookup
|
| 3613 |
+
for that specialization [[basic.lookup]]. — *end note*]
|
| 3614 |
|
| 3615 |
[*Example 1*:
|
| 3616 |
|
| 3617 |
``` cpp
|
| 3618 |
+
struct A { operator int(); };
|
| 3619 |
+
template<class B, class T>
|
| 3620 |
+
struct D : B {
|
| 3621 |
+
T get() { return operator T(); } // conversion-function-id is dependent
|
| 3622 |
+
};
|
| 3623 |
+
int f(D<A, int> d) { return d.get(); } // OK, lookup finds A::operator int
|
| 3624 |
+
```
|
| 3625 |
+
|
| 3626 |
+
— *end example*]
|
| 3627 |
+
|
| 3628 |
+
[*Example 2*:
|
| 3629 |
+
|
| 3630 |
+
``` cpp
|
| 3631 |
+
void f(char);
|
| 3632 |
+
|
| 3633 |
+
template<class T> void g(T t) {
|
| 3634 |
+
f(1); // f(char)
|
| 3635 |
+
f(T(1)); // dependent
|
| 3636 |
+
f(t); // dependent
|
| 3637 |
+
dd++; // not dependent; error: declaration for dd not found
|
| 3638 |
}
|
| 3639 |
+
|
| 3640 |
+
enum E { e };
|
| 3641 |
+
void f(E);
|
| 3642 |
+
|
| 3643 |
+
double dd;
|
| 3644 |
+
void h() {
|
| 3645 |
+
g(e); // will cause one call of f(char) followed by two calls of f(E)
|
| 3646 |
+
g('a'); // will cause three calls of f(char)
|
| 3647 |
+
}
|
| 3648 |
+
```
|
| 3649 |
+
|
| 3650 |
+
— *end example*]
|
| 3651 |
+
|
| 3652 |
+
[*Example 3*:
|
| 3653 |
+
|
| 3654 |
+
``` cpp
|
| 3655 |
+
struct A {
|
| 3656 |
+
struct B { ... };
|
| 3657 |
+
int a;
|
| 3658 |
+
int Y;
|
| 3659 |
+
};
|
| 3660 |
+
|
| 3661 |
+
int a;
|
| 3662 |
+
|
| 3663 |
+
template<class T> struct Y : T {
|
| 3664 |
+
struct B { ... };
|
| 3665 |
+
B b; // The B defined in Y
|
| 3666 |
+
void f(int i) { a = i; } // ::a
|
| 3667 |
+
Y* p; // Y<T>
|
| 3668 |
};
|
| 3669 |
+
|
| 3670 |
+
Y<A> ya;
|
| 3671 |
```
|
| 3672 |
|
| 3673 |
+
The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
|
| 3674 |
+
not affect the binding of names in `Y<A>`.
|
| 3675 |
+
|
| 3676 |
— *end example*]
|
| 3677 |
|
| 3678 |
+
If the validity or meaning of the program would be changed by
|
| 3679 |
+
considering a default argument or default template argument introduced
|
| 3680 |
+
in a declaration that is reachable from the point of instantiation of a
|
| 3681 |
+
specialization [[temp.point]] but is not found by lookup for the
|
| 3682 |
+
specialization, the program is ill-formed, no diagnostic required.
|
| 3683 |
+
|
| 3684 |
``` bnf
|
| 3685 |
typename-specifier:
|
| 3686 |
typename nested-name-specifier identifier
|
| 3687 |
typename nested-name-specifier 'templateₒₚₜ ' simple-template-id
|
| 3688 |
```
|
| 3689 |
|
| 3690 |
+
The component names of a *typename-specifier* are its *identifier* (if
|
| 3691 |
+
any) and those of its *nested-name-specifier* and *simple-template-id*
|
| 3692 |
+
(if any). A *typename-specifier* denotes the type or class template
|
| 3693 |
+
denoted by the *simple-type-specifier* [[dcl.type.simple]] formed by
|
| 3694 |
+
omitting the keyword `typename`.
|
| 3695 |
|
| 3696 |
+
[*Note 2*: The usual qualified name lookup [[basic.lookup.qual]]
|
| 3697 |
+
applies even in the presence of `typename`. — *end note*]
|
| 3698 |
+
|
| 3699 |
+
[*Example 4*:
|
| 3700 |
|
| 3701 |
``` cpp
|
| 3702 |
struct A {
|
| 3703 |
struct X { };
|
| 3704 |
int X;
|
|
|
|
| 3710 |
typename T::X x;
|
| 3711 |
}
|
| 3712 |
void foo() {
|
| 3713 |
A a;
|
| 3714 |
B b;
|
| 3715 |
+
f(b); // OK, T::X refers to B::X
|
| 3716 |
f(a); // error: T::X refers to the data member A::X not the struct A::X
|
| 3717 |
}
|
| 3718 |
```
|
| 3719 |
|
| 3720 |
— *end example*]
|
| 3721 |
|
| 3722 |
+
A qualified or unqualified name is said to be in a *type-only context*
|
| 3723 |
+
if it is the terminal name of
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3724 |
|
| 3725 |
+
- a *typename-specifier*, *nested-name-specifier*,
|
| 3726 |
+
*elaborated-type-specifier*, *class-or-decltype*, or
|
| 3727 |
+
- a *type-specifier* of a
|
| 3728 |
+
- *new-type-id*,
|
| 3729 |
+
- *defining-type-id*,
|
| 3730 |
+
- *conversion-type-id*,
|
| 3731 |
+
- *trailing-return-type*,
|
| 3732 |
+
- default argument of a *type-parameter*, or
|
| 3733 |
+
- *type-id* of a `static_cast`, `const_cast`, `reinterpret_cast`, or
|
| 3734 |
+
`dynamic_cast`, or
|
| 3735 |
+
- a *decl-specifier* of the *decl-specifier-seq* of a
|
| 3736 |
- *simple-declaration* or a *function-definition* in namespace scope,
|
| 3737 |
- *member-declaration*,
|
| 3738 |
+
- *parameter-declaration* in a *member-declaration*,[^10] unless that
|
| 3739 |
*parameter-declaration* appears in a default argument,
|
| 3740 |
- *parameter-declaration* in a *declarator* of a function or function
|
| 3741 |
template declaration whose *declarator-id* is qualified, unless that
|
| 3742 |
*parameter-declaration* appears in a default argument,
|
| 3743 |
- *parameter-declaration* in a *lambda-declarator* or
|
| 3744 |
*requirement-parameter-list*, unless that *parameter-declaration*
|
| 3745 |
appears in a default argument, or
|
| 3746 |
- *parameter-declaration* of a (non-type) *template-parameter*.
|
| 3747 |
|
| 3748 |
+
[*Example 5*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3749 |
|
| 3750 |
``` cpp
|
| 3751 |
template<class T> T::R f(); // OK, return type of a function declaration at global scope
|
| 3752 |
template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
|
| 3753 |
// a void variable template
|
|
|
|
| 3765 |
}
|
| 3766 |
```
|
| 3767 |
|
| 3768 |
— *end example*]
|
| 3769 |
|
| 3770 |
+
A *qualified-id* whose terminal name is dependent and that is in a
|
| 3771 |
+
type-only context is considered to denote a type. A name that refers to
|
| 3772 |
+
a *using-declarator* whose terminal name is dependent is interpreted as
|
| 3773 |
+
a *typedef-name* if the *using-declarator* uses the keyword `typename`.
|
| 3774 |
|
| 3775 |
+
[*Example 6*:
|
| 3776 |
|
| 3777 |
``` cpp
|
| 3778 |
template <class T> void f(int i) {
|
| 3779 |
T::x * i; // expression, not the declaration of a variable i
|
| 3780 |
}
|
|
|
|
| 3793 |
}
|
| 3794 |
```
|
| 3795 |
|
| 3796 |
— *end example*]
|
| 3797 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3798 |
The validity of a template may be checked prior to any instantiation.
|
| 3799 |
|
| 3800 |
+
[*Note 3*: Knowing which names are type names allows the syntax of
|
| 3801 |
every template to be checked in this way. — *end note*]
|
| 3802 |
|
| 3803 |
The program is ill-formed, no diagnostic required, if:
|
| 3804 |
|
| 3805 |
+
- no valid specialization, ignoring *static_assert-declaration*s that
|
| 3806 |
+
fail, can be generated for a template or a substatement of a constexpr
|
| 3807 |
+
if statement [[stmt.if]] within a template and the template is not
|
| 3808 |
+
instantiated, or
|
| 3809 |
+
- any *constraint-expression* in the program, introduced or otherwise,
|
| 3810 |
+
has (in its normal form) an atomic constraint A where no satisfaction
|
| 3811 |
+
check of A could be well-formed and no satisfaction check of A is
|
| 3812 |
+
performed, or
|
| 3813 |
- every valid specialization of a variadic template requires an empty
|
| 3814 |
template parameter pack, or
|
| 3815 |
- a hypothetical instantiation of a template immediately following its
|
| 3816 |
definition would be ill-formed due to a construct that does not depend
|
| 3817 |
on a template parameter, or
|
| 3818 |
- the interpretation of such a construct in the hypothetical
|
| 3819 |
instantiation is different from the interpretation of the
|
| 3820 |
corresponding construct in any actual instantiation of the template.
|
| 3821 |
+
|
| 3822 |
+
[*Note 4*:
|
| 3823 |
+
|
| 3824 |
This can happen in situations including the following:
|
| 3825 |
+
|
| 3826 |
- a type used in a non-dependent name is incomplete at the point at
|
| 3827 |
which a template is defined but is complete at the point at which an
|
| 3828 |
instantiation is performed, or
|
| 3829 |
- lookup for a name in the template definition found a
|
| 3830 |
+
*using-declaration*, but the lookup in the corresponding scope in the
|
| 3831 |
+
instantiation does not find any declarations because the
|
| 3832 |
+
*using-declaration* was a pack expansion and the corresponding pack is
|
| 3833 |
+
empty, or
|
| 3834 |
+
- an instantiation uses a default argument or default template argument
|
| 3835 |
+
that had not been defined at the point at which the template was
|
| 3836 |
+
defined, or
|
| 3837 |
- constant expression evaluation [[expr.const]] within the template
|
| 3838 |
instantiation uses
|
| 3839 |
+
- the value of a const object of integral or unscoped enumeration type
|
| 3840 |
+
or
|
| 3841 |
- the value of a `constexpr` object or
|
| 3842 |
- the value of a reference or
|
| 3843 |
- the definition of a constexpr function,
|
| 3844 |
|
| 3845 |
and that entity was not defined when the template was defined, or
|
|
|
|
| 3853 |
— *end note*]
|
| 3854 |
|
| 3855 |
Otherwise, no diagnostic shall be issued for a template for which a
|
| 3856 |
valid specialization can be generated.
|
| 3857 |
|
| 3858 |
+
[*Note 5*: If a template is instantiated, errors will be diagnosed
|
| 3859 |
according to the other rules in this document. Exactly when these errors
|
| 3860 |
are diagnosed is a quality of implementation issue. — *end note*]
|
| 3861 |
|
| 3862 |
+
[*Example 7*:
|
| 3863 |
|
| 3864 |
``` cpp
|
| 3865 |
int j;
|
| 3866 |
template<class T> class X {
|
| 3867 |
void f(T t, int i, char* p) {
|
| 3868 |
t = i; // diagnosed if X::f is instantiated, and the assignment to t is an error
|
| 3869 |
p = i; // may be diagnosed even if X::f is not instantiated
|
| 3870 |
p = j; // may be diagnosed even if X::f is not instantiated
|
| 3871 |
+
X<T>::g(t); // OK
|
| 3872 |
+
X<T>::h(); // may be diagnosed even if X::f is not instantiated
|
| 3873 |
}
|
| 3874 |
void g(T t) {
|
| 3875 |
+; // may be diagnosed even if X::g is not instantiated
|
| 3876 |
}
|
| 3877 |
};
|
|
|
|
| 3883 |
template<class... T> struct A : T..., T... { }; // error: duplicate base class
|
| 3884 |
```
|
| 3885 |
|
| 3886 |
— *end example*]
|
| 3887 |
|
| 3888 |
+
[*Note 6*: For purposes of name lookup, default arguments and
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3889 |
*noexcept-specifier*s of function templates and default arguments and
|
| 3890 |
*noexcept-specifier*s of member functions of class templates are
|
| 3891 |
considered definitions [[temp.decls]]. — *end note*]
|
| 3892 |
|
| 3893 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
|
|
|
| 3898 |
*template-argument-list*, as a *template-argument* for a template
|
| 3899 |
*template-parameter*, or as the final identifier in the
|
| 3900 |
*elaborated-type-specifier* of a friend class template declaration, it
|
| 3901 |
is a *template-name* that refers to the class template itself.
|
| 3902 |
Otherwise, it is a *type-name* equivalent to the *template-name*
|
| 3903 |
+
followed by the template argument list
|
| 3904 |
+
[[temp.decls.general]], [[temp.arg.general]] of the class template
|
| 3905 |
+
enclosed in `<>`.
|
| 3906 |
|
| 3907 |
+
When the injected-class-name of a class template specialization or
|
| 3908 |
+
partial specialization is used as a *type-name*, it is equivalent to the
|
| 3909 |
+
*template-name* followed by the *template-argument*s of the class
|
| 3910 |
+
template specialization or partial specialization enclosed in `<>`.
|
|
|
|
| 3911 |
|
| 3912 |
[*Example 1*:
|
| 3913 |
|
| 3914 |
``` cpp
|
| 3915 |
template<template<class> class T> class A { };
|
|
|
|
| 3926 |
|
| 3927 |
— *end example*]
|
| 3928 |
|
| 3929 |
The injected-class-name of a class template or class template
|
| 3930 |
specialization can be used as either a *template-name* or a *type-name*
|
| 3931 |
+
wherever it is named.
|
| 3932 |
|
| 3933 |
[*Example 2*:
|
| 3934 |
|
| 3935 |
``` cpp
|
| 3936 |
template <class T> struct Base {
|
|
|
|
| 3939 |
|
| 3940 |
template <class T> struct Derived: public Base<T> {
|
| 3941 |
typename Derived::Base* p; // meaning Derived::Base<T>
|
| 3942 |
};
|
| 3943 |
|
| 3944 |
+
template<class T, template<class> class U = T::Base> struct Third { };
|
| 3945 |
+
Third<Derived<int> > t; // OK, default argument uses injected-class-name as a template
|
| 3946 |
```
|
| 3947 |
|
| 3948 |
— *end example*]
|
| 3949 |
|
| 3950 |
A lookup that finds an injected-class-name [[class.member.lookup]] can
|
|
|
|
| 3982 |
};
|
| 3983 |
```
|
| 3984 |
|
| 3985 |
— *end example*]
|
| 3986 |
|
| 3987 |
+
The name of a *template-parameter* shall not be bound to any following
|
| 3988 |
+
declaration whose locus is contained by the scope to which the
|
| 3989 |
+
template-parameter belongs.
|
| 3990 |
|
| 3991 |
[*Example 5*:
|
| 3992 |
|
| 3993 |
``` cpp
|
| 3994 |
template<class T, int i> class Y {
|
| 3995 |
+
int T; // error: template-parameter hidden
|
| 3996 |
void f() {
|
| 3997 |
+
char T; // error: template-parameter hidden
|
| 3998 |
}
|
| 3999 |
+
friend void T(); // OK, no name bound
|
| 4000 |
};
|
| 4001 |
|
| 4002 |
+
template<class X> class X; // error: hidden by template-parameter
|
| 4003 |
```
|
| 4004 |
|
| 4005 |
— *end example*]
|
| 4006 |
|
| 4007 |
+
Unqualified name lookup considers the template parameter scope of a
|
| 4008 |
+
*template-declaration* immediately after the outermost scope associated
|
| 4009 |
+
with the template declared (even if its parent scope does not contain
|
| 4010 |
+
the *template-parameter-list*).
|
| 4011 |
+
|
| 4012 |
+
[*Note 1*: The scope of a class template, including its non-dependent
|
| 4013 |
+
base classes [[temp.dep.type]], [[class.member.lookup]], is searched
|
| 4014 |
+
before its template parameter scope. — *end note*]
|
| 4015 |
|
| 4016 |
[*Example 6*:
|
| 4017 |
|
| 4018 |
``` cpp
|
| 4019 |
+
struct B { };
|
| 4020 |
+
namespace N {
|
| 4021 |
+
typedef void V;
|
| 4022 |
+
template<class T> struct A : B {
|
| 4023 |
typedef void C;
|
| 4024 |
void f();
|
| 4025 |
template<class U> void g(U);
|
| 4026 |
};
|
|
|
|
|
|
|
|
|
|
| 4027 |
}
|
| 4028 |
|
| 4029 |
+
template<class V> void N::A<V>::f() { // N::V not considered here
|
| 4030 |
+
V v; // V is still the template parameter, not N::V
|
|
|
|
| 4031 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4032 |
|
| 4033 |
+
template<class B> template<class C> void N::A<B>::g(C) {
|
| 4034 |
+
B b; // B is the base class, not the template parameter
|
| 4035 |
+
C c; // C is the template parameter, not A's C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4036 |
}
|
| 4037 |
```
|
| 4038 |
|
| 4039 |
— *end example*]
|
| 4040 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4041 |
### Dependent names <a id="temp.dep">[[temp.dep]]</a>
|
| 4042 |
|
| 4043 |
+
#### General <a id="temp.dep.general">[[temp.dep.general]]</a>
|
| 4044 |
+
|
| 4045 |
Inside a template, some constructs have semantics which may differ from
|
| 4046 |
one instantiation to another. Such a construct *depends* on the template
|
| 4047 |
parameters. In particular, types and expressions may depend on the type
|
| 4048 |
and/or value of template parameters (as determined by the template
|
| 4049 |
arguments) and this determines the context for name lookup for certain
|
| 4050 |
names. An expression may be *type-dependent* (that is, its type may
|
| 4051 |
depend on a template parameter) or *value-dependent* (that is, its value
|
| 4052 |
when evaluated as a constant expression [[expr.const]] may depend on a
|
| 4053 |
+
template parameter) as described below.
|
| 4054 |
|
| 4055 |
+
A *dependent call* is an expression, possibly formed as a non-member
|
| 4056 |
+
candidate for an operator [[over.match.oper]], of the form:
|
| 4057 |
|
| 4058 |
``` bnf
|
| 4059 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 4060 |
```
|
| 4061 |
|
| 4062 |
+
where the *postfix-expression* is an *unqualified-id* and
|
|
|
|
| 4063 |
|
| 4064 |
- any of the expressions in the *expression-list* is a pack expansion
|
| 4065 |
+
[[temp.variadic]], or
|
| 4066 |
- any of the expressions or *braced-init-list*s in the *expression-list*
|
| 4067 |
is type-dependent [[temp.dep.expr]], or
|
| 4068 |
- the *unqualified-id* is a *template-id* in which any of the template
|
| 4069 |
arguments depends on a template parameter.
|
| 4070 |
|
| 4071 |
+
The component name of an *unqualified-id* [[expr.prim.id.unqual]] is
|
| 4072 |
+
dependent if
|
| 4073 |
|
| 4074 |
+
- it is a *conversion-function-id* whose *conversion-type-id* is
|
| 4075 |
+
dependent, or
|
| 4076 |
+
- it is `operator=` and the current class is a templated entity, or
|
| 4077 |
+
- the *unqualified-id* is the *postfix-expression* in a dependent call.
|
| 4078 |
+
|
| 4079 |
+
[*Note 1*: Such names are looked up only at the point of the template
|
| 4080 |
+
instantiation [[temp.point]] in both the context of the template
|
| 4081 |
+
definition and the context of the point of instantiation
|
| 4082 |
[[temp.dep.candidate]]. — *end note*]
|
| 4083 |
|
| 4084 |
[*Example 1*:
|
| 4085 |
|
| 4086 |
``` cpp
|
|
|
|
| 4096 |
The base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 4097 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 4098 |
|
| 4099 |
— *end example*]
|
| 4100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4101 |
#### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
|
| 4102 |
|
| 4103 |
+
A name or *template-id* refers to the *current instantiation* if it is
|
| 4104 |
|
| 4105 |
- in the definition of a class template, a nested class of a class
|
| 4106 |
template, a member of a class template, or a member of a nested class
|
| 4107 |
of a class template, the injected-class-name [[class.pre]] of the
|
| 4108 |
class template or nested class,
|
| 4109 |
- in the definition of a primary class template or a member of a primary
|
| 4110 |
class template, the name of the class template followed by the
|
| 4111 |
+
template argument list of its *template-head* [[temp.arg]] enclosed in
|
| 4112 |
+
`<>` (or an equivalent template alias specialization),
|
| 4113 |
- in the definition of a nested class of a class template, the name of
|
| 4114 |
the nested class referenced as a member of the current instantiation,
|
| 4115 |
or
|
| 4116 |
+
- in the definition of a class template partial specialization or a
|
| 4117 |
+
member of a class template partial specialization, the name of the
|
| 4118 |
+
class template followed by a template argument list equivalent to that
|
| 4119 |
+
of the partial specialization [[temp.spec.partial]] enclosed in `<>`
|
| 4120 |
+
(or an equivalent template alias specialization).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4121 |
|
| 4122 |
A template argument that is equivalent to a template parameter can be
|
| 4123 |
used in place of that template parameter in a reference to the current
|
| 4124 |
instantiation. For a template *type-parameter*, a template argument is
|
| 4125 |
equivalent to a template parameter if it denotes the same type. For a
|
|
|
|
| 4201 |
|
| 4202 |
— *end example*]
|
| 4203 |
|
| 4204 |
— *end note*]
|
| 4205 |
|
| 4206 |
+
A qualified [[basic.lookup.qual]] or unqualified name is a *member of
|
| 4207 |
+
the current instantiation* if
|
| 4208 |
|
| 4209 |
+
- its lookup context, if it is a qualified name, is the current
|
| 4210 |
+
instantiation, and
|
| 4211 |
+
- lookup for it finds any member of a class that is the current
|
| 4212 |
+
instantiation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4213 |
|
| 4214 |
[*Example 3*:
|
| 4215 |
|
| 4216 |
``` cpp
|
| 4217 |
template <class T> class A {
|
|
|
|
| 4227 |
}
|
| 4228 |
```
|
| 4229 |
|
| 4230 |
— *end example*]
|
| 4231 |
|
| 4232 |
+
A qualified or unqualified name names a *dependent member of the current
|
| 4233 |
+
instantiation* if it is a member of the current instantiation that, when
|
| 4234 |
+
looked up, refers to at least one member declaration (including a
|
| 4235 |
+
*using-declarator* whose terminal name is dependent) of a class that is
|
| 4236 |
+
the current instantiation.
|
| 4237 |
|
| 4238 |
+
A qualified name [[basic.lookup.qual]] is dependent if
|
| 4239 |
|
| 4240 |
+
- it is a *conversion-function-id* whose *conversion-type-id* is
|
| 4241 |
+
dependent, or
|
| 4242 |
+
- its lookup context is dependent and is not the current instantiation,
|
| 4243 |
+
or
|
| 4244 |
+
- its lookup context is the current instantiation and it is
|
| 4245 |
+
`operator=`,[^11] or
|
| 4246 |
+
- its lookup context is the current instantiation and has at least one
|
| 4247 |
+
dependent base class, and qualified name lookup for the name finds
|
| 4248 |
+
nothing [[basic.lookup.qual]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4249 |
|
| 4250 |
[*Example 4*:
|
| 4251 |
|
| 4252 |
``` cpp
|
| 4253 |
+
struct A {
|
| 4254 |
+
using B = int;
|
| 4255 |
+
A f();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4256 |
};
|
| 4257 |
+
struct C : A {};
|
| 4258 |
+
template<class T>
|
| 4259 |
+
void g(T t) {
|
| 4260 |
+
decltype(t.A::f())::B i; // error: typename needed to interpret B as a type
|
| 4261 |
+
}
|
| 4262 |
+
template void g(C); // … even though A is ::A here
|
| 4263 |
```
|
| 4264 |
|
| 4265 |
— *end example*]
|
| 4266 |
|
| 4267 |
If, for a given set of template arguments, a specialization of a
|
| 4268 |
template is instantiated that refers to a member of the current
|
| 4269 |
+
instantiation with a qualified name, the name is looked up in the
|
| 4270 |
+
template instantiation context. If the result of this lookup differs
|
| 4271 |
+
from the result of name lookup in the template definition context, name
|
| 4272 |
+
lookup is ambiguous.
|
|
|
|
| 4273 |
|
| 4274 |
[*Example 5*:
|
| 4275 |
|
| 4276 |
``` cpp
|
| 4277 |
struct A {
|
|
|
|
| 4287 |
int f() { return this->m; } // finds A::m in the template definition context
|
| 4288 |
int g() { return m; } // finds A::m in the template definition context
|
| 4289 |
};
|
| 4290 |
|
| 4291 |
template int C<B>::f(); // error: finds both A::m and B::m
|
| 4292 |
+
template int C<B>::g(); // OK, transformation to class member access syntax
|
| 4293 |
+
// does not occur in the template definition context; see~[class.mfct.non.static]
|
| 4294 |
```
|
| 4295 |
|
| 4296 |
— *end example*]
|
| 4297 |
|
| 4298 |
A type is dependent if it is
|
| 4299 |
|
| 4300 |
- a template parameter,
|
| 4301 |
+
- denoted by a dependent (qualified) name,
|
| 4302 |
+
- a nested class or enumeration that is a direct member of a class that
|
| 4303 |
+
is the current instantiation,
|
| 4304 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 4305 |
- a compound type constructed from any dependent type,
|
| 4306 |
- an array type whose element type is dependent or whose bound (if any)
|
| 4307 |
is value-dependent,
|
| 4308 |
+
- a function type whose parameters include one or more function
|
| 4309 |
+
parameter packs,
|
| 4310 |
- a function type whose exception specification is value-dependent,
|
| 4311 |
- denoted by a *simple-template-id* in which either the template name is
|
| 4312 |
a template parameter or any of the template arguments is a dependent
|
| 4313 |
type or an expression that is type-dependent or value-dependent or is
|
| 4314 |
+
a pack expansion,[^12] or
|
|
|
|
|
|
|
| 4315 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 4316 |
type-dependent [[temp.dep.expr]].
|
| 4317 |
|
| 4318 |
+
[*Note 3*: Because typedefs do not introduce new types, but instead
|
| 4319 |
simply refer to other types, a name that refers to a typedef that is a
|
| 4320 |
member of the current instantiation is dependent only if the type
|
| 4321 |
referred to is dependent. — *end note*]
|
| 4322 |
|
| 4323 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
| 4324 |
|
| 4325 |
Except as described below, an expression is type-dependent if any
|
| 4326 |
subexpression is type-dependent.
|
| 4327 |
|
| 4328 |
+
`this` is type-dependent if the current class [[expr.prim.this]] is
|
|
|
|
|
|
|
| 4329 |
dependent [[temp.dep.type]].
|
| 4330 |
|
| 4331 |
+
An *id-expression* is type-dependent if it is a *template-id* that is
|
| 4332 |
+
not a concept-id and is dependent; or if its terminal name is
|
| 4333 |
|
| 4334 |
+
- associated by name lookup with one or more declarations declared with
|
| 4335 |
+
a dependent type,
|
| 4336 |
+
- associated by name lookup with a non-type *template-parameter*
|
| 4337 |
+
declared with a type that contains a placeholder type
|
| 4338 |
+
[[dcl.spec.auto]],
|
| 4339 |
+
- associated by name lookup with a variable declared with a type that
|
| 4340 |
+
contains a placeholder type [[dcl.spec.auto]] where the initializer is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4341 |
type-dependent,
|
| 4342 |
+
- associated by name lookup with one or more declarations of member
|
| 4343 |
+
functions of a class that is the current instantiation declared with a
|
| 4344 |
+
return type that contains a placeholder type,
|
| 4345 |
+
- associated by name lookup with a structured binding declaration
|
| 4346 |
+
[[dcl.struct.bind]] whose *brace-or-equal-initializer* is
|
| 4347 |
+
type-dependent,
|
| 4348 |
+
- associated by name lookup with an entity captured by copy
|
| 4349 |
+
[[expr.prim.lambda.capture]] in a *lambda-expression* that has an
|
| 4350 |
+
explicit object parameter whose type is dependent [[dcl.fct]],
|
| 4351 |
- the *identifier* `__func__` [[dcl.fct.def.general]], where any
|
| 4352 |
enclosing function is a template, a member of a class template, or a
|
| 4353 |
generic lambda,
|
|
|
|
| 4354 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 4355 |
+
- dependent
|
|
|
|
| 4356 |
|
| 4357 |
or if it names a dependent member of the current instantiation that is a
|
| 4358 |
static data member of type “array of unknown bound of `T`” for some `T`
|
| 4359 |
[[temp.static]]. Expressions of the following forms are type-dependent
|
| 4360 |
+
only if the type specified by the *type-id*, *simple-type-specifier*,
|
| 4361 |
+
*typename-specifier*, or *new-type-id* is dependent, even if any
|
| 4362 |
+
subexpression is type-dependent:
|
| 4363 |
|
| 4364 |
``` bnf
|
| 4365 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 4366 |
+
simple-type-specifier braced-init-list
|
| 4367 |
+
typename-specifier '(' expression-listₒₚₜ ')'
|
| 4368 |
+
typename-specifier braced-init-list
|
| 4369 |
'::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 4370 |
'::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
| 4371 |
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 4372 |
static_cast '<' type-id '>' '(' expression ')'
|
| 4373 |
const_cast '<' type-id '>' '(' expression ')'
|
|
|
|
| 4394 |
|
| 4395 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 4396 |
[[support.types]]. — *end note*]
|
| 4397 |
|
| 4398 |
A class member access expression [[expr.ref]] is type-dependent if the
|
| 4399 |
+
terminal name of its *id-expression*, if any, is dependent or the
|
| 4400 |
expression refers to a member of the current instantiation and the type
|
| 4401 |
+
of the referenced member is dependent.
|
|
|
|
| 4402 |
|
| 4403 |
[*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
|
| 4404 |
the expression is usually the type of the member `y` of the class of `x`
|
| 4405 |
(or the class pointed to by `xp`). However, if `x` or `xp` refers to a
|
| 4406 |
dependent type that is not the current instantiation, the type of `y` is
|
| 4407 |
+
always dependent. — *end note*]
|
|
|
|
|
|
|
| 4408 |
|
| 4409 |
A *braced-init-list* is type-dependent if any element is type-dependent
|
| 4410 |
or is a pack expansion.
|
| 4411 |
|
| 4412 |
A *fold-expression* is type-dependent.
|
|
|
|
| 4483 |
Furthermore, a non-type *template-argument* is dependent if the
|
| 4484 |
corresponding non-type *template-parameter* is of reference or pointer
|
| 4485 |
type and the *template-argument* designates or points to a member of the
|
| 4486 |
current instantiation or a member of a dependent type.
|
| 4487 |
|
| 4488 |
+
A template *template-parameter* is dependent if it names a
|
| 4489 |
+
*template-parameter* or its terminal name is dependent.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4490 |
|
| 4491 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 4492 |
|
| 4493 |
#### Point of instantiation <a id="temp.point">[[temp.point]]</a>
|
| 4494 |
|
|
|
|
| 4560 |
different meanings according to the one-definition rule
|
| 4561 |
[[basic.def.odr]], the program is ill-formed, no diagnostic required.
|
| 4562 |
|
| 4563 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 4564 |
|
| 4565 |
+
If a dependent call [[temp.dep]] would be ill-formed or would find a
|
| 4566 |
+
better match had the lookup for its dependent name considered all the
|
| 4567 |
+
function declarations with external linkage introduced in the associated
|
| 4568 |
+
namespaces in all translation units, not just considering those
|
| 4569 |
+
declarations found in the template definition and template instantiation
|
| 4570 |
+
contexts [[basic.lookup.argdep]], then the program is ill-formed, no
|
| 4571 |
+
diagnostic required.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4572 |
|
| 4573 |
[*Example 1*:
|
| 4574 |
|
| 4575 |
Source file \`"X.h"\`
|
| 4576 |
|
|
|
|
| 4670 |
module;
|
| 4671 |
#include "X.h"
|
| 4672 |
export module M;
|
| 4673 |
import F;
|
| 4674 |
void g(X x) {
|
| 4675 |
+
f(x); // OK, instantiates f from F,
|
| 4676 |
// operator+ is visible in instantiation context
|
| 4677 |
}
|
| 4678 |
```
|
| 4679 |
|
| 4680 |
— *end example*]
|
|
|
|
| 4766 |
}
|
| 4767 |
```
|
| 4768 |
|
| 4769 |
— *end example*]
|
| 4770 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4771 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 4772 |
|
| 4773 |
+
### General <a id="temp.spec.general">[[temp.spec.general]]</a>
|
| 4774 |
+
|
| 4775 |
The act of instantiating a function, a variable, a class, a member of a
|
| 4776 |
class template, or a member template is referred to as *template
|
| 4777 |
instantiation*.
|
| 4778 |
|
| 4779 |
A function instantiated from a function template is called an
|
|
|
|
| 4792 |
|
| 4793 |
An explicit specialization may be declared for a function template, a
|
| 4794 |
variable template, a class template, a member of a class template, or a
|
| 4795 |
member template. An explicit specialization declaration is introduced by
|
| 4796 |
`template<>`. In an explicit specialization declaration for a variable
|
| 4797 |
+
template, a class template, a member of a class template, or a class
|
| 4798 |
+
member template, the variable or class that is explicitly specialized
|
| 4799 |
+
shall be specified with a *simple-template-id*. In the explicit
|
| 4800 |
specialization declaration for a function template or a member function
|
| 4801 |
+
template, the function or member function explicitly specialized may be
|
| 4802 |
+
specified using a *template-id*.
|
| 4803 |
|
| 4804 |
[*Example 1*:
|
| 4805 |
|
| 4806 |
``` cpp
|
| 4807 |
template<class T = int> struct A {
|
|
|
|
| 4837 |
program,
|
| 4838 |
- an explicit specialization shall be defined at most once in a program,
|
| 4839 |
as specified in [[basic.def.odr]], and
|
| 4840 |
- both an explicit instantiation and a declaration of an explicit
|
| 4841 |
specialization shall not appear in a program unless the explicit
|
| 4842 |
+
specialization is reachable from the explicit instantiation.
|
| 4843 |
|
| 4844 |
+
An implementation is not required to diagnose a violation of this rule
|
| 4845 |
+
if neither declaration is reachable from the other.
|
| 4846 |
|
| 4847 |
The usual access checking rules do not apply to names in a declaration
|
| 4848 |
of an explicit instantiation or explicit specialization, with the
|
| 4849 |
exception of names appearing in a function body, default argument,
|
| 4850 |
+
*base-clause*, *member-specification*, *enumerator-list*, or static data
|
| 4851 |
member or variable template initializer.
|
| 4852 |
|
| 4853 |
[*Note 1*: In particular, the template arguments and names used in the
|
| 4854 |
function declarator (including parameter types, return types and
|
| 4855 |
+
exception specifications) can be private types or objects that would
|
| 4856 |
normally not be accessible. — *end note*]
|
| 4857 |
|
| 4858 |
Each class template specialization instantiated from a template has its
|
| 4859 |
own copy of any static members.
|
| 4860 |
|
|
|
|
| 4940 |
}
|
| 4941 |
```
|
| 4942 |
|
| 4943 |
— *end example*]
|
| 4944 |
|
| 4945 |
+
If the template selected for the specialization
|
| 4946 |
+
[[temp.spec.partial.match]] has been declared, but not defined, at the
|
| 4947 |
+
point of instantiation [[temp.point]], the instantiation yields an
|
| 4948 |
+
incomplete class type [[term.incomplete.type]].
|
| 4949 |
|
| 4950 |
[*Example 2*:
|
| 4951 |
|
| 4952 |
``` cpp
|
| 4953 |
template<class T> class X;
|
|
|
|
| 4993 |
|
| 4994 |
— *end example*]
|
| 4995 |
|
| 4996 |
However, for the purpose of determining whether an instantiated
|
| 4997 |
redeclaration is valid according to [[basic.def.odr]] and
|
| 4998 |
+
[[class.mem]], an instantiated declaration that corresponds to a
|
| 4999 |
+
definition in the template is considered to be a definition.
|
| 5000 |
|
| 5001 |
[*Example 4*:
|
| 5002 |
|
| 5003 |
``` cpp
|
| 5004 |
template<class T, class U>
|
| 5005 |
struct Outer {
|
| 5006 |
template<class X, class Y> struct Inner;
|
| 5007 |
template<class Y> struct Inner<T, Y>; // #1a
|
| 5008 |
+
template<class Y> struct Inner<T, Y> { }; // #1b; OK, valid redeclaration of #1a
|
| 5009 |
template<class Y> struct Inner<U, Y> { }; // #2
|
| 5010 |
};
|
| 5011 |
|
| 5012 |
Outer<int, int> outer; // error at #2
|
| 5013 |
```
|
|
|
|
| 5026 |
Friendly<float> ff; // error: produces second definition of f(U)
|
| 5027 |
```
|
| 5028 |
|
| 5029 |
— *end example*]
|
| 5030 |
|
| 5031 |
+
Unless a member of a templated class is a declared specialization, the
|
| 5032 |
+
specialization of the member is implicitly instantiated when the
|
| 5033 |
+
specialization is referenced in a context that requires the member
|
| 5034 |
+
definition to exist or if the existence of the definition of the member
|
| 5035 |
+
affects the semantics of the program; in particular, the initialization
|
| 5036 |
+
(and any associated side effects) of a static data member does not occur
|
| 5037 |
+
unless the static data member is itself used in a way that requires the
|
| 5038 |
+
definition of the static data member to exist.
|
|
|
|
| 5039 |
|
| 5040 |
Unless a function template specialization is a declared specialization,
|
| 5041 |
the function template specialization is implicitly instantiated when the
|
| 5042 |
specialization is referenced in a context that requires a function
|
| 5043 |
definition to exist or if the existence of the definition affects the
|
|
|
|
| 5052 |
context that requires the value of the default argument.
|
| 5053 |
|
| 5054 |
[*Note 4*: An inline function that is the subject of an explicit
|
| 5055 |
instantiation declaration is not a declared specialization; the intent
|
| 5056 |
is that it still be implicitly instantiated when odr-used
|
| 5057 |
+
[[term.odr.use]] so that the body can be considered for inlining, but
|
| 5058 |
that no out-of-line copy of it be generated in the translation
|
| 5059 |
unit. — *end note*]
|
| 5060 |
|
| 5061 |
[*Example 5*:
|
| 5062 |
|
|
|
|
| 5140 |
used in a way that involves overload resolution, a declaration of the
|
| 5141 |
specialization is implicitly instantiated [[temp.over]].
|
| 5142 |
|
| 5143 |
An implementation shall not implicitly instantiate a function template,
|
| 5144 |
a variable template, a member template, a non-virtual member function, a
|
| 5145 |
+
member class or static data member of a templated class, or a
|
| 5146 |
substatement of a constexpr if statement [[stmt.if]], unless such
|
| 5147 |
instantiation is required.
|
| 5148 |
|
| 5149 |
[*Note 5*: The instantiation of a generic lambda does not require
|
| 5150 |
instantiation of substatements of a constexpr if statement within its
|
|
|
|
| 5152 |
instantiated. — *end note*]
|
| 5153 |
|
| 5154 |
It is unspecified whether or not an implementation implicitly
|
| 5155 |
instantiates a virtual member function of a class template if the
|
| 5156 |
virtual member function would not otherwise be instantiated. The use of
|
| 5157 |
+
a template specialization in a default argument or default member
|
| 5158 |
+
initializer shall not cause the template to be implicitly instantiated
|
| 5159 |
+
except where needed to determine the correctness of the default argument
|
| 5160 |
+
or default member initializer. The use of a default argument in a
|
| 5161 |
function call causes specializations in the default argument to be
|
| 5162 |
+
implicitly instantiated. Similarly, the use of a default member
|
| 5163 |
+
initializer in a constructor definition or an aggregate initialization
|
| 5164 |
+
causes specializations in the default member initializer to be
|
| 5165 |
+
instantiated.
|
| 5166 |
|
| 5167 |
+
If a templated function `f` is called in a way that requires a default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5168 |
argument to be used, the dependent names are looked up, the semantics
|
| 5169 |
constraints are checked, and the instantiation of any template used in
|
| 5170 |
the default argument is done as if the default argument had been an
|
| 5171 |
initializer used in a function template specialization with the same
|
| 5172 |
scope, the same template parameters and the same access as that of the
|
| 5173 |
function template `f` used at that point, except that the scope in which
|
| 5174 |
+
a closure type is declared [[expr.prim.lambda.closure]] — and therefore
|
| 5175 |
+
its associated namespaces — remain as determined from the context of the
|
| 5176 |
definition for the default argument. This analysis is called *default
|
| 5177 |
argument instantiation*. The instantiated default argument is then used
|
| 5178 |
as the argument of `f`.
|
| 5179 |
|
| 5180 |
Each default argument is instantiated independently.
|
| 5181 |
|
| 5182 |
+
[*Example 8*:
|
| 5183 |
|
| 5184 |
``` cpp
|
| 5185 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 5186 |
|
| 5187 |
class A { };
|
|
|
|
| 5212 |
There is an *implementation-defined* quantity that specifies the limit
|
| 5213 |
on the total depth of recursive instantiations [[implimits]], which
|
| 5214 |
could involve more than one template. The result of an infinite
|
| 5215 |
recursion in instantiation is undefined.
|
| 5216 |
|
| 5217 |
+
[*Example 9*:
|
| 5218 |
|
| 5219 |
``` cpp
|
| 5220 |
template<class T> class X {
|
| 5221 |
X<T>* p; // OK
|
| 5222 |
X<T*> a; // implicit generation of X<T> requires
|
|
|
|
| 5238 |
|
| 5239 |
[*Note 7*: The satisfaction of constraints is determined during
|
| 5240 |
template argument deduction [[temp.deduct]] and overload resolution
|
| 5241 |
[[over.match]]. — *end note*]
|
| 5242 |
|
| 5243 |
+
[*Example 10*:
|
| 5244 |
|
| 5245 |
``` cpp
|
| 5246 |
template<typename T> concept C = sizeof(T) > 2;
|
| 5247 |
template<typename T> concept D = C<T> && sizeof(T) > 4;
|
| 5248 |
|
|
|
|
| 5260 |
the implicit declaration of a default constructor for `S<char>`
|
| 5261 |
[[class.default.ctor]], so there is no viable constructor for `s1`.
|
| 5262 |
|
| 5263 |
— *end example*]
|
| 5264 |
|
| 5265 |
+
[*Example 11*:
|
| 5266 |
|
| 5267 |
``` cpp
|
| 5268 |
template<typename T> struct S1 {
|
| 5269 |
template<typename U>
|
| 5270 |
requires false
|
|
|
|
| 5314 |
If the explicit instantiation is for a class or member class, the
|
| 5315 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 5316 |
*simple-template-id*; otherwise, the *declaration* shall be a
|
| 5317 |
*simple-declaration* whose *init-declarator-list* comprises a single
|
| 5318 |
*init-declarator* that does not have an *initializer*. If the explicit
|
| 5319 |
+
instantiation is for a variable template specialization, the
|
| 5320 |
+
*unqualified-id* in the *declarator* shall be a *simple-template-id*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5321 |
|
| 5322 |
[*Example 1*:
|
| 5323 |
|
| 5324 |
``` cpp
|
| 5325 |
template<class T> class Array { void mf(); };
|
|
|
|
| 5335 |
template void N::f<int>(int&);
|
| 5336 |
```
|
| 5337 |
|
| 5338 |
— *end example*]
|
| 5339 |
|
| 5340 |
+
An explicit instantiation does not introduce a name
|
| 5341 |
+
[[basic.scope.scope]]. A declaration of a function template, a variable
|
| 5342 |
+
template, a member function or static data member of a class template,
|
| 5343 |
+
or a member function template of a class or class template shall be
|
| 5344 |
+
reachable from any explicit instantiation of that entity. A definition
|
| 5345 |
+
of a class template, a member class of a class template, or a member
|
| 5346 |
+
class template of a class or class template shall be reachable from any
|
| 5347 |
+
explicit instantiation of that entity unless an explicit specialization
|
| 5348 |
+
of the entity with the same template arguments is reachable therefrom.
|
| 5349 |
+
If the *declaration* of the explicit instantiation names an
|
| 5350 |
+
implicitly-declared special member function [[special]], the program is
|
| 5351 |
+
ill-formed.
|
| 5352 |
|
| 5353 |
The *declaration* in an *explicit-instantiation* and the *declaration*
|
| 5354 |
produced by the corresponding substitution into the templated function,
|
| 5355 |
variable, or class are two declarations of the same entity.
|
| 5356 |
|
| 5357 |
+
[*Note 1*:
|
| 5358 |
|
| 5359 |
These declarations are required to have matching types as specified in
|
| 5360 |
[[basic.link]], except as specified in [[except.spec]].
|
| 5361 |
|
| 5362 |
[*Example 2*:
|
|
|
|
| 5390 |
an explicit instantiation definition, the definition of a function
|
| 5391 |
template, a variable template, a member function template, or a member
|
| 5392 |
function or static data member of a class template shall be present in
|
| 5393 |
every translation unit in which it is explicitly instantiated.
|
| 5394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5395 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 5396 |
instantiation of a function template specialization or of a member
|
| 5397 |
+
function template specialization provided it can be deduced
|
| 5398 |
+
[[temp.deduct.decl]]. If all template arguments can be deduced, the
|
| 5399 |
+
empty template argument list `<>` may be omitted.
|
| 5400 |
|
| 5401 |
+
[*Example 3*:
|
| 5402 |
|
| 5403 |
``` cpp
|
| 5404 |
template<class T> class Array { ... };
|
| 5405 |
template<class T> void sort(Array<T>& v) { ... }
|
| 5406 |
|
|
|
|
| 5408 |
template void sort<>(Array<int>&);
|
| 5409 |
```
|
| 5410 |
|
| 5411 |
— *end example*]
|
| 5412 |
|
| 5413 |
+
[*Note 2*: An explicit instantiation of a constrained template is
|
| 5414 |
required to satisfy that template’s associated constraints
|
| 5415 |
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 5416 |
forming the template name of an explicit instantiation in which all
|
| 5417 |
template arguments are specified [[temp.names]], or, for explicit
|
| 5418 |
instantiations of function templates, during template argument deduction
|
| 5419 |
[[temp.deduct.decl]] when one or more trailing template arguments are
|
| 5420 |
left unspecified. — *end note*]
|
| 5421 |
|
| 5422 |
An explicit instantiation that names a class template specialization is
|
| 5423 |
also an explicit instantiation of the same kind (declaration or
|
| 5424 |
+
definition) of each of its direct non-template members that has not been
|
|
|
|
| 5425 |
previously explicitly specialized in the translation unit containing the
|
| 5426 |
explicit instantiation, provided that the associated constraints, if
|
| 5427 |
any, of that member are satisfied by the template arguments of the
|
| 5428 |
+
explicit instantiation [[temp.constr.decl]], [[temp.constr.constr]],
|
| 5429 |
except as described below.
|
| 5430 |
|
| 5431 |
+
[*Note 3*: In addition, it will typically be an explicit instantiation
|
| 5432 |
of certain implementation-dependent data about the class. — *end note*]
|
| 5433 |
|
| 5434 |
An explicit instantiation definition that names a class template
|
| 5435 |
specialization explicitly instantiates the class template specialization
|
| 5436 |
and is an explicit instantiation definition of only those members that
|
| 5437 |
have been defined at the point of instantiation.
|
| 5438 |
|
| 5439 |
An explicit instantiation of a prospective destructor [[class.dtor]]
|
| 5440 |
+
shall correspond to the selected destructor of the class.
|
| 5441 |
|
| 5442 |
If an entity is the subject of both an explicit instantiation
|
| 5443 |
declaration and an explicit instantiation definition in the same
|
| 5444 |
translation unit, the definition shall follow the declaration. An entity
|
| 5445 |
that is the subject of an explicit instantiation declaration and that is
|
| 5446 |
also used in a way that would otherwise cause an implicit instantiation
|
| 5447 |
[[temp.inst]] in the translation unit shall be the subject of an
|
| 5448 |
explicit instantiation definition somewhere in the program; otherwise
|
| 5449 |
the program is ill-formed, no diagnostic required.
|
| 5450 |
|
| 5451 |
+
[*Note 4*: This rule does apply to inline functions even though an
|
| 5452 |
explicit instantiation declaration of such an entity has no other
|
| 5453 |
normative effect. This is needed to ensure that if the address of an
|
| 5454 |
inline function is taken in a translation unit in which the
|
| 5455 |
implementation chose to suppress the out-of-line body, another
|
| 5456 |
translation unit will supply the body. — *end note*]
|
|
|
|
| 5459 |
a template with internal linkage.
|
| 5460 |
|
| 5461 |
An explicit instantiation does not constitute a use of a default
|
| 5462 |
argument, so default argument instantiation is not done.
|
| 5463 |
|
| 5464 |
+
[*Example 4*:
|
| 5465 |
|
| 5466 |
``` cpp
|
| 5467 |
char* p = 0;
|
| 5468 |
template<class T> T g(T x = &p) { return x; }
|
| 5469 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
|
|
| 5512 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 5513 |
generated from the template.
|
| 5514 |
|
| 5515 |
— *end example*]
|
| 5516 |
|
| 5517 |
+
The *declaration* in an *explicit-specialization* shall not be an
|
| 5518 |
+
*export-declaration*. An explicit specialization shall not use a
|
| 5519 |
+
*storage-class-specifier* [[dcl.stc]] other than `thread_local`.
|
| 5520 |
|
| 5521 |
An explicit specialization may be declared in any scope in which the
|
| 5522 |
+
corresponding primary template may be defined
|
| 5523 |
+
[[dcl.meaning]], [[class.mem]], [[temp.mem]].
|
| 5524 |
|
| 5525 |
+
An explicit specialization does not introduce a name
|
| 5526 |
+
[[basic.scope.scope]]. A declaration of a function template, class
|
| 5527 |
+
template, or variable template being explicitly specialized shall be
|
| 5528 |
+
reachable from the declaration of the explicit specialization.
|
| 5529 |
|
| 5530 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 5531 |
required. — *end note*]
|
| 5532 |
|
| 5533 |
+
The definition of a class or class template shall be reachable from the
|
| 5534 |
declaration of an explicit specialization for a member template of the
|
| 5535 |
class or class template.
|
| 5536 |
|
| 5537 |
[*Example 2*:
|
| 5538 |
|
| 5539 |
``` cpp
|
| 5540 |
template<> class X<int> { ... }; // error: X not a template
|
| 5541 |
|
| 5542 |
template<class T> class X;
|
| 5543 |
|
| 5544 |
+
template<> class X<char*> { ... }; // OK, X is a template
|
| 5545 |
```
|
| 5546 |
|
| 5547 |
— *end example*]
|
| 5548 |
|
| 5549 |
A member function, a member function template, a member class, a member
|
| 5550 |
enumeration, a member class template, a static data member, or a static
|
| 5551 |
data member template of a class template may be explicitly specialized
|
| 5552 |
for a class specialization that is implicitly instantiated; in this
|
| 5553 |
+
case, the definition of the class template shall be reachable from the
|
| 5554 |
+
explicit specialization for the member of the class template. If such an
|
| 5555 |
+
explicit specialization for the member of a class template names an
|
| 5556 |
implicitly-declared special member function [[special]], the program is
|
| 5557 |
ill-formed.
|
| 5558 |
|
| 5559 |
A member of an explicitly specialized class is not implicitly
|
| 5560 |
instantiated from the member declaration of the class template; instead,
|
| 5561 |
the member of the class template specialization shall itself be
|
| 5562 |
+
explicitly defined if its definition is required. The definition of the
|
| 5563 |
+
class template explicit specialization shall be reachable from the
|
| 5564 |
+
definition of any member of it. The definition of an explicitly
|
| 5565 |
+
specialized class is unrelated to the definition of a generated
|
| 5566 |
+
specialization. That is, its members need not have the same names,
|
| 5567 |
+
types, etc. as the members of a generated specialization. Members of an
|
| 5568 |
+
explicitly specialized class template are defined in the same manner as
|
| 5569 |
+
members of normal classes, and not using the `template<>` syntax. The
|
| 5570 |
+
same is true when defining a member of an explicitly specialized member
|
| 5571 |
+
class. However, `template<>` is used in defining a member of an
|
| 5572 |
+
explicitly specialized member class template that is specialized as a
|
| 5573 |
+
class template.
|
| 5574 |
|
| 5575 |
[*Example 3*:
|
| 5576 |
|
| 5577 |
``` cpp
|
| 5578 |
template<class T> struct A {
|
|
|
|
| 5618 |
```
|
| 5619 |
|
| 5620 |
— *end example*]
|
| 5621 |
|
| 5622 |
If a template, a member template or a member of a class template is
|
| 5623 |
+
explicitly specialized, a declaration of that specialization shall be
|
| 5624 |
+
reachable from every use of that specialization that would cause an
|
| 5625 |
+
implicit instantiation to take place, in every translation unit in which
|
| 5626 |
+
such a use occurs; no diagnostic is required. If the program does not
|
| 5627 |
+
provide a definition for an explicit specialization and either the
|
| 5628 |
+
specialization is used in a way that would cause an implicit
|
| 5629 |
+
instantiation to take place or the member is a virtual member function,
|
| 5630 |
+
the program is ill-formed, no diagnostic required. An implicit
|
| 5631 |
+
instantiation is never generated for an explicit specialization that is
|
| 5632 |
+
declared but not defined.
|
| 5633 |
|
| 5634 |
[*Example 4*:
|
| 5635 |
|
| 5636 |
``` cpp
|
| 5637 |
class String { };
|
|
|
|
| 5641 |
void f(Array<String>& v) {
|
| 5642 |
sort(v); // use primary template sort(Array<T>&), T is String
|
| 5643 |
}
|
| 5644 |
|
| 5645 |
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
| 5646 |
+
template<> void sort<>(Array<char*>& v); // OK, sort<char*> not yet used
|
| 5647 |
template<class T> struct A {
|
| 5648 |
enum E : T;
|
| 5649 |
enum class S : T;
|
| 5650 |
};
|
| 5651 |
template<> enum A<int>::E : int { eint }; // OK
|
|
|
|
| 5676 |
positioning of the explicit specialization declarations and their points
|
| 5677 |
of instantiation in the translation unit as specified above and below.
|
| 5678 |
When writing a specialization, be careful about its location; or to make
|
| 5679 |
it compile will be such a trial as to kindle its self-immolation.
|
| 5680 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5681 |
A *simple-template-id* that names a class template explicit
|
| 5682 |
specialization that has been declared but not defined can be used
|
| 5683 |
exactly like the names of other incompletely-defined classes
|
| 5684 |
[[basic.types]].
|
| 5685 |
|
| 5686 |
+
[*Example 5*:
|
| 5687 |
|
| 5688 |
``` cpp
|
| 5689 |
template<class T> class X; // X is a class template
|
| 5690 |
template<> class X<int>;
|
| 5691 |
|
| 5692 |
+
X<int>* p; // OK, pointer to declared class X<int>
|
| 5693 |
X<int> x; // error: object of incomplete class X<int>
|
| 5694 |
```
|
| 5695 |
|
| 5696 |
— *end example*]
|
| 5697 |
|
| 5698 |
A trailing *template-argument* can be left unspecified in the
|
| 5699 |
*template-id* naming an explicit function template specialization
|
| 5700 |
+
provided it can be deduced [[temp.deduct.decl]].
|
| 5701 |
|
| 5702 |
+
[*Example 6*:
|
| 5703 |
|
| 5704 |
``` cpp
|
| 5705 |
template<class T> class Array { ... };
|
| 5706 |
template<class T> void sort(Array<T>& v);
|
| 5707 |
|
|
|
|
| 5724 |
A function with the same name as a template and a type that exactly
|
| 5725 |
matches that of a template specialization is not an explicit
|
| 5726 |
specialization [[temp.fct]].
|
| 5727 |
|
| 5728 |
Whether an explicit specialization of a function or variable template is
|
| 5729 |
+
inline, constexpr, constinit, or consteval is determined by the explicit
|
| 5730 |
+
specialization and is independent of those properties of the template.
|
| 5731 |
+
Similarly, attributes appearing in the declaration of a template have no
|
| 5732 |
+
effect on an explicit specialization of that template.
|
| 5733 |
|
| 5734 |
+
[*Example 7*:
|
| 5735 |
|
| 5736 |
``` cpp
|
| 5737 |
template<class T> void f(T) { ... }
|
| 5738 |
template<class T> inline T g(T) { ... }
|
| 5739 |
|
| 5740 |
+
template<> inline void f<>(int) { ... } // OK, inline
|
| 5741 |
+
template<> int g<>(int) { ... } // OK, not inline
|
| 5742 |
+
|
| 5743 |
+
template<typename> [[noreturn]] void h([[maybe_unused]] int i);
|
| 5744 |
+
template<> void h<int>(int i) {
|
| 5745 |
+
// Implementations are expected not to warn that the function returns
|
| 5746 |
+
// but can warn about the unused parameter.
|
| 5747 |
+
}
|
| 5748 |
```
|
| 5749 |
|
| 5750 |
— *end example*]
|
| 5751 |
|
| 5752 |
An explicit specialization of a static data member of a template or an
|
|
|
|
| 5754 |
if the declaration includes an initializer; otherwise, it is a
|
| 5755 |
declaration.
|
| 5756 |
|
| 5757 |
[*Note 3*:
|
| 5758 |
|
| 5759 |
+
The definition of a static data member of a template for which
|
| 5760 |
+
default-initialization is desired can use functional cast notation
|
| 5761 |
+
[[expr.type.conv]]:
|
| 5762 |
|
| 5763 |
``` cpp
|
| 5764 |
template<> X Q<int>::x; // declaration
|
| 5765 |
template<> X Q<int>::x (); // error: declares a function
|
| 5766 |
+
template<> X Q<int>::x = X(); // definition
|
| 5767 |
```
|
| 5768 |
|
| 5769 |
— *end note*]
|
| 5770 |
|
| 5771 |
A member or a member template of a class template may be explicitly
|
| 5772 |
specialized for a given implicit instantiation of the class template,
|
| 5773 |
even if the member or member template is defined in the class template
|
| 5774 |
definition. An explicit specialization of a member or member template is
|
| 5775 |
specified using the syntax for explicit specialization.
|
| 5776 |
|
| 5777 |
+
[*Example 8*:
|
| 5778 |
|
| 5779 |
``` cpp
|
| 5780 |
template<class T> struct A {
|
| 5781 |
void f(T);
|
| 5782 |
template<class X1> void g1(T, X1);
|
|
|
|
| 5808 |
A member or a member template may be nested within many enclosing class
|
| 5809 |
templates. In an explicit specialization for such a member, the member
|
| 5810 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 5811 |
template that is explicitly specialized.
|
| 5812 |
|
| 5813 |
+
[*Example 9*:
|
| 5814 |
|
| 5815 |
``` cpp
|
| 5816 |
template<class T1> class A {
|
| 5817 |
template<class T2> class B {
|
| 5818 |
void mf();
|
|
|
|
| 5834 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 5835 |
preceding the explicit specialization declaration of the member. The
|
| 5836 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 5837 |
shall be the same as those specified in the primary template definition.
|
| 5838 |
|
| 5839 |
+
[*Example 10*:
|
| 5840 |
|
| 5841 |
``` cpp
|
| 5842 |
template <class T1> class A {
|
| 5843 |
template<class T2> class B {
|
| 5844 |
template<class T3> void mf1(T3);
|
|
|
|
| 5871 |
- the explicit specialization of a function template;
|
| 5872 |
- the explicit specialization of a member function template;
|
| 5873 |
- the explicit specialization of a member function of a class template
|
| 5874 |
where the class template specialization to which the member function
|
| 5875 |
specialization belongs is implicitly instantiated. \[*Note 4*: Default
|
| 5876 |
+
function arguments can be specified in the declaration or definition
|
| 5877 |
of a member function of a class template specialization that is
|
| 5878 |
explicitly specialized. — *end note*]
|
| 5879 |
|
| 5880 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
| 5881 |
|
| 5882 |
+
### General <a id="temp.fct.spec.general">[[temp.fct.spec.general]]</a>
|
| 5883 |
+
|
| 5884 |
A function instantiated from a function template is called a function
|
| 5885 |
template specialization; so is an explicit specialization of a function
|
| 5886 |
template. Template arguments can be explicitly specified when naming the
|
| 5887 |
function template specialization, deduced from the context (e.g.,
|
| 5888 |
deduced from the function arguments in a call to the function template
|
|
|
|
| 5940 |
```
|
| 5941 |
|
| 5942 |
— *end example*]
|
| 5943 |
|
| 5944 |
Template arguments shall not be specified when referring to a
|
| 5945 |
+
specialization of a constructor template [[class.ctor]], [[class.qual]].
|
|
|
|
| 5946 |
|
| 5947 |
A template argument list may be specified when referring to a
|
| 5948 |
specialization of a function template
|
| 5949 |
|
| 5950 |
- when a function is called,
|
|
|
|
| 5954 |
- in an explicit instantiation, or
|
| 5955 |
- in a friend declaration.
|
| 5956 |
|
| 5957 |
Trailing template arguments that can be deduced [[temp.deduct]] or
|
| 5958 |
obtained from default *template-argument*s may be omitted from the list
|
| 5959 |
+
of explicit *template-argument*s.
|
| 5960 |
+
|
| 5961 |
+
[*Note 1*: A trailing template parameter pack [[temp.variadic]] not
|
| 5962 |
+
otherwise deduced will be deduced as an empty sequence of template
|
| 5963 |
+
arguments. — *end note*]
|
| 5964 |
+
|
| 5965 |
+
If all of the template arguments can be deduced or obtained from default
|
| 5966 |
+
*template-argument*s, they may all be omitted; in this case, the empty
|
| 5967 |
+
template argument list `<>` itself may also be omitted.
|
|
|
|
| 5968 |
|
| 5969 |
[*Example 2*:
|
| 5970 |
|
| 5971 |
``` cpp
|
| 5972 |
template<class X, class Y> X f(Y);
|
|
|
|
| 5982 |
}
|
| 5983 |
```
|
| 5984 |
|
| 5985 |
— *end example*]
|
| 5986 |
|
| 5987 |
+
[*Note 2*:
|
| 5988 |
|
| 5989 |
An empty template argument list can be used to indicate that a given use
|
| 5990 |
refers to a specialization of a function template even when a
|
| 5991 |
non-template function [[dcl.fct]] is visible that would otherwise be
|
| 5992 |
used. For example:
|
|
|
|
| 6025 |
Implicit conversions [[conv]] will be performed on a function argument
|
| 6026 |
to convert it to the type of the corresponding function parameter if the
|
| 6027 |
parameter type contains no *template-parameter*s that participate in
|
| 6028 |
template argument deduction.
|
| 6029 |
|
| 6030 |
+
[*Note 3*:
|
| 6031 |
|
| 6032 |
Template parameters do not participate in template argument deduction if
|
| 6033 |
they are explicitly specified. For example,
|
| 6034 |
|
| 6035 |
``` cpp
|
|
|
|
| 6044 |
}
|
| 6045 |
```
|
| 6046 |
|
| 6047 |
— *end note*]
|
| 6048 |
|
| 6049 |
+
[*Note 4*: Because the explicit template argument list follows the
|
| 6050 |
function template name, and because constructor templates [[class.ctor]]
|
| 6051 |
are named without using a function name [[class.qual]], there is no way
|
| 6052 |
to provide an explicit template argument list for these function
|
| 6053 |
templates. — *end note*]
|
| 6054 |
|
|
|
|
| 6068 |
|
| 6069 |
— *end example*]
|
| 6070 |
|
| 6071 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 6072 |
|
| 6073 |
+
#### General <a id="temp.deduct.general">[[temp.deduct.general]]</a>
|
| 6074 |
+
|
| 6075 |
When a function template specialization is referenced, all of the
|
| 6076 |
template arguments shall have values. The values can be explicitly
|
| 6077 |
specified or, in some cases, be deduced from the use or obtained from
|
| 6078 |
default *template-argument*s.
|
| 6079 |
|
|
|
|
| 6168 |
|
| 6169 |
— *end example*]
|
| 6170 |
|
| 6171 |
When all template arguments have been deduced or obtained from default
|
| 6172 |
template arguments, all uses of template parameters in the template
|
| 6173 |
+
parameter list of the template are replaced with the corresponding
|
| 6174 |
+
deduced or default argument values. If the substitution results in an
|
| 6175 |
+
invalid type, as described above, type deduction fails. If the function
|
| 6176 |
+
template has associated constraints [[temp.constr.decl]], those
|
| 6177 |
+
constraints are checked for satisfaction [[temp.constr.constr]]. If the
|
| 6178 |
+
constraints are not satisfied, type deduction fails. In the context of a
|
| 6179 |
+
function call, if type deduction has not yet failed, then for those
|
| 6180 |
+
function parameters for which the function call has arguments, each
|
| 6181 |
+
function parameter with a type that was non-dependent before
|
| 6182 |
+
substitution of any explicitly-specified template arguments is checked
|
| 6183 |
+
against its corresponding argument; if the corresponding argument cannot
|
| 6184 |
+
be implicitly converted to the parameter type, type deduction fails.
|
| 6185 |
+
|
| 6186 |
+
[*Note 3*: Overload resolution will check the other parameters,
|
| 6187 |
+
including parameters with dependent types in which no template
|
| 6188 |
+
parameters participate in template argument deduction and parameters
|
| 6189 |
+
that became non-dependent due to substitution of explicitly-specified
|
| 6190 |
+
template arguments. — *end note*]
|
| 6191 |
+
|
| 6192 |
+
If type deduction has not yet failed, then all uses of template
|
| 6193 |
+
parameters in the function type are replaced with the corresponding
|
| 6194 |
+
deduced or default argument values. If the substitution results in an
|
| 6195 |
+
invalid type, as described above, type deduction fails.
|
| 6196 |
+
|
| 6197 |
+
[*Example 5*:
|
| 6198 |
+
|
| 6199 |
+
``` cpp
|
| 6200 |
+
template <class T> struct Z {
|
| 6201 |
+
typedef typename T::x xx;
|
| 6202 |
+
};
|
| 6203 |
+
template <class T> concept C = requires { typename T::A; };
|
| 6204 |
+
template <C T> typename Z<T>::xx f(void *, T); // #1
|
| 6205 |
+
template <class T> void f(int, T); // #2
|
| 6206 |
+
struct A {} a;
|
| 6207 |
+
struct ZZ {
|
| 6208 |
+
template <class T, class = typename Z<T>::xx> operator T *();
|
| 6209 |
+
operator int();
|
| 6210 |
+
};
|
| 6211 |
+
int main() {
|
| 6212 |
+
ZZ zz;
|
| 6213 |
+
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 6214 |
+
f(zz, 42); // OK, deduction fails for #1 because C<int> is not satisfied
|
| 6215 |
+
}
|
| 6216 |
+
```
|
| 6217 |
+
|
| 6218 |
+
— *end example*]
|
| 6219 |
|
| 6220 |
At certain points in the template argument deduction process it is
|
| 6221 |
necessary to take a function type that makes use of template parameters
|
| 6222 |
and replace those template parameters with the corresponding template
|
| 6223 |
arguments. This is done at the beginning of template argument deduction
|
| 6224 |
when any explicitly specified template arguments are substituted into
|
| 6225 |
the function type, and again at the end of template argument deduction
|
| 6226 |
when any template arguments that were deduced or obtained from default
|
| 6227 |
arguments are substituted.
|
| 6228 |
|
| 6229 |
+
The *deduction substitution loci* are
|
| 6230 |
+
|
| 6231 |
+
- the function type outside of the *noexcept-specifier*,
|
| 6232 |
+
- the *explicit-specifier*, and
|
| 6233 |
+
- the template parameter declarations.
|
| 6234 |
+
|
| 6235 |
The substitution occurs in all types and expressions that are used in
|
| 6236 |
+
the deduction substitution loci. The expressions include not only
|
| 6237 |
+
constant expressions such as those that appear in array bounds or as
|
| 6238 |
+
nontype template arguments but also general expressions (i.e.,
|
| 6239 |
+
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 6240 |
+
contexts that allow non-constant expressions. The substitution proceeds
|
| 6241 |
+
in lexical order and stops when a condition that causes deduction to
|
| 6242 |
+
fail is encountered. If substitution into different declarations of the
|
| 6243 |
+
same function template would cause template instantiations to occur in a
|
| 6244 |
+
different order or not at all, the program is ill-formed; no diagnostic
|
| 6245 |
+
required.
|
| 6246 |
|
| 6247 |
+
[*Note 4*: The equivalent substitution in exception specifications is
|
| 6248 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 6249 |
a program is ill-formed if the substitution results in an invalid type
|
| 6250 |
or expression. — *end note*]
|
| 6251 |
|
| 6252 |
+
[*Example 6*:
|
| 6253 |
|
| 6254 |
``` cpp
|
| 6255 |
template <class T> struct A { using X = typename T::X; };
|
| 6256 |
template <class T> typename T::X f(typename A<T>::X);
|
| 6257 |
template <class T> void f(...) { }
|
|
|
|
| 6270 |
|
| 6271 |
— *end example*]
|
| 6272 |
|
| 6273 |
If a substitution results in an invalid type or expression, type
|
| 6274 |
deduction fails. An invalid type or expression is one that would be
|
| 6275 |
+
ill-formed, with a diagnostic required, if written in the same context
|
| 6276 |
+
using the substituted arguments.
|
| 6277 |
|
| 6278 |
+
[*Note 5*: If no diagnostic is required, the program is still
|
| 6279 |
ill-formed. Access checking is done as part of the substitution
|
| 6280 |
process. — *end note*]
|
| 6281 |
|
| 6282 |
+
Invalid types and expressions can result in a deduction failure only in
|
| 6283 |
+
the immediate context of the deduction substitution loci.
|
|
|
|
| 6284 |
|
| 6285 |
+
[*Note 6*: The substitution into types and expressions can result in
|
| 6286 |
effects such as the instantiation of class template specializations
|
| 6287 |
and/or function template specializations, the generation of
|
| 6288 |
implicitly-defined functions, etc. Such effects are not in the
|
| 6289 |
“immediate context” and can result in the program being
|
| 6290 |
ill-formed. — *end note*]
|
| 6291 |
|
| 6292 |
A *lambda-expression* appearing in a function type or a template
|
| 6293 |
parameter is not considered part of the immediate context for the
|
| 6294 |
purposes of template argument deduction.
|
| 6295 |
|
| 6296 |
+
[*Note 7*:
|
| 6297 |
|
| 6298 |
The intent is to avoid requiring implementations to deal with
|
| 6299 |
substitution failure involving arbitrary statements.
|
| 6300 |
|
| 6301 |
+
[*Example 7*:
|
| 6302 |
|
| 6303 |
``` cpp
|
| 6304 |
template <class T>
|
| 6305 |
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 6306 |
void f(...);
|
|
|
|
| 6329 |
|
| 6330 |
— *end example*]
|
| 6331 |
|
| 6332 |
— *end note*]
|
| 6333 |
|
| 6334 |
+
[*Example 8*:
|
| 6335 |
|
| 6336 |
``` cpp
|
| 6337 |
struct X { };
|
| 6338 |
struct Y {
|
| 6339 |
Y(X) {}
|
|
|
|
| 6346 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 6347 |
```
|
| 6348 |
|
| 6349 |
— *end example*]
|
| 6350 |
|
| 6351 |
+
[*Note 8*:
|
| 6352 |
|
| 6353 |
+
Type deduction can fail for the following reasons:
|
| 6354 |
|
| 6355 |
- Attempting to instantiate a pack expansion containing multiple packs
|
| 6356 |
of differing lengths.
|
| 6357 |
- Attempting to create an array with an element type that is `void`, a
|
| 6358 |
function type, or a reference type, or attempting to create an array
|
| 6359 |
with a size that is zero or negative.
|
| 6360 |
+
\[*Example 9*:
|
| 6361 |
``` cpp
|
| 6362 |
template <class T> int f(T[5]);
|
| 6363 |
int I = f<int>(0);
|
| 6364 |
int j = f<void>(0); // invalid array
|
| 6365 |
```
|
| 6366 |
|
| 6367 |
— *end example*]
|
| 6368 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 6369 |
qualified name.
|
| 6370 |
+
\[*Example 10*:
|
| 6371 |
``` cpp
|
| 6372 |
template <class T> int f(typename T::B*);
|
| 6373 |
int i = f<int>(0);
|
| 6374 |
```
|
| 6375 |
|
|
|
|
| 6380 |
- the specified member is not a type where a type is required, or
|
| 6381 |
- the specified member is not a template where a template is required,
|
| 6382 |
or
|
| 6383 |
- the specified member is not a non-type where a non-type is required.
|
| 6384 |
|
| 6385 |
+
\[*Example 11*:
|
| 6386 |
``` cpp
|
| 6387 |
template <int I> struct X { };
|
| 6388 |
template <template <class T> class> struct Z { };
|
| 6389 |
template <class T> void f(typename T::Y*) {}
|
| 6390 |
template <class T> void g(X<T::N>*) {}
|
| 6391 |
+
template <class T> void h(Z<T::TT>*) {}
|
| 6392 |
struct A {};
|
| 6393 |
struct B { int Y; };
|
| 6394 |
struct C {
|
| 6395 |
typedef int N;
|
| 6396 |
};
|
|
|
|
| 6410 |
— *end example*]
|
| 6411 |
- Attempting to create a pointer to reference type.
|
| 6412 |
- Attempting to create a reference to `void`.
|
| 6413 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 6414 |
class type.
|
| 6415 |
+
\[*Example 12*:
|
| 6416 |
``` cpp
|
| 6417 |
template <class T> int f(int T::*);
|
| 6418 |
int i = f<int>(0);
|
| 6419 |
```
|
| 6420 |
|
| 6421 |
— *end example*]
|
| 6422 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 6423 |
+
\[*Example 13*:
|
| 6424 |
``` cpp
|
| 6425 |
template <class T, T> struct S {};
|
| 6426 |
+
template <class T> int f(S<T, T{}>*); // #1
|
| 6427 |
+
class X {
|
| 6428 |
+
int m;
|
| 6429 |
+
};
|
| 6430 |
+
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a non-type template argument
|
| 6431 |
```
|
| 6432 |
|
| 6433 |
— *end example*]
|
| 6434 |
- Attempting to perform an invalid conversion in either a template
|
| 6435 |
argument expression, or an expression used in the function
|
| 6436 |
declaration.
|
| 6437 |
+
\[*Example 14*:
|
| 6438 |
``` cpp
|
| 6439 |
template <class T, T*> int f(int);
|
| 6440 |
+
int i2 = f<int,1>(0); // can't convert 1 to int*
|
| 6441 |
```
|
| 6442 |
|
| 6443 |
— *end example*]
|
| 6444 |
- Attempting to create a function type in which a parameter has a type
|
| 6445 |
of `void`, or in which the return type is a function type or array
|
| 6446 |
type.
|
| 6447 |
|
| 6448 |
— *end note*]
|
| 6449 |
|
| 6450 |
+
[*Example 15*:
|
| 6451 |
|
| 6452 |
In the following example, assuming a `signed char` cannot represent the
|
| 6453 |
value 1000, a narrowing conversion [[dcl.init.list]] would be required
|
| 6454 |
to convert the *template-argument* of type `int` to `signed char`,
|
| 6455 |
therefore substitution fails for the second template
|
|
|
|
| 6635 |
template <typename... T> struct X;
|
| 6636 |
template <> struct X<> {};
|
| 6637 |
template <typename T, typename... Ts>
|
| 6638 |
struct X<T, Ts...> : X<Ts...> {};
|
| 6639 |
struct D : X<int> {};
|
| 6640 |
+
struct E : X<>, X<int> {};
|
| 6641 |
|
| 6642 |
template <typename... T>
|
| 6643 |
int f(const X<T...>&);
|
| 6644 |
int x = f(D()); // calls f<int>, not f<>
|
| 6645 |
// B is X<>, C is X<int>
|
| 6646 |
+
int z = f(E()); // calls f<int>, not f<>
|
| 6647 |
```
|
| 6648 |
|
| 6649 |
— *end example*]
|
| 6650 |
|
| 6651 |
These alternatives are considered only if type deduction would otherwise
|
|
|
|
| 6704 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 6705 |
```
|
| 6706 |
|
| 6707 |
— *end example*]
|
| 6708 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6709 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 6710 |
|
| 6711 |
Template arguments can be deduced from the type specified when taking
|
| 6712 |
+
the address of an overload set [[over.over]]. If there is a target, the
|
| 6713 |
+
function template’s function type and the target type are used as the
|
| 6714 |
+
types of `P` and `A`, and the deduction is done as described in
|
| 6715 |
+
[[temp.deduct.type]]. Otherwise, deduction is performed with empty sets
|
| 6716 |
+
of types `P` and `A`.
|
| 6717 |
|
| 6718 |
A placeholder type [[dcl.spec.auto]] in the return type of a function
|
| 6719 |
template is a non-deduced context. If template argument deduction
|
| 6720 |
succeeds for such a function, the return type is determined from
|
| 6721 |
instantiation of the function body.
|
| 6722 |
|
| 6723 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 6724 |
|
| 6725 |
Template argument deduction is done by comparing the return type of the
|
| 6726 |
+
conversion function template (call it `P`) with the type specified by
|
| 6727 |
+
the *conversion-type-id* of the *conversion-function-id* being looked up
|
| 6728 |
+
(call it `A`) as described in [[temp.deduct.type]]. If the
|
| 6729 |
+
*conversion-function-id* is constructed during overload resolution
|
| 6730 |
+
[[over.match.funcs]], the rules in the remainder of this subclause
|
| 6731 |
+
apply.
|
| 6732 |
|
| 6733 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 6734 |
of `P` for type deduction and for any further references to or
|
| 6735 |
transformations of `P` in the remainder of this subclause.
|
| 6736 |
|
|
|
|
| 6748 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 6749 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 6750 |
referred to by `A` is used for type deduction.
|
| 6751 |
|
| 6752 |
In general, the deduction process attempts to find template argument
|
| 6753 |
+
values that will make the deduced `A` identical to `A`. However, certain
|
| 6754 |
+
attributes of `A` may be ignored:
|
| 6755 |
|
| 6756 |
+
- If the original `A` is a reference type, any cv-qualifiers of `A`
|
| 6757 |
+
(i.e., the type referred to by the reference).
|
| 6758 |
+
- If the original `A` is a function pointer or
|
| 6759 |
+
pointer-to-member-function type with a potentially-throwing exception
|
| 6760 |
+
specification [[except.spec]], the exception specification.
|
| 6761 |
+
- Any cv-qualifiers in `A` that can be restored by a qualification
|
| 6762 |
+
conversion.
|
|
|
|
|
|
|
| 6763 |
|
| 6764 |
+
These attributes are ignored only if type deduction would otherwise
|
| 6765 |
+
fail. If ignoring them allows more than one possible deduced `A`, the
|
| 6766 |
+
type deduction fails.
|
| 6767 |
|
| 6768 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 6769 |
|
| 6770 |
Template argument deduction is done by comparing certain types
|
| 6771 |
associated with the two function templates being compared.
|
|
|
|
| 6787 |
|
| 6788 |
The types used to determine the ordering depend on the context in which
|
| 6789 |
the partial ordering is done:
|
| 6790 |
|
| 6791 |
- In the context of a function call, the types used are those function
|
| 6792 |
+
parameter types for which the function call has arguments.[^13]
|
| 6793 |
- In the context of a call to a conversion function, the return types of
|
| 6794 |
the conversion function templates are used.
|
| 6795 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 6796 |
type is used.
|
| 6797 |
|
|
|
|
| 6932 |
array bound if it is not otherwise deduced.
|
| 6933 |
|
| 6934 |
A given type `P` can be composed from a number of other types,
|
| 6935 |
templates, and non-type values:
|
| 6936 |
|
| 6937 |
+
- A function type includes the types of each of the function parameters,
|
| 6938 |
+
the return type, and its exception specification.
|
| 6939 |
- A pointer-to-member type includes the type of the class object pointed
|
| 6940 |
to and the type of the member pointed to.
|
| 6941 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 6942 |
includes the types, templates, and non-type values referenced by the
|
| 6943 |
template argument list of the specialization.
|
|
|
|
| 7010 |
``` cpp
|
| 7011 |
template<class T> void f(T x, T y) { ... }
|
| 7012 |
struct A { ... };
|
| 7013 |
struct B : A { ... };
|
| 7014 |
void g(A a, B b) {
|
| 7015 |
+
f(a,b); // error: T deduced as both A and B
|
| 7016 |
+
f(b,a); // error: T deduced as both A and B
|
| 7017 |
+
f(a,a); // OK, T is A
|
| 7018 |
+
f(b,b); // OK, T is B
|
| 7019 |
}
|
| 7020 |
```
|
| 7021 |
|
| 7022 |
Here is an example where two template arguments are deduced from a
|
| 7023 |
single function parameter/argument pair. This can lead to conflicts that
|
|
|
|
| 7029 |
int g1( int, float, float);
|
| 7030 |
char g2( int, float, float);
|
| 7031 |
int g3( int, char, float);
|
| 7032 |
|
| 7033 |
void r() {
|
| 7034 |
+
f(g1); // OK, T is int and U is float
|
| 7035 |
+
f(g2); // error: T deduced as both char and int
|
| 7036 |
+
f(g3); // error: U deduced as both char and float
|
| 7037 |
+
}
|
| 7038 |
+
```
|
| 7039 |
+
|
| 7040 |
+
Here is an example where the exception specification of a function type
|
| 7041 |
+
is deduced:
|
| 7042 |
+
|
| 7043 |
+
``` cpp
|
| 7044 |
+
template<bool E> void f1(void (*)() noexcept(E));
|
| 7045 |
+
template<bool> struct A { };
|
| 7046 |
+
template<bool B> void f2(void (*)(A<B>) noexcept(B));
|
| 7047 |
+
|
| 7048 |
+
void g1();
|
| 7049 |
+
void g2() noexcept;
|
| 7050 |
+
void g3(A<true>);
|
| 7051 |
+
|
| 7052 |
+
void h() {
|
| 7053 |
+
f1(g1); // OK, E is false
|
| 7054 |
+
f1(g2); // OK, E is true
|
| 7055 |
+
f2(g3); // error: B deduced as both true and false
|
| 7056 |
}
|
| 7057 |
```
|
| 7058 |
|
| 7059 |
Here is an example where a qualification conversion applies between the
|
| 7060 |
argument type on the function call and the deduced template argument
|
|
|
|
| 7084 |
}
|
| 7085 |
```
|
| 7086 |
|
| 7087 |
— *end example*]
|
| 7088 |
|
| 7089 |
+
A template type argument `T`, a template template argument `TT`, or a
|
| 7090 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 7091 |
the following forms:
|
| 7092 |
|
| 7093 |
``` cpp
|
| 7094 |
+
\opt{cv} T
|
|
|
|
| 7095 |
T*
|
| 7096 |
T&
|
| 7097 |
T&&
|
| 7098 |
+
\opt{T}[\opt{i}]
|
| 7099 |
+
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 7100 |
+
\opt{T} \opt{T}::*
|
| 7101 |
+
\opt{TT}<T>
|
| 7102 |
+
\opt{TT}<i>
|
| 7103 |
+
\opt{TT}<TT>
|
| 7104 |
+
\opt{TT}<>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7105 |
```
|
| 7106 |
|
| 7107 |
+
where
|
| 7108 |
+
|
| 7109 |
+
- `\opt{T}` represents a type or parameter-type-list that either
|
| 7110 |
+
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 7111 |
+
`A`, or is the same non-dependent type in `P` and `A`,
|
| 7112 |
+
- `\opt{TT}` represents either a class template or a template template
|
| 7113 |
+
parameter,
|
| 7114 |
+
- `\opt{i}` represents an expression that either is an `i`, is
|
| 7115 |
+
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 7116 |
+
and `A`, and
|
| 7117 |
+
- `noexcept(\opt{i})` represents an exception specification
|
| 7118 |
+
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 7119 |
+
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 7120 |
+
above.
|
| 7121 |
+
|
| 7122 |
+
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 7123 |
+
`TT`s, deduction is not possible. — *end note*]
|
| 7124 |
+
|
| 7125 |
+
Similarly, `<T>` represents template argument lists where at least one
|
| 7126 |
+
argument contains a `T`, `<i>` represents template argument lists where
|
| 7127 |
+
at least one argument contains an `i` and `<>` represents template
|
| 7128 |
+
argument lists where no argument contains a `T` or an `i`.
|
| 7129 |
|
| 7130 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 7131 |
the respective template argument list of `P` is compared with the
|
| 7132 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 7133 |
`A`. If the template argument list of `P` contains a pack expansion that
|
|
|
|
| 7171 |
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 7172 |
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 7173 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 7174 |
is changed to simply `T`).
|
| 7175 |
|
| 7176 |
+
[*Note 3*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 7177 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 7178 |
`X&`. — *end note*]
|
| 7179 |
|
| 7180 |
[*Example 5*:
|
| 7181 |
|
|
|
|
| 7278 |
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 7279 |
```
|
| 7280 |
|
| 7281 |
— *end example*]
|
| 7282 |
|
| 7283 |
+
The type of `B` in the *noexcept-specifier* `noexcept(B)` of a function
|
| 7284 |
+
type is `bool`.
|
| 7285 |
+
|
| 7286 |
[*Example 10*:
|
| 7287 |
|
| 7288 |
+
``` cpp
|
| 7289 |
+
template<bool> struct A { };
|
| 7290 |
+
template<auto> struct B;
|
| 7291 |
+
template<auto X, void (*F)() noexcept(X)> struct B<F> {
|
| 7292 |
+
A<X> ax;
|
| 7293 |
+
};
|
| 7294 |
+
void f_nothrow() noexcept;
|
| 7295 |
+
B<f_nothrow> bn; // OK, type of X deduced as bool
|
| 7296 |
+
```
|
| 7297 |
+
|
| 7298 |
+
— *end example*]
|
| 7299 |
+
|
| 7300 |
+
[*Example 11*:
|
| 7301 |
+
|
| 7302 |
``` cpp
|
| 7303 |
template<class T, T i> void f(int (&a)[i]);
|
| 7304 |
int v[10];
|
| 7305 |
void g() {
|
| 7306 |
+
f(v); // OK, T is std::size_t
|
| 7307 |
}
|
| 7308 |
```
|
| 7309 |
|
| 7310 |
— *end example*]
|
| 7311 |
|
| 7312 |
+
[*Note 4*:
|
| 7313 |
|
| 7314 |
Except for reference and pointer types, a major array bound is not part
|
| 7315 |
of a function parameter type and cannot be deduced from an argument:
|
| 7316 |
|
| 7317 |
``` cpp
|
|
|
|
| 7319 |
template<int i> void f2(int a[i][20]);
|
| 7320 |
template<int i> void f3(int (&a)[i][20]);
|
| 7321 |
|
| 7322 |
void g() {
|
| 7323 |
int v[10][20];
|
| 7324 |
+
f1(v); // OK, i deduced as 20
|
| 7325 |
f1<20>(v); // OK
|
| 7326 |
f2(v); // error: cannot deduce template-argument i
|
| 7327 |
f2<10>(v); // OK
|
| 7328 |
+
f3(v); // OK, i deduced as 10
|
| 7329 |
}
|
| 7330 |
```
|
| 7331 |
|
| 7332 |
— *end note*]
|
| 7333 |
|
| 7334 |
+
[*Note 5*:
|
| 7335 |
|
| 7336 |
If, in the declaration of a function template with a non-type template
|
| 7337 |
parameter, the non-type template parameter is used in a subexpression in
|
| 7338 |
the function parameter list, the expression is a non-deduced context as
|
| 7339 |
specified above.
|
| 7340 |
|
| 7341 |
+
[*Example 12*:
|
| 7342 |
|
| 7343 |
``` cpp
|
| 7344 |
template <int i> class A { ... };
|
| 7345 |
template <int i> void g(A<i+1>);
|
| 7346 |
template <int i> void f(A<i>, A<i+1>);
|
|
|
|
| 7355 |
|
| 7356 |
— *end example*]
|
| 7357 |
|
| 7358 |
— *end note*]
|
| 7359 |
|
| 7360 |
+
[*Note 6*:
|
| 7361 |
|
| 7362 |
Template parameters do not participate in template argument deduction if
|
| 7363 |
they are used only in non-deduced contexts. For example,
|
| 7364 |
|
| 7365 |
``` cpp
|
|
|
|
| 7379 |
|
| 7380 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 7381 |
from the type of the corresponding template parameter of the template
|
| 7382 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 7383 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 7384 |
+
type, deduction fails.[^14]
|
| 7385 |
|
| 7386 |
+
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 7387 |
+
`bool`, deduction fails.
|
| 7388 |
+
|
| 7389 |
+
[*Example 13*:
|
| 7390 |
|
| 7391 |
``` cpp
|
| 7392 |
template<int i> class A { ... };
|
| 7393 |
template<short s> void f(A<s>);
|
| 7394 |
void k1() {
|
|
|
|
| 7399 |
|
| 7400 |
template<const short cs> class B { };
|
| 7401 |
template<short s> void g(B<s>);
|
| 7402 |
void k2() {
|
| 7403 |
B<1> b;
|
| 7404 |
+
g(b); // OK, cv-qualifiers are ignored on template parameter types
|
| 7405 |
}
|
| 7406 |
```
|
| 7407 |
|
| 7408 |
— *end example*]
|
| 7409 |
|
| 7410 |
A *template-argument* can be deduced from a function, pointer to
|
| 7411 |
function, or pointer-to-member-function type.
|
| 7412 |
|
| 7413 |
+
[*Example 14*:
|
| 7414 |
|
| 7415 |
``` cpp
|
| 7416 |
template<class T> void f(void(*)(T,int));
|
| 7417 |
template<class T> void foo(T,int);
|
| 7418 |
void g(int,int);
|
|
|
|
| 7420 |
|
| 7421 |
void h(int,int,int);
|
| 7422 |
void h(char,int);
|
| 7423 |
int m() {
|
| 7424 |
f(&g); // error: ambiguous
|
| 7425 |
+
f(&h); // OK, void h(char,int) is a unique match
|
| 7426 |
f(&foo); // error: type deduction fails because foo is a template
|
| 7427 |
}
|
| 7428 |
```
|
| 7429 |
|
| 7430 |
— *end example*]
|
| 7431 |
|
| 7432 |
A template *type-parameter* cannot be deduced from the type of a
|
| 7433 |
function default argument.
|
| 7434 |
|
| 7435 |
+
[*Example 15*:
|
| 7436 |
|
| 7437 |
``` cpp
|
| 7438 |
template <class T> void f(T = 5, T = 7);
|
| 7439 |
void g() {
|
| 7440 |
+
f(1); // OK, calls f<int>(1,7)
|
| 7441 |
f(); // error: cannot deduce T
|
| 7442 |
+
f<int>(); // OK, calls f<int>(5,7)
|
| 7443 |
}
|
| 7444 |
```
|
| 7445 |
|
| 7446 |
— *end example*]
|
| 7447 |
|
| 7448 |
The *template-argument* corresponding to a template *template-parameter*
|
| 7449 |
is deduced from the type of the *template-argument* of a class template
|
| 7450 |
specialization used in the argument list of a function call.
|
| 7451 |
|
| 7452 |
+
[*Example 16*:
|
| 7453 |
|
| 7454 |
``` cpp
|
| 7455 |
template <template <class T> class X> struct A { };
|
| 7456 |
template <template <class T> class X> void f(A<X>) { }
|
| 7457 |
template<class T> struct B { };
|
|
|
|
| 7459 |
f(ab); // calls f(A<B>)
|
| 7460 |
```
|
| 7461 |
|
| 7462 |
— *end example*]
|
| 7463 |
|
| 7464 |
+
[*Note 7*: Template argument deduction involving parameter packs
|
| 7465 |
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 7466 |
pack. — *end note*]
|
| 7467 |
|
| 7468 |
+
[*Example 17*:
|
| 7469 |
|
| 7470 |
``` cpp
|
| 7471 |
template<class> struct X { };
|
| 7472 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 7473 |
template<class ... Types> struct Y { };
|
|
|
|
| 7477 |
void g(int, float);
|
| 7478 |
|
| 7479 |
X<int> x1; // uses primary template
|
| 7480 |
X<int(int, float, double)> x2; // uses partial specialization; ArgTypes contains float, double
|
| 7481 |
X<int(float, int)> x3; // uses primary template
|
| 7482 |
+
Y<> y1; // uses primary template; Types is empty
|
| 7483 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 7484 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 7485 |
int fv = f(g); // OK; Types contains int, float
|
| 7486 |
```
|
| 7487 |
|
|
|
|
| 7493 |
function template, template argument deduction is performed to identify
|
| 7494 |
the specialization to which the declaration refers. Specifically, this
|
| 7495 |
is done for explicit instantiations [[temp.explicit]], explicit
|
| 7496 |
specializations [[temp.expl.spec]], and certain friend declarations
|
| 7497 |
[[temp.friend]]. This is also done to determine whether a deallocation
|
| 7498 |
+
function template specialization matches a placement `operator new`
|
| 7499 |
+
[[basic.stc.dynamic.deallocation]], [[expr.new]]. In all these cases,
|
| 7500 |
`P` is the type of the function template being considered as a potential
|
| 7501 |
match and `A` is either the function type from the declaration or the
|
| 7502 |
type of the deallocation function that would match the placement
|
| 7503 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 7504 |
described in [[temp.deduct.type]].
|
|
|
|
| 7508 |
[[temp.func.order]], deduction fails and, in the declaration cases, the
|
| 7509 |
program is ill-formed.
|
| 7510 |
|
| 7511 |
### Overload resolution <a id="temp.over">[[temp.over]]</a>
|
| 7512 |
|
| 7513 |
+
When a call of a function or function template is written (explicitly,
|
| 7514 |
+
or implicitly using the operator notation), template argument deduction
|
| 7515 |
+
[[temp.deduct]] and checking of any explicit template arguments
|
| 7516 |
+
[[temp.arg]] are performed for each function template to find the
|
| 7517 |
+
template argument values (if any) that can be used with that function
|
| 7518 |
+
template to instantiate a function template specialization that can be
|
| 7519 |
+
invoked with the call arguments or, for conversion function templates,
|
| 7520 |
+
that can convert to the required type. For each function template, if
|
| 7521 |
the argument deduction and checking succeeds, the *template-argument*s
|
| 7522 |
(deduced and/or explicit) are used to synthesize the declaration of a
|
| 7523 |
single function template specialization which is added to the candidate
|
| 7524 |
functions set to be used in overload resolution. If, for a given
|
| 7525 |
function template, argument deduction fails or the synthesized function
|
| 7526 |
template specialization would be ill-formed, no such function is added
|
| 7527 |
to the set of candidate functions for that template. The complete set of
|
| 7528 |
candidate functions includes all the synthesized declarations and all of
|
| 7529 |
+
the non-template functions found by name lookup. The synthesized
|
| 7530 |
declarations are treated like any other functions in the remainder of
|
| 7531 |
overload resolution, except as explicitly noted in
|
| 7532 |
+
[[over.match.best]].[^15]
|
| 7533 |
|
| 7534 |
[*Example 1*:
|
| 7535 |
|
| 7536 |
``` cpp
|
| 7537 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
|
|
| 7548 |
``` cpp
|
| 7549 |
int max(int,int);
|
| 7550 |
```
|
| 7551 |
|
| 7552 |
to the example above would resolve the third call, by providing a
|
| 7553 |
+
function that can be called for `max(a,c)` after using the standard
|
| 7554 |
conversion of `char` to `int` for `c`.
|
| 7555 |
|
| 7556 |
— *end example*]
|
| 7557 |
|
| 7558 |
[*Example 2*:
|
|
|
|
| 7602 |
|
| 7603 |
``` cpp
|
| 7604 |
template<class T> void f(T); // declaration
|
| 7605 |
|
| 7606 |
void g() {
|
| 7607 |
+
f("Annemarie"); // calls f<const char*>
|
| 7608 |
}
|
| 7609 |
```
|
| 7610 |
|
| 7611 |
+
The call to `f` is well-formed even if the template `f` is only declared
|
| 7612 |
and not defined at the point of the call. The program will be ill-formed
|
| 7613 |
+
unless a specialization for `f<const char*>` is explicitly instantiated
|
| 7614 |
+
in some translation unit [[temp.pre]].
|
| 7615 |
|
| 7616 |
— *end example*]
|
| 7617 |
|
| 7618 |
<!-- Link reference definitions -->
|
| 7619 |
[basic.def]: basic.md#basic.def
|
| 7620 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 7621 |
[basic.link]: basic.md#basic.link
|
| 7622 |
[basic.lookup]: basic.md#basic.lookup
|
| 7623 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
|
|
|
| 7624 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
|
|
|
|
|
|
|
|
|
| 7625 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 7626 |
+
[basic.scope.scope]: basic.md#basic.scope.scope
|
| 7627 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 7628 |
[basic.types]: basic.md#basic.types
|
| 7629 |
[class.access]: class.md#class.access
|
| 7630 |
[class.base.init]: class.md#class.base.init
|
| 7631 |
[class.conv.fct]: class.md#class.conv.fct
|
| 7632 |
[class.ctor]: class.md#class.ctor
|
| 7633 |
[class.default.ctor]: class.md#class.default.ctor
|
| 7634 |
[class.derived]: class.md#class.derived
|
| 7635 |
[class.dtor]: class.md#class.dtor
|
|
|
|
| 7636 |
[class.local]: class.md#class.local
|
| 7637 |
[class.mem]: class.md#class.mem
|
| 7638 |
+
[class.member.lookup]: basic.md#class.member.lookup
|
| 7639 |
[class.pre]: class.md#class.pre
|
| 7640 |
[class.qual]: basic.md#class.qual
|
| 7641 |
[class.temporary]: basic.md#class.temporary
|
| 7642 |
[conv]: expr.md#conv
|
| 7643 |
[conv.array]: expr.md#conv.array
|
|
|
|
| 7646 |
[conv.lval]: expr.md#conv.lval
|
| 7647 |
[conv.qual]: expr.md#conv.qual
|
| 7648 |
[dcl.align]: dcl.md#dcl.align
|
| 7649 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 7650 |
[dcl.decl]: dcl.md#dcl.decl
|
|
|
|
| 7651 |
[dcl.fct]: dcl.md#dcl.fct
|
| 7652 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 7653 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 7654 |
[dcl.init]: dcl.md#dcl.init
|
| 7655 |
[dcl.init.list]: dcl.md#dcl.init.list
|
|
|
|
| 7659 |
[dcl.stc]: dcl.md#dcl.stc
|
| 7660 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 7661 |
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 7662 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 7663 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 7664 |
+
[depr.template.template]: future.md#depr.template.template
|
| 7665 |
[except.spec]: except.md#except.spec
|
| 7666 |
[expr.const]: expr.md#expr.const
|
| 7667 |
[expr.context]: expr.md#expr.context
|
| 7668 |
[expr.log.and]: expr.md#expr.log.and
|
| 7669 |
[expr.log.or]: expr.md#expr.log.or
|
| 7670 |
[expr.new]: expr.md#expr.new
|
| 7671 |
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 7672 |
[expr.prim.id]: expr.md#expr.prim.id
|
| 7673 |
+
[expr.prim.id.qual]: expr.md#expr.prim.id.qual
|
| 7674 |
[expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
|
| 7675 |
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 7676 |
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 7677 |
+
[expr.prim.this]: expr.md#expr.prim.this
|
| 7678 |
[expr.ref]: expr.md#expr.ref
|
| 7679 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 7680 |
+
[expr.type.conv]: expr.md#expr.type.conv
|
| 7681 |
[expr.typeid]: expr.md#expr.typeid
|
| 7682 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 7683 |
[implimits]: limits.md#implimits
|
| 7684 |
[intro.defs]: intro.md#intro.defs
|
| 7685 |
[intro.object]: basic.md#intro.object
|
| 7686 |
[lex.string]: lex.md#lex.string
|
| 7687 |
+
[module.unit]: module.md#module.unit
|
|
|
|
| 7688 |
[namespace.udecl]: dcl.md#namespace.udecl
|
|
|
|
| 7689 |
[over.match]: over.md#over.match
|
| 7690 |
[over.match.best]: over.md#over.match.best
|
| 7691 |
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 7692 |
+
[over.match.funcs]: over.md#over.match.funcs
|
| 7693 |
[over.match.oper]: over.md#over.match.oper
|
|
|
|
| 7694 |
[over.match.viable]: over.md#over.match.viable
|
| 7695 |
[over.over]: over.md#over.over
|
| 7696 |
[special]: class.md#special
|
| 7697 |
[stmt.if]: stmt.md#stmt.if
|
| 7698 |
[support.types]: support.md#support.types
|
| 7699 |
[temp]: #temp
|
| 7700 |
[temp.alias]: #temp.alias
|
| 7701 |
[temp.arg]: #temp.arg
|
| 7702 |
[temp.arg.explicit]: #temp.arg.explicit
|
| 7703 |
+
[temp.arg.general]: #temp.arg.general
|
| 7704 |
[temp.arg.nontype]: #temp.arg.nontype
|
| 7705 |
[temp.arg.template]: #temp.arg.template
|
| 7706 |
[temp.arg.type]: #temp.arg.type
|
| 7707 |
[temp.class]: #temp.class
|
| 7708 |
+
[temp.class.general]: #temp.class.general
|
|
|
|
|
|
|
|
|
|
| 7709 |
[temp.concept]: #temp.concept
|
| 7710 |
[temp.constr]: #temp.constr
|
| 7711 |
[temp.constr.atomic]: #temp.constr.atomic
|
| 7712 |
[temp.constr.constr]: #temp.constr.constr
|
| 7713 |
+
[temp.constr.constr.general]: #temp.constr.constr.general
|
| 7714 |
[temp.constr.decl]: #temp.constr.decl
|
| 7715 |
+
[temp.constr.general]: #temp.constr.general
|
| 7716 |
[temp.constr.normal]: #temp.constr.normal
|
| 7717 |
[temp.constr.op]: #temp.constr.op
|
| 7718 |
[temp.constr.order]: #temp.constr.order
|
| 7719 |
[temp.decls]: #temp.decls
|
| 7720 |
+
[temp.decls.general]: #temp.decls.general
|
| 7721 |
[temp.deduct]: #temp.deduct
|
| 7722 |
[temp.deduct.call]: #temp.deduct.call
|
| 7723 |
[temp.deduct.conv]: #temp.deduct.conv
|
| 7724 |
[temp.deduct.decl]: #temp.deduct.decl
|
| 7725 |
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
| 7726 |
+
[temp.deduct.general]: #temp.deduct.general
|
| 7727 |
[temp.deduct.guide]: #temp.deduct.guide
|
| 7728 |
[temp.deduct.partial]: #temp.deduct.partial
|
| 7729 |
[temp.deduct.type]: #temp.deduct.type
|
| 7730 |
[temp.dep]: #temp.dep
|
| 7731 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 7732 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 7733 |
[temp.dep.expr]: #temp.dep.expr
|
| 7734 |
+
[temp.dep.general]: #temp.dep.general
|
| 7735 |
[temp.dep.res]: #temp.dep.res
|
| 7736 |
[temp.dep.temp]: #temp.dep.temp
|
| 7737 |
[temp.dep.type]: #temp.dep.type
|
| 7738 |
[temp.expl.spec]: #temp.expl.spec
|
| 7739 |
[temp.explicit]: #temp.explicit
|
| 7740 |
[temp.fct]: #temp.fct
|
| 7741 |
+
[temp.fct.general]: #temp.fct.general
|
| 7742 |
[temp.fct.spec]: #temp.fct.spec
|
| 7743 |
+
[temp.fct.spec.general]: #temp.fct.spec.general
|
| 7744 |
[temp.fold.empty]: #temp.fold.empty
|
| 7745 |
[temp.friend]: #temp.friend
|
| 7746 |
[temp.func.order]: #temp.func.order
|
|
|
|
| 7747 |
[temp.inst]: #temp.inst
|
| 7748 |
[temp.local]: #temp.local
|
| 7749 |
[temp.mem]: #temp.mem
|
| 7750 |
[temp.mem.class]: #temp.mem.class
|
| 7751 |
[temp.mem.enum]: #temp.mem.enum
|
| 7752 |
[temp.mem.func]: #temp.mem.func
|
| 7753 |
[temp.names]: #temp.names
|
|
|
|
| 7754 |
[temp.over]: #temp.over
|
| 7755 |
[temp.over.link]: #temp.over.link
|
| 7756 |
[temp.param]: #temp.param
|
| 7757 |
[temp.point]: #temp.point
|
| 7758 |
[temp.pre]: #temp.pre
|
| 7759 |
[temp.res]: #temp.res
|
| 7760 |
+
[temp.res.general]: #temp.res.general
|
| 7761 |
[temp.spec]: #temp.spec
|
| 7762 |
+
[temp.spec.general]: #temp.spec.general
|
| 7763 |
+
[temp.spec.partial]: #temp.spec.partial
|
| 7764 |
+
[temp.spec.partial.general]: #temp.spec.partial.general
|
| 7765 |
+
[temp.spec.partial.match]: #temp.spec.partial.match
|
| 7766 |
+
[temp.spec.partial.member]: #temp.spec.partial.member
|
| 7767 |
+
[temp.spec.partial.order]: #temp.spec.partial.order
|
| 7768 |
[temp.static]: #temp.static
|
| 7769 |
[temp.type]: #temp.type
|
| 7770 |
[temp.variadic]: #temp.variadic
|
| 7771 |
+
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 7772 |
+
[term.odr.use]: basic.md#term.odr.use
|
| 7773 |
|
| 7774 |
[^1]: Since template *template-parameter*s and template
|
| 7775 |
*template-argument*s are treated as types for descriptive purposes,
|
| 7776 |
the terms *non-type parameter* and *non-type argument* are used to
|
| 7777 |
refer to non-type, non-template parameters and arguments.
|
|
|
|
| 7785 |
because the form of the *template-parameter* determines the
|
| 7786 |
allowable forms of the *template-argument*.
|
| 7787 |
|
| 7788 |
[^4]: A constraint is in disjunctive normal form when it is a
|
| 7789 |
disjunction of clauses where each clause is a conjunction of atomic
|
| 7790 |
+
constraints. For atomic constraints A, B, and C, the disjunctive
|
|
|
|
|
|
|
| 7791 |
normal form of the constraint A ∧ (B ∨ C) is (A ∧ B) ∨ (A ∧ C). Its
|
| 7792 |
+
disjunctive clauses are (A ∧ B) and (A ∧ C).
|
| 7793 |
|
| 7794 |
[^5]: A constraint is in conjunctive normal form when it is a
|
| 7795 |
conjunction of clauses where each clause is a disjunction of atomic
|
| 7796 |
+
constraints. For atomic constraints A, B, and C, the constraint
|
|
|
|
|
|
|
| 7797 |
A ∧ (B ∨ C) is in conjunctive normal form. Its conjunctive clauses
|
| 7798 |
+
are A and (B ∨ C).
|
| 7799 |
|
| 7800 |
[^6]: The identity of enumerators is not preserved.
|
| 7801 |
|
| 7802 |
[^7]: An array as a *template-parameter* decays to a pointer.
|
| 7803 |
|
| 7804 |
+
[^8]: There is no context in which they would be used.
|
| 7805 |
|
| 7806 |
[^9]: That is, declarations of non-template functions do not merely
|
| 7807 |
guide overload resolution of function template specializations with
|
| 7808 |
the same name. If such a non-template function is odr-used
|
| 7809 |
+
[[term.odr.use]] in a program, it must be defined; it will not be
|
| 7810 |
implicitly instantiated using the function template definition.
|
| 7811 |
|
| 7812 |
[^10]: This includes friend function declarations.
|
| 7813 |
|
| 7814 |
+
[^11]: Every instantiation of a class template declares a different set
|
| 7815 |
+
of assignment operators.
|
| 7816 |
|
| 7817 |
+
[^12]: This includes an injected-class-name [[class.pre]] of a class
|
| 7818 |
+
template used without a *template-argument-list*.
|
| 7819 |
+
|
| 7820 |
+
[^13]: Default arguments are not considered to be arguments in this
|
| 7821 |
context; they only become arguments after a function has been
|
| 7822 |
selected.
|
| 7823 |
|
| 7824 |
+
[^14]: Although the *template-argument* corresponding to a
|
| 7825 |
+
*template-parameter* of type `bool` can be deduced from an array
|
| 7826 |
bound, the resulting value will always be `true` because the array
|
| 7827 |
bound will be nonzero.
|
| 7828 |
|
| 7829 |
+
[^15]: The parameters of function template specializations contain no
|
| 7830 |
template parameter types. The set of conversions allowed on deduced
|
| 7831 |
arguments is limited, because the argument deduction process
|
| 7832 |
produces function templates with parameters that either match the
|
| 7833 |
call arguments exactly or differ only in ways that can be bridged by
|
| 7834 |
the allowed limited conversions. Non-deduced arguments allow the
|