- tmp/tmpj6leptb4/{from.md → to.md} +277 -299
tmp/tmpj6leptb4/{from.md → to.md}
RENAMED
|
@@ -1,48 +1,48 @@
|
|
| 1 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
template
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
[*Note 1*: However, this syntax is allowed in class template partial
|
| 17 |
-
specializations [[temp.class.spec]]. — *end note*]
|
| 18 |
|
| 19 |
For purposes of name lookup and instantiation, default arguments,
|
| 20 |
*type-constraint*s, *requires-clause*s [[temp.pre]], and
|
| 21 |
*noexcept-specifier*s of function templates and of member functions of
|
| 22 |
class templates are considered definitions; each default argument,
|
| 23 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 24 |
separate definition which is unrelated to the templated function
|
| 25 |
-
definition or to any other default arguments *type-constraint*s,
|
| 26 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 27 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 28 |
are considered definitions.
|
| 29 |
|
| 30 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 31 |
possible to partially or explicitly specialize an alias template.
|
| 32 |
|
| 33 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 34 |
|
|
|
|
|
|
|
| 35 |
A *class template* defines the layout and operations for an unbounded
|
| 36 |
set of related types.
|
| 37 |
|
| 38 |
[*Example 1*:
|
| 39 |
|
| 40 |
-
|
| 41 |
-
definitions: one class `List<T>` for every type
|
| 42 |
-
linked list of elements of type `T`. Similarly, a
|
| 43 |
-
describing a contiguous, dynamic array
|
|
|
|
| 44 |
|
| 45 |
``` cpp
|
| 46 |
template<class T> class Array {
|
| 47 |
T* v;
|
| 48 |
int sz;
|
|
@@ -52,26 +52,25 @@ public:
|
|
| 52 |
T& elem(int i) { return v[i]; }
|
| 53 |
};
|
| 54 |
```
|
| 55 |
|
| 56 |
The prefix `template<class T>` specifies that a template is being
|
| 57 |
-
declared and that a *type-name* `T`
|
| 58 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 59 |
|
| 60 |
— *end example*]
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
of
|
| 65 |
-
template definition
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
the template parameter names used in the
|
| 69 |
-
template
|
| 70 |
-
definition
|
| 71 |
-
|
| 72 |
-
pack shall be expanded with an ellipsis in the template argument list.
|
| 73 |
|
| 74 |
[*Example 2*:
|
| 75 |
|
| 76 |
``` cpp
|
| 77 |
template<class T1, class T2> struct A {
|
|
@@ -102,11 +101,11 @@ template<C T> struct S {
|
|
| 102 |
void g();
|
| 103 |
void h();
|
| 104 |
template<D U> struct Inner;
|
| 105 |
};
|
| 106 |
|
| 107 |
-
template<C A> void S<A>::f() { } // OK
|
| 108 |
template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
|
| 109 |
|
| 110 |
template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
|
| 111 |
void S<T>::h() { } // functionally equivalent but not equivalent
|
| 112 |
|
|
@@ -114,13 +113,15 @@ template<C X> template<D Y>
|
|
| 114 |
struct S<X>::Inner { }; // OK
|
| 115 |
```
|
| 116 |
|
| 117 |
— *end example*]
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 124 |
|
| 125 |
A member function of a class template may be defined outside of the
|
| 126 |
class template definition in which it is declared.
|
|
@@ -137,11 +138,11 @@ public:
|
|
| 137 |
T& elem(int i) { return v[i]; }
|
| 138 |
};
|
| 139 |
```
|
| 140 |
|
| 141 |
declares three member functions of a class template. The subscript
|
| 142 |
-
function
|
| 143 |
|
| 144 |
``` cpp
|
| 145 |
template<class T> T& Array<T>::operator[](int i) {
|
| 146 |
if (i<0 || sz<=i) error("Array: range error");
|
| 147 |
return v[i];
|
|
@@ -190,12 +191,12 @@ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
|
| 190 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 191 |
|
| 192 |
Deduction guides are used when a *template-name* appears as a type
|
| 193 |
specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
|
| 194 |
guides are not found by name lookup. Instead, when performing class
|
| 195 |
-
template argument deduction [[over.match.class.deduct]],
|
| 196 |
-
guides declared for the class template are considered.
|
| 197 |
|
| 198 |
``` bnf
|
| 199 |
deduction-guide:
|
| 200 |
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 201 |
```
|
|
@@ -221,15 +222,15 @@ S x{A()}; // x is of type S<short, int>
|
|
| 221 |
|
| 222 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 223 |
deduction guide as in a function declaration [[dcl.fct]]. The
|
| 224 |
*simple-template-id* shall name a class template specialization. The
|
| 225 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 226 |
-
the *simple-template-id*. A *deduction-guide* shall
|
| 227 |
-
|
| 228 |
-
template,
|
| 229 |
-
same
|
| 230 |
-
|
| 231 |
|
| 232 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 233 |
|
| 234 |
A member class of a class template may be defined outside the class
|
| 235 |
template definition in which it is declared.
|
|
@@ -241,13 +242,13 @@ instantiation [[temp.inst]]. For example,
|
|
| 241 |
|
| 242 |
``` cpp
|
| 243 |
template<class T> struct A {
|
| 244 |
class B;
|
| 245 |
};
|
| 246 |
-
A<int>::B* b1; // OK
|
| 247 |
template<class T> class A<T>::B { };
|
| 248 |
-
A<int>::B b2; // OK
|
| 249 |
```
|
| 250 |
|
| 251 |
— *end note*]
|
| 252 |
|
| 253 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
|
@@ -283,11 +284,11 @@ of unknown bound can have a different bound from its definition, if any.
|
|
| 283 |
``` cpp
|
| 284 |
template <class T> struct A {
|
| 285 |
static int i[];
|
| 286 |
};
|
| 287 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 288 |
-
template <> int A<int>::i[] = { 1 }; // OK
|
| 289 |
```
|
| 290 |
|
| 291 |
— *end example*]
|
| 292 |
|
| 293 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
|
@@ -379,11 +380,11 @@ int main() {
|
|
| 379 |
}
|
| 380 |
```
|
| 381 |
|
| 382 |
— *end example*]
|
| 383 |
|
| 384 |
-
A member function template shall not be virtual.
|
| 385 |
|
| 386 |
[*Example 4*:
|
| 387 |
|
| 388 |
``` cpp
|
| 389 |
template <class T> struct AA {
|
|
@@ -404,19 +405,21 @@ class B {
|
|
| 404 |
virtual void f(int);
|
| 405 |
};
|
| 406 |
|
| 407 |
class D : public B {
|
| 408 |
template <class T> void f(T); // does not override B::f(int)
|
| 409 |
-
void f(int i) { f<>(i); } // overriding function that calls the template
|
| 410 |
};
|
| 411 |
```
|
| 412 |
|
| 413 |
— *end example*]
|
| 414 |
|
|
|
|
|
|
|
| 415 |
A specialization of a conversion function template is referenced in the
|
| 416 |
same way as a non-template conversion function that converts to the same
|
| 417 |
-
type.
|
| 418 |
|
| 419 |
[*Example 6*:
|
| 420 |
|
| 421 |
``` cpp
|
| 422 |
struct A {
|
|
@@ -433,27 +436,15 @@ int main() {
|
|
| 433 |
}
|
| 434 |
```
|
| 435 |
|
| 436 |
— *end example*]
|
| 437 |
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
conversion function template
|
| 441 |
|
| 442 |
-
|
| 443 |
-
lookup. Instead, any conversion function templates visible in the
|
| 444 |
-
context of the use are considered. For each such operator, if argument
|
| 445 |
-
deduction succeeds [[temp.deduct.conv]], the resulting specialization is
|
| 446 |
-
used as if found by name lookup.
|
| 447 |
-
|
| 448 |
-
A *using-declaration* in a derived class cannot refer to a
|
| 449 |
-
specialization of a conversion function template in a base class.
|
| 450 |
-
|
| 451 |
-
Overload resolution [[over.ics.rank]] and partial ordering
|
| 452 |
-
[[temp.func.order]] are used to select the best conversion function
|
| 453 |
-
among multiple specializations of conversion function templates and/or
|
| 454 |
-
non-template conversion functions.
|
| 455 |
|
| 456 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 457 |
|
| 458 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 459 |
more template arguments.
|
|
@@ -582,94 +573,72 @@ template<class ... Args1> struct zip {
|
|
| 582 |
typedef Tuple<Pair<Args1, Args2> ... > type;
|
| 583 |
};
|
| 584 |
};
|
| 585 |
|
| 586 |
typedef zip<short, int>::with<unsigned short, unsigned>::type T1;
|
| 587 |
-
// T1 is Tuple<Pair<short, unsigned short>, Pair<int, unsigned>
|
| 588 |
typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
| 589 |
// error: different number of arguments specified for Args1 and Args2
|
| 590 |
|
| 591 |
template<class ... Args>
|
| 592 |
-
void g(Args ... args) { // OK
|
| 593 |
-
f(const_cast<const Args*>(&args)...); // OK
|
| 594 |
f(5 ...); // error: pattern does not contain any packs
|
| 595 |
f(args); // error: pack ``args'' is not expanded
|
| 596 |
-
f(h(args ...) + args ...); // OK
|
| 597 |
// second ``args'' expanded within f
|
| 598 |
}
|
| 599 |
```
|
| 600 |
|
| 601 |
— *end example*]
|
| 602 |
|
| 603 |
-
The instantiation of a pack expansion
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
follows:
|
| 610 |
|
| 611 |
-
- if the pack is a template parameter pack, the element is
|
| 612 |
-
|
| 613 |
-
|
|
|
|
|
|
|
|
|
|
| 614 |
- if the pack is a function parameter pack, the element is an
|
| 615 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 616 |
from instantiation of the function parameter pack declaration;
|
| 617 |
otherwise
|
| 618 |
- if the pack is an *init-capture* pack, the element is an
|
| 619 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 620 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 621 |
-
pack.
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
etc. — *end note*]
|
| 628 |
-
|
| 629 |
-
When N is zero, the instantiation of the expansion produces an empty
|
| 630 |
-
list. Such an instantiation does not alter the syntactic interpretation
|
| 631 |
-
of the enclosing construct, even in cases where omitting the list
|
| 632 |
-
entirely would otherwise be ill-formed or would result in an ambiguity
|
| 633 |
-
in the grammar.
|
| 634 |
-
|
| 635 |
-
[*Example 6*:
|
| 636 |
-
|
| 637 |
-
``` cpp
|
| 638 |
-
template<class... T> struct X : T... { };
|
| 639 |
-
template<class... T> void f(T... values) {
|
| 640 |
-
X<T...> x(values...);
|
| 641 |
-
}
|
| 642 |
-
|
| 643 |
-
template void f<>(); // OK: X<> has no base classes
|
| 644 |
-
// x is a variable of type X<> that is value-initialized
|
| 645 |
-
```
|
| 646 |
-
|
| 647 |
-
— *end example*]
|
| 648 |
|
| 649 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 650 |
-
an integral constant
|
| 651 |
-
expands.
|
| 652 |
|
| 653 |
-
The instantiation of a *fold-expression* produces:
|
| 654 |
|
| 655 |
-
- `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
|
|
|
| 662 |
|
| 663 |
-
In each case, *op* is the *fold-operator*
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
with its iᵗʰ element. For a binary fold-expression, E is generated by
|
| 667 |
-
instantiating the *cast-expression* that did not contain an unexpanded
|
| 668 |
-
pack.
|
| 669 |
|
| 670 |
-
[*Example
|
| 671 |
|
| 672 |
``` cpp
|
| 673 |
template<typename ...Args>
|
| 674 |
bool all(Args ...args) { return (... && args); }
|
| 675 |
|
|
@@ -679,12 +648,12 @@ bool b = all(true, true, true, false);
|
|
| 679 |
Within the instantiation of `all`, the returned expression expands to
|
| 680 |
`((true && true) && true) && false`, which evaluates to `false`.
|
| 681 |
|
| 682 |
— *end example*]
|
| 683 |
|
| 684 |
-
If N is zero for a unary fold
|
| 685 |
-
|
| 686 |
[[temp.fold.empty]], the instantiation is ill-formed.
|
| 687 |
|
| 688 |
**Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
|
| 689 |
|
| 690 |
| Operator | Value when pack is empty |
|
|
@@ -692,29 +661,39 @@ shown in [[temp.fold.empty]]; if the operator is not listed in
|
|
| 692 |
| `&&` | `true` |
|
| 693 |
| `||` | `false` |
|
| 694 |
| `,` | `void()` |
|
| 695 |
|
| 696 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 698 |
|
| 699 |
A friend of a class or class template can be a function template or
|
| 700 |
class template, a specialization of a function template or class
|
| 701 |
-
template, or a non-template function or class.
|
| 702 |
-
declaration that is not a template declaration:
|
| 703 |
-
|
| 704 |
-
- if the name of the friend is a qualified or unqualified *template-id*,
|
| 705 |
-
the friend declaration refers to a specialization of a function
|
| 706 |
-
template, otherwise,
|
| 707 |
-
- if the name of the friend is a *qualified-id* and a matching
|
| 708 |
-
non-template function is found in the specified class or namespace,
|
| 709 |
-
the friend declaration refers to that function, otherwise,
|
| 710 |
-
- if the name of the friend is a *qualified-id* and a matching function
|
| 711 |
-
template is found in the specified class or namespace, the friend
|
| 712 |
-
declaration refers to the deduced specialization of that function
|
| 713 |
-
template [[temp.deduct.decl]], otherwise,
|
| 714 |
-
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 715 |
-
non-template function.
|
| 716 |
|
| 717 |
[*Example 1*:
|
| 718 |
|
| 719 |
``` cpp
|
| 720 |
template<class T> class task;
|
|
@@ -744,10 +723,19 @@ function template `preempt` as a friend; and each specialization of the
|
|
| 744 |
template has the class template specialization `task<int>` as a friend,
|
| 745 |
and has all specializations of the class template `frd` as friends.
|
| 746 |
|
| 747 |
— *end example*]
|
| 748 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 749 |
A friend template may be declared within a class or class template. A
|
| 750 |
friend function template may be defined within a class or class
|
| 751 |
template, but a friend class template may not be defined in a class or
|
| 752 |
class template. In these cases, all specializations of the friend class
|
| 753 |
or friend function template are friends of the class or class template
|
|
@@ -764,11 +752,11 @@ class A {
|
|
| 764 |
|
| 765 |
— *end example*]
|
| 766 |
|
| 767 |
A template friend declaration specifies that all specializations of that
|
| 768 |
template, whether they are implicitly instantiated [[temp.inst]],
|
| 769 |
-
partially specialized [[temp.
|
| 770 |
[[temp.expl.spec]], are friends of the class containing the template
|
| 771 |
friend declaration.
|
| 772 |
|
| 773 |
[*Example 3*:
|
| 774 |
|
|
@@ -787,17 +775,17 @@ template<class T> struct A<T*> { X::Y ab; }; // OK
|
|
| 787 |
A template friend declaration may declare a member of a dependent type
|
| 788 |
to be a friend. The friend declaration shall declare a function or
|
| 789 |
specify a type with an *elaborated-type-specifier*, in either case with
|
| 790 |
a *nested-name-specifier* ending with a *simple-template-id*, *C*, whose
|
| 791 |
*template-name* names a class template. The template parameters of the
|
| 792 |
-
template friend declaration shall be deducible from *C*
|
| 793 |
-
[[temp.deduct.type]]
|
| 794 |
the class template is a friend of the class granting friendship if
|
| 795 |
deduction of the template parameters of *C* from *S* succeeds, and
|
| 796 |
substituting the deduced template arguments into the friend declaration
|
| 797 |
-
produces a declaration that
|
| 798 |
-
|
| 799 |
|
| 800 |
[*Example 4*:
|
| 801 |
|
| 802 |
``` cpp
|
| 803 |
template<class T> struct A {
|
|
@@ -833,13 +821,10 @@ class C {
|
|
| 833 |
}; // of those function templates
|
| 834 |
```
|
| 835 |
|
| 836 |
— *end example*]
|
| 837 |
|
| 838 |
-
[*Note 1*: A friend declaration may first declare a member of an
|
| 839 |
-
enclosing namespace scope [[temp.inject]]. — *end note*]
|
| 840 |
-
|
| 841 |
A friend template shall not be declared in a local class.
|
| 842 |
|
| 843 |
Friend declarations shall not declare partial specializations.
|
| 844 |
|
| 845 |
[*Example 5*:
|
|
@@ -863,30 +848,28 @@ definition. A friend function template with a constraint that depends on
|
|
| 863 |
a template parameter from an enclosing template shall be a definition.
|
| 864 |
Such a constrained friend function or function template declaration does
|
| 865 |
not declare the same function or function template as a declaration in
|
| 866 |
any other scope.
|
| 867 |
|
| 868 |
-
###
|
| 869 |
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
specialization* of the class template named in the *simple-template-id*.
|
| 874 |
-
A partial specialization of a class template provides an alternative
|
| 875 |
definition of the template that is used instead of the primary
|
| 876 |
definition when the arguments in a specialization match those given in
|
| 877 |
-
the partial specialization [[temp.
|
| 878 |
-
template shall
|
| 879 |
-
A partial specialization shall be
|
| 880 |
-
|
| 881 |
-
specialization as the result of an implicit or explicit instantiation
|
| 882 |
-
|
| 883 |
-
required.
|
| 884 |
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
|
|
|
| 888 |
|
| 889 |
[*Example 1*:
|
| 890 |
|
| 891 |
``` cpp
|
| 892 |
template<class T1, class T2, int I> class A { };
|
|
@@ -900,11 +883,11 @@ The first declaration declares the primary (unspecialized) class
|
|
| 900 |
template. The second and subsequent declarations declare partial
|
| 901 |
specializations of the primary template.
|
| 902 |
|
| 903 |
— *end example*]
|
| 904 |
|
| 905 |
-
A
|
| 906 |
|
| 907 |
[*Example 2*:
|
| 908 |
|
| 909 |
``` cpp
|
| 910 |
template<typename T> concept C = true;
|
|
@@ -921,38 +904,18 @@ specialization succeeds, while the reverse does not. \#2 is more
|
|
| 921 |
specialized because the template arguments are equivalent, but the
|
| 922 |
partial specialization is more constrained [[temp.constr.order]].
|
| 923 |
|
| 924 |
— *end example*]
|
| 925 |
|
| 926 |
-
The template
|
| 927 |
-
|
| 928 |
-
specializations, the template argument list is explicitly written
|
| 929 |
-
immediately following the class template name. For primary templates,
|
| 930 |
-
this list is implicitly described by the template parameter list.
|
| 931 |
-
Specifically, the order of the template arguments is the sequence in
|
| 932 |
-
which they appear in the template parameter list.
|
| 933 |
|
| 934 |
-
|
| 935 |
-
|
|
|
|
| 936 |
|
| 937 |
-
[*
|
| 938 |
-
|
| 939 |
-
The template argument list cannot be specified in the primary template
|
| 940 |
-
declaration. For example,
|
| 941 |
-
|
| 942 |
-
``` cpp
|
| 943 |
-
template<class T1, class T2, int I>
|
| 944 |
-
class A<T1, T2, I> { }; // error
|
| 945 |
-
```
|
| 946 |
-
|
| 947 |
-
— *end note*]
|
| 948 |
-
|
| 949 |
-
A class template partial specialization may be declared in any scope in
|
| 950 |
-
which the corresponding primary template may be defined (
|
| 951 |
-
[[namespace.memdef]], [[class.mem]], [[temp.mem]]).
|
| 952 |
-
|
| 953 |
-
[*Example 4*:
|
| 954 |
|
| 955 |
``` cpp
|
| 956 |
template<class T> struct A {
|
| 957 |
struct C {
|
| 958 |
template<class T2> struct B { };
|
|
@@ -967,18 +930,19 @@ template<class T> template<class T2>
|
|
| 967 |
A<short>::C::B<int*> absip; // uses partial specialization #2
|
| 968 |
```
|
| 969 |
|
| 970 |
— *end example*]
|
| 971 |
|
| 972 |
-
Partial specialization declarations
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
also considered. One consequence is that a *using-declaration* which
|
| 976 |
-
refers to a class template does not restrict the set of partial
|
| 977 |
-
specializations which may be found through the *using-declaration*.
|
| 978 |
|
| 979 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 980 |
|
| 981 |
``` cpp
|
| 982 |
namespace N {
|
| 983 |
template<class T1, class T2> class A { }; // primary template
|
| 984 |
}
|
|
@@ -996,61 +960,61 @@ A<int,int*> a; // uses the partial specialization, which is found through t
|
|
| 996 |
— *end example*]
|
| 997 |
|
| 998 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 999 |
parameter. All other non-type arguments are specialized.
|
| 1000 |
|
| 1001 |
-
Within the argument list of a
|
| 1002 |
-
|
| 1003 |
|
| 1004 |
- The type of a template parameter corresponding to a specialized
|
| 1005 |
-
non-type argument shall not be dependent on a parameter of the
|
| 1006 |
specialization.
|
| 1007 |
-
\[*Example
|
| 1008 |
``` cpp
|
| 1009 |
template <class T, T t> struct C {};
|
| 1010 |
template <class T> struct C<T, 1>; // error
|
| 1011 |
|
| 1012 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 1013 |
int array[5];
|
| 1014 |
template< int X > class A<X,&array> { }; // error
|
| 1015 |
```
|
| 1016 |
|
| 1017 |
— *end example*]
|
| 1018 |
-
- The specialization shall be more specialized than the primary
|
| 1019 |
-
[[temp.
|
| 1020 |
-
- The template parameter list of a specialization shall not
|
| 1021 |
-
default template argument values.[^8]
|
| 1022 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 1023 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 1024 |
template argument list.
|
| 1025 |
|
| 1026 |
The usual access checking rules do not apply to non-dependent names used
|
| 1027 |
to specify template arguments of the *simple-template-id* of the partial
|
| 1028 |
specialization.
|
| 1029 |
|
| 1030 |
-
[*Note 2*: The template arguments
|
| 1031 |
would normally not be accessible. Dependent names cannot be checked when
|
| 1032 |
declaring the partial specialization, but will be checked when
|
| 1033 |
substituting into the partial specialization. — *end note*]
|
| 1034 |
|
| 1035 |
-
#### Matching of
|
| 1036 |
|
| 1037 |
-
When a
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
|
| 1044 |
-
- If exactly one matching specialization is found, the
|
| 1045 |
-
generated from that specialization.
|
| 1046 |
-
- If more than one matching specialization is found, the partial
|
| 1047 |
-
rules [[temp.
|
| 1048 |
-
specializations is more specialized than the
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
the program is ill-formed.
|
| 1052 |
- If no matches are found, the instantiation is generated from the
|
| 1053 |
primary template.
|
| 1054 |
|
| 1055 |
A partial specialization matches a given actual template argument list
|
| 1056 |
if the template arguments of the partial specialization can be deduced
|
|
@@ -1108,21 +1072,22 @@ template <int I, int J, int K> struct B {};
|
|
| 1108 |
template <int I> struct B<I, I*2, 2> {}; // OK
|
| 1109 |
```
|
| 1110 |
|
| 1111 |
— *end example*]
|
| 1112 |
|
| 1113 |
-
In a
|
| 1114 |
-
`A<int, int, 1>`) the argument list shall match the
|
| 1115 |
-
list of the primary template. The template arguments
|
| 1116 |
-
are deduced from the arguments of the
|
|
|
|
| 1117 |
|
| 1118 |
-
#### Partial ordering of
|
| 1119 |
|
| 1120 |
-
For two
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
[[temp.func.order]]:
|
| 1125 |
|
| 1126 |
- Each of the two function templates has the same template parameters
|
| 1127 |
and associated constraints [[temp.constr.decl]] as the corresponding
|
| 1128 |
partial specialization.
|
|
@@ -1176,26 +1141,20 @@ template<D T> void f(S<T>); // B
|
|
| 1176 |
The partial specialization \#2 is more specialized than \#1 because `B`
|
| 1177 |
is more specialized than `A`.
|
| 1178 |
|
| 1179 |
— *end example*]
|
| 1180 |
|
| 1181 |
-
#### Members of class template specializations <a id="temp.
|
| 1182 |
|
| 1183 |
-
The
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
that are used in a way that requires a definition shall be defined; the
|
| 1192 |
-
definitions of members of the primary template are never used as
|
| 1193 |
-
definitions for members of a class template partial specialization. An
|
| 1194 |
-
explicit specialization of a member of a class template partial
|
| 1195 |
-
specialization is declared in the same way as an explicit specialization
|
| 1196 |
-
of the primary template.
|
| 1197 |
|
| 1198 |
[*Example 1*:
|
| 1199 |
|
| 1200 |
``` cpp
|
| 1201 |
// primary class template
|
|
@@ -1232,15 +1191,15 @@ int main() {
|
|
| 1232 |
— *end example*]
|
| 1233 |
|
| 1234 |
If a member template of a class template is partially specialized, the
|
| 1235 |
member template partial specializations are member templates of the
|
| 1236 |
enclosing class template; if the enclosing class template is
|
| 1237 |
-
instantiated
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
|
| 1241 |
-
|
| 1242 |
specializations of the member template are ignored for this
|
| 1243 |
specialization of the enclosing class template. If a partial
|
| 1244 |
specialization of the member template is explicitly specialized for a
|
| 1245 |
given (implicit) specialization of the enclosing class template, the
|
| 1246 |
primary member template and its other partial specializations are still
|
|
@@ -1263,28 +1222,33 @@ A<char>::B<int> abci; // uses #1
|
|
| 1263 |
|
| 1264 |
— *end example*]
|
| 1265 |
|
| 1266 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 1267 |
|
|
|
|
|
|
|
| 1268 |
A function template defines an unbounded set of related functions.
|
| 1269 |
|
| 1270 |
[*Example 1*:
|
| 1271 |
|
| 1272 |
-
A family of sort functions
|
| 1273 |
|
| 1274 |
``` cpp
|
| 1275 |
template<class T> class Array { };
|
| 1276 |
template<class T> void sort(Array<T>&);
|
| 1277 |
```
|
| 1278 |
|
| 1279 |
— *end example*]
|
| 1280 |
|
| 1281 |
-
A function template can
|
| 1282 |
-
|
| 1283 |
-
|
| 1284 |
-
|
| 1285 |
-
|
|
|
|
|
|
|
|
|
|
| 1286 |
|
| 1287 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1288 |
|
| 1289 |
It is possible to overload function templates so that two different
|
| 1290 |
function template specializations have the same type.
|
|
@@ -1319,17 +1283,17 @@ names of the template parameters are significant only for establishing
|
|
| 1319 |
the relationship between the template parameters and the rest of the
|
| 1320 |
signature.
|
| 1321 |
|
| 1322 |
[*Note 1*:
|
| 1323 |
|
| 1324 |
-
Two distinct function templates
|
| 1325 |
and function parameter lists, even if overload resolution alone cannot
|
| 1326 |
distinguish them.
|
| 1327 |
|
| 1328 |
``` cpp
|
| 1329 |
template<class T> void f();
|
| 1330 |
-
template<int I> void f(); // OK
|
| 1331 |
// distinguishable with an explicit template argument list
|
| 1332 |
```
|
| 1333 |
|
| 1334 |
— *end note*]
|
| 1335 |
|
|
@@ -1366,25 +1330,27 @@ another token that names the same template parameter in the other
|
|
| 1366 |
expression. Two unevaluated operands that do not involve template
|
| 1367 |
parameters are considered equivalent if two function definitions
|
| 1368 |
containing the expressions would satisfy the one-definition rule, except
|
| 1369 |
that the tokens used to name types and declarations may differ as long
|
| 1370 |
as they name the same entities, and the tokens used to form concept-ids
|
| 1371 |
-
may differ as long as the two *template-id*s are the same
|
|
|
|
| 1372 |
|
| 1373 |
[*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
|
| 1374 |
type. — *end note*]
|
| 1375 |
|
| 1376 |
Two *lambda-expression*s are never considered equivalent.
|
| 1377 |
|
| 1378 |
[*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
|
| 1379 |
signature of a function template with external linkage. — *end note*]
|
| 1380 |
|
| 1381 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 1382 |
-
only the name itself is considered, not the result of name lookup
|
| 1383 |
-
|
| 1384 |
-
|
| 1385 |
-
first declaration
|
|
|
|
| 1386 |
|
| 1387 |
[*Example 3*:
|
| 1388 |
|
| 1389 |
``` cpp
|
| 1390 |
template <int I, int J> void f(A<I+J>); // #1
|
|
@@ -1393,11 +1359,11 @@ template <int K, int L> void f(A<K+L>); // same as #1
|
|
| 1393 |
template <class T> decltype(g(T())) h();
|
| 1394 |
int g(int);
|
| 1395 |
template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup…
|
| 1396 |
{ return g(T()); } // …{} although the lookup here does find g(int)
|
| 1397 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 1398 |
-
//
|
| 1399 |
|
| 1400 |
// ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
|
| 1401 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 1402 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 1403 |
|
|
@@ -1414,13 +1380,25 @@ of template arguments, the evaluation of the expression results in the
|
|
| 1414 |
same value. Two unevaluated operands that are not equivalent are
|
| 1415 |
functionally equivalent if, for any given set of template arguments, the
|
| 1416 |
expressions perform the same operations in the same order with the same
|
| 1417 |
entities.
|
| 1418 |
|
| 1419 |
-
[*Note
|
| 1420 |
parentheses. — *end note*]
|
| 1421 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1422 |
Two *template-head*s are *equivalent* if their
|
| 1423 |
*template-parameter-list*s have the same length, corresponding
|
| 1424 |
*template-parameter*s are equivalent and are both declared with
|
| 1425 |
*type-constraint*s that are equivalent if either *template-parameter* is
|
| 1426 |
declared with a *type-constraint*, and if either *template-head* has a
|
|
@@ -1440,25 +1418,24 @@ When determining whether types or *type-constraint*s are equivalent, the
|
|
| 1440 |
rules above are used to compare expressions involving template
|
| 1441 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 1442 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 1443 |
template argument lists.
|
| 1444 |
|
| 1445 |
-
|
| 1446 |
-
|
| 1447 |
-
|
| 1448 |
-
|
| 1449 |
-
expressions involving template parameters. Two function templates are
|
| 1450 |
-
*functionally equivalent* if they are declared in the same scope, have
|
| 1451 |
-
the same name, accept and are satisfied by the same set of template
|
| 1452 |
-
argument lists, and have return types and parameter lists that are
|
| 1453 |
-
functionally equivalent using the rules described above to compare
|
| 1454 |
-
expressions involving template parameters. If the validity or meaning of
|
| 1455 |
-
the program depends on whether two constructs are equivalent, and they
|
| 1456 |
-
are functionally equivalent but not equivalent, the program is
|
| 1457 |
-
ill-formed, no diagnostic required.
|
| 1458 |
|
| 1459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1460 |
|
| 1461 |
This rule guarantees that equivalent declarations will be linked with
|
| 1462 |
one another, while not requiring implementations to use heroic efforts
|
| 1463 |
to guarantee that functionally equivalent declarations will be treated
|
| 1464 |
as distinct. For example, the last two declarations are functionally
|
|
@@ -1480,24 +1457,23 @@ template <int I> void f(A<I>, A<I+1+2+3+4>);
|
|
| 1480 |
|
| 1481 |
— *end note*]
|
| 1482 |
|
| 1483 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 1484 |
|
| 1485 |
-
If
|
| 1486 |
-
|
| 1487 |
-
|
| 1488 |
-
|
| 1489 |
-
|
| 1490 |
-
|
| 1491 |
-
specialization refers:
|
| 1492 |
|
| 1493 |
- during overload resolution for a call to a function template
|
| 1494 |
specialization [[over.match.best]];
|
| 1495 |
- when the address of a function template specialization is taken;
|
| 1496 |
- when a placement operator delete that is a function template
|
| 1497 |
-
specialization is selected to match a placement operator new
|
| 1498 |
-
[[basic.stc.dynamic.deallocation]], [[expr.new]]
|
| 1499 |
- when a friend function declaration [[temp.friend]], an explicit
|
| 1500 |
instantiation [[temp.explicit]] or an explicit specialization
|
| 1501 |
[[temp.expl.spec]] refers to a function template specialization.
|
| 1502 |
|
| 1503 |
Partial ordering selects which of two function templates is more
|
|
@@ -1647,12 +1623,12 @@ template<class T, class... U> void f(T, U...); // #1
|
|
| 1647 |
template<class T > void f(T); // #2
|
| 1648 |
template<class T, class... U> void g(T*, U...); // #3
|
| 1649 |
template<class T > void g(T); // #4
|
| 1650 |
|
| 1651 |
void h(int i) {
|
| 1652 |
-
f(&i); // OK
|
| 1653 |
-
g(&i); // OK
|
| 1654 |
}
|
| 1655 |
```
|
| 1656 |
|
| 1657 |
— *end example*]
|
| 1658 |
|
|
@@ -1702,20 +1678,20 @@ template <typename T> concept C = True<T>;
|
|
| 1702 |
|
| 1703 |
void f(C auto &, auto &) = delete;
|
| 1704 |
template <C Q> void f(Q &, C auto &);
|
| 1705 |
|
| 1706 |
void g(struct A *ap, struct B *bp) {
|
| 1707 |
-
f(*ap, *bp); // OK
|
| 1708 |
}
|
| 1709 |
|
| 1710 |
template <typename T, typename U> struct X {};
|
| 1711 |
|
| 1712 |
template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
|
| 1713 |
template <C T, C U, C V> bool operator==(T, X<U, V>);
|
| 1714 |
|
| 1715 |
void h() {
|
| 1716 |
-
X<void *, int>{} == 0; // OK
|
| 1717 |
}
|
| 1718 |
```
|
| 1719 |
|
| 1720 |
— *end example*]
|
| 1721 |
|
|
@@ -1736,11 +1712,11 @@ it is equivalent to the associated type obtained by substitution of its
|
|
| 1736 |
[*Example 1*:
|
| 1737 |
|
| 1738 |
``` cpp
|
| 1739 |
template<class T> struct Alloc { ... };
|
| 1740 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 1741 |
-
Vec<int> v; // same as vector<int, Alloc<int>
|
| 1742 |
|
| 1743 |
template<class T>
|
| 1744 |
void process(Vec<T>& v)
|
| 1745 |
{ ... }
|
| 1746 |
|
|
@@ -1753,11 +1729,11 @@ template<template<class> class TT>
|
|
| 1753 |
|
| 1754 |
f(v); // error: Vec not deduced
|
| 1755 |
|
| 1756 |
template<template<class,class> class TT>
|
| 1757 |
void g(TT<int, Alloc<int>>);
|
| 1758 |
-
g(v); // OK
|
| 1759 |
```
|
| 1760 |
|
| 1761 |
— *end example*]
|
| 1762 |
|
| 1763 |
However, if the *template-id* is dependent, subsequent template argument
|
|
@@ -1809,20 +1785,21 @@ template <class T>
|
|
| 1809 |
A *concept* is a template that defines constraints on its template
|
| 1810 |
arguments.
|
| 1811 |
|
| 1812 |
``` bnf
|
| 1813 |
concept-definition:
|
| 1814 |
-
concept concept-name '=' constraint-expression ';'
|
| 1815 |
```
|
| 1816 |
|
| 1817 |
``` bnf
|
| 1818 |
concept-name:
|
| 1819 |
identifier
|
| 1820 |
```
|
| 1821 |
|
| 1822 |
A *concept-definition* declares a concept. Its *identifier* becomes a
|
| 1823 |
-
*concept-name* referring to that concept within its scope.
|
|
|
|
| 1824 |
|
| 1825 |
[*Example 1*:
|
| 1826 |
|
| 1827 |
``` cpp
|
| 1828 |
template<typename T>
|
|
@@ -1838,20 +1815,21 @@ template<C T> // C, as a type-constraint, constrains f2(T)
|
|
| 1838 |
T f2(T x) { return x; }
|
| 1839 |
```
|
| 1840 |
|
| 1841 |
— *end example*]
|
| 1842 |
|
| 1843 |
-
A *concept-definition* shall
|
| 1844 |
[[basic.scope.namespace]].
|
| 1845 |
|
| 1846 |
A concept shall not have associated constraints [[temp.constr.decl]].
|
| 1847 |
|
| 1848 |
A concept is not instantiated [[temp.spec]].
|
| 1849 |
|
| 1850 |
[*Note 1*: A concept-id [[temp.names]] is evaluated as an expression. A
|
| 1851 |
concept cannot be explicitly instantiated [[temp.explicit]], explicitly
|
| 1852 |
-
specialized [[temp.expl.spec]], or partially specialized
|
|
|
|
| 1853 |
|
| 1854 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 1855 |
operand [[expr.context]].
|
| 1856 |
|
| 1857 |
The first declared template parameter of a concept definition is its
|
|
|
|
| 1 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="temp.decls.general">[[temp.decls.general]]</a>
|
| 4 |
+
|
| 5 |
+
The template parameters of a template are specified in the angle bracket
|
| 6 |
+
enclosed list that immediately follows the keyword `template`.
|
| 7 |
+
|
| 8 |
+
A *primary template* declaration is one in which the name of the
|
| 9 |
+
template is not followed by a *template-argument-list*. The template
|
| 10 |
+
argument list of a primary template is the template argument list of its
|
| 11 |
+
*template-head* [[temp.arg]]. A template declaration in which the name
|
| 12 |
+
of the template is followed by a *template-argument-list* is a partial
|
| 13 |
+
specialization [[temp.spec.partial]] of the template named in the
|
| 14 |
+
declaration, which shall be a class or variable template.
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
For purposes of name lookup and instantiation, default arguments,
|
| 17 |
*type-constraint*s, *requires-clause*s [[temp.pre]], and
|
| 18 |
*noexcept-specifier*s of function templates and of member functions of
|
| 19 |
class templates are considered definitions; each default argument,
|
| 20 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 21 |
separate definition which is unrelated to the templated function
|
| 22 |
+
definition or to any other default arguments, *type-constraint*s,
|
| 23 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 24 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 25 |
are considered definitions.
|
| 26 |
|
| 27 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 28 |
possible to partially or explicitly specialize an alias template.
|
| 29 |
|
| 30 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 31 |
|
| 32 |
+
#### General <a id="temp.class.general">[[temp.class.general]]</a>
|
| 33 |
+
|
| 34 |
A *class template* defines the layout and operations for an unbounded
|
| 35 |
set of related types.
|
| 36 |
|
| 37 |
[*Example 1*:
|
| 38 |
|
| 39 |
+
It is possible for a single class template `List` to provide an
|
| 40 |
+
unbounded set of class definitions: one class `List<T>` for every type
|
| 41 |
+
`T`, each describing a linked list of elements of type `T`. Similarly, a
|
| 42 |
+
class template `Array` describing a contiguous, dynamic array can be
|
| 43 |
+
defined like this:
|
| 44 |
|
| 45 |
``` cpp
|
| 46 |
template<class T> class Array {
|
| 47 |
T* v;
|
| 48 |
int sz;
|
|
|
|
| 52 |
T& elem(int i) { return v[i]; }
|
| 53 |
};
|
| 54 |
```
|
| 55 |
|
| 56 |
The prefix `template<class T>` specifies that a template is being
|
| 57 |
+
declared and that a *type-name* `T` can be used in the declaration. In
|
| 58 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 59 |
|
| 60 |
— *end example*]
|
| 61 |
|
| 62 |
+
[*Note 1*:
|
| 63 |
+
|
| 64 |
+
When a member of a class template is defined outside of the class
|
| 65 |
+
template definition, the member definition is defined as a template
|
| 66 |
+
definition with the *template-head* equivalent to that of the class
|
| 67 |
+
template. The names of the template parameters used in the definition of
|
| 68 |
+
the member can differ from the template parameter names used in the
|
| 69 |
+
class template definition. The class template name in the member
|
| 70 |
+
definition is followed by the template argument list of the
|
| 71 |
+
*template-head* [[temp.arg]].
|
|
|
|
| 72 |
|
| 73 |
[*Example 2*:
|
| 74 |
|
| 75 |
``` cpp
|
| 76 |
template<class T1, class T2> struct A {
|
|
|
|
| 101 |
void g();
|
| 102 |
void h();
|
| 103 |
template<D U> struct Inner;
|
| 104 |
};
|
| 105 |
|
| 106 |
+
template<C A> void S<A>::f() { } // OK, template-head{s} match
|
| 107 |
template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
|
| 108 |
|
| 109 |
template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
|
| 110 |
void S<T>::h() { } // functionally equivalent but not equivalent
|
| 111 |
|
|
|
|
| 113 |
struct S<X>::Inner { }; // OK
|
| 114 |
```
|
| 115 |
|
| 116 |
— *end example*]
|
| 117 |
|
| 118 |
+
— *end note*]
|
| 119 |
+
|
| 120 |
+
In a partial specialization, explicit specialization or explicit
|
| 121 |
+
instantiation of a class template, the *class-key* shall agree in kind
|
| 122 |
+
with the original class template declaration [[dcl.type.elab]].
|
| 123 |
|
| 124 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 125 |
|
| 126 |
A member function of a class template may be defined outside of the
|
| 127 |
class template definition in which it is declared.
|
|
|
|
| 138 |
T& elem(int i) { return v[i]; }
|
| 139 |
};
|
| 140 |
```
|
| 141 |
|
| 142 |
declares three member functions of a class template. The subscript
|
| 143 |
+
function can be defined like this:
|
| 144 |
|
| 145 |
``` cpp
|
| 146 |
template<class T> T& Array<T>::operator[](int i) {
|
| 147 |
if (i<0 || sz<=i) error("Array: range error");
|
| 148 |
return v[i];
|
|
|
|
| 191 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 192 |
|
| 193 |
Deduction guides are used when a *template-name* appears as a type
|
| 194 |
specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
|
| 195 |
guides are not found by name lookup. Instead, when performing class
|
| 196 |
+
template argument deduction [[over.match.class.deduct]], all reachable
|
| 197 |
+
deduction guides declared for the class template are considered.
|
| 198 |
|
| 199 |
``` bnf
|
| 200 |
deduction-guide:
|
| 201 |
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 202 |
```
|
|
|
|
| 222 |
|
| 223 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 224 |
deduction guide as in a function declaration [[dcl.fct]]. The
|
| 225 |
*simple-template-id* shall name a class template specialization. The
|
| 226 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 227 |
+
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 228 |
+
which the corresponding class template belongs and, for a member class
|
| 229 |
+
template, have the same access. Two deduction guide declarations for the
|
| 230 |
+
same class template shall not have equivalent
|
| 231 |
+
*parameter-declaration-clause*s if either is reachable from the other.
|
| 232 |
|
| 233 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 234 |
|
| 235 |
A member class of a class template may be defined outside the class
|
| 236 |
template definition in which it is declared.
|
|
|
|
| 242 |
|
| 243 |
``` cpp
|
| 244 |
template<class T> struct A {
|
| 245 |
class B;
|
| 246 |
};
|
| 247 |
+
A<int>::B* b1; // OK, requires A to be defined but not A::B
|
| 248 |
template<class T> class A<T>::B { };
|
| 249 |
+
A<int>::B b2; // OK, requires A::B to be defined
|
| 250 |
```
|
| 251 |
|
| 252 |
— *end note*]
|
| 253 |
|
| 254 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
|
|
|
| 284 |
``` cpp
|
| 285 |
template <class T> struct A {
|
| 286 |
static int i[];
|
| 287 |
};
|
| 288 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 289 |
+
template <> int A<int>::i[] = { 1 }; // OK, 1 element
|
| 290 |
```
|
| 291 |
|
| 292 |
— *end example*]
|
| 293 |
|
| 294 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
|
|
|
| 380 |
}
|
| 381 |
```
|
| 382 |
|
| 383 |
— *end example*]
|
| 384 |
|
| 385 |
+
A member function template shall not be declared `virtual`.
|
| 386 |
|
| 387 |
[*Example 4*:
|
| 388 |
|
| 389 |
``` cpp
|
| 390 |
template <class T> struct AA {
|
|
|
|
| 405 |
virtual void f(int);
|
| 406 |
};
|
| 407 |
|
| 408 |
class D : public B {
|
| 409 |
template <class T> void f(T); // does not override B::f(int)
|
| 410 |
+
void f(int i) { f<>(i); } // overriding function that calls the function template specialization
|
| 411 |
};
|
| 412 |
```
|
| 413 |
|
| 414 |
— *end example*]
|
| 415 |
|
| 416 |
+
[*Note 1*:
|
| 417 |
+
|
| 418 |
A specialization of a conversion function template is referenced in the
|
| 419 |
same way as a non-template conversion function that converts to the same
|
| 420 |
+
type [[class.conv.fct]].
|
| 421 |
|
| 422 |
[*Example 6*:
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
struct A {
|
|
|
|
| 436 |
}
|
| 437 |
```
|
| 438 |
|
| 439 |
— *end example*]
|
| 440 |
|
| 441 |
+
There is no syntax to form a *template-id* [[temp.names]] by providing
|
| 442 |
+
an explicit template argument list [[temp.arg.explicit]] for a
|
| 443 |
+
conversion function template.
|
| 444 |
|
| 445 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 448 |
|
| 449 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 450 |
more template arguments.
|
|
|
|
| 573 |
typedef Tuple<Pair<Args1, Args2> ... > type;
|
| 574 |
};
|
| 575 |
};
|
| 576 |
|
| 577 |
typedef zip<short, int>::with<unsigned short, unsigned>::type T1;
|
| 578 |
+
// T1 is Tuple<Pair<short, unsigned short>, Pair<int, unsigned>>
|
| 579 |
typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
| 580 |
// error: different number of arguments specified for Args1 and Args2
|
| 581 |
|
| 582 |
template<class ... Args>
|
| 583 |
+
void g(Args ... args) { // OK, Args is expanded by the function parameter pack args
|
| 584 |
+
f(const_cast<const Args*>(&args)...); // OK, ``Args'' and ``args'' are expanded
|
| 585 |
f(5 ...); // error: pattern does not contain any packs
|
| 586 |
f(args); // error: pack ``args'' is not expanded
|
| 587 |
+
f(h(args ...) + args ...); // OK, first ``args'' expanded within h,
|
| 588 |
// second ``args'' expanded within f
|
| 589 |
}
|
| 590 |
```
|
| 591 |
|
| 592 |
— *end example*]
|
| 593 |
|
| 594 |
+
The instantiation of a pack expansion considers items
|
| 595 |
+
`E₁`, `E₂`, …, `E_N`, where N is the number of elements in the pack
|
| 596 |
+
expansion parameters. Each `Eᵢ` is generated by instantiating the
|
| 597 |
+
pattern and replacing each pack expansion parameter with its iᵗʰ
|
| 598 |
+
element. Such an element, in the context of the instantiation, is
|
| 599 |
+
interpreted as follows:
|
|
|
|
| 600 |
|
| 601 |
+
- if the pack is a template parameter pack, the element is an
|
| 602 |
+
*id-expression* (for a non-type template parameter pack), a
|
| 603 |
+
*typedef-name* (for a type template parameter pack declared without
|
| 604 |
+
`template`), or a *template-name* (for a type template parameter pack
|
| 605 |
+
declared with `template`), designating the iᵗʰ corresponding type or
|
| 606 |
+
value template argument;
|
| 607 |
- if the pack is a function parameter pack, the element is an
|
| 608 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 609 |
from instantiation of the function parameter pack declaration;
|
| 610 |
otherwise
|
| 611 |
- if the pack is an *init-capture* pack, the element is an
|
| 612 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 613 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 614 |
+
pack declaration.
|
| 615 |
|
| 616 |
+
When N is zero, the instantiation of a pack expansion does not alter the
|
| 617 |
+
syntactic interpretation of the enclosing construct, even in cases where
|
| 618 |
+
omitting the pack expansion entirely would otherwise be ill-formed or
|
| 619 |
+
would result in an ambiguity in the grammar.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 622 |
+
an integral constant with value N.
|
|
|
|
| 623 |
|
| 624 |
+
The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
| 625 |
|
| 626 |
+
- `(` `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for a unary
|
| 627 |
+
left fold,
|
| 628 |
+
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))`
|
| 629 |
+
`)` for a unary right fold,
|
| 630 |
+
- `(` `(((`E *op* E₁`)` *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for
|
| 631 |
+
a binary left fold, and
|
| 632 |
+
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* `(`$\mathtt{E}_{N}$
|
| 633 |
+
*op* E`)))` `)` for a binary right fold.
|
| 634 |
|
| 635 |
+
In each case, *op* is the *fold-operator*. For a binary fold, E is
|
| 636 |
+
generated by instantiating the *cast-expression* that did not contain an
|
| 637 |
+
unexpanded pack.
|
|
|
|
|
|
|
|
|
|
| 638 |
|
| 639 |
+
[*Example 6*:
|
| 640 |
|
| 641 |
``` cpp
|
| 642 |
template<typename ...Args>
|
| 643 |
bool all(Args ...args) { return (... && args); }
|
| 644 |
|
|
|
|
| 648 |
Within the instantiation of `all`, the returned expression expands to
|
| 649 |
`((true && true) && true) && false`, which evaluates to `false`.
|
| 650 |
|
| 651 |
— *end example*]
|
| 652 |
|
| 653 |
+
If N is zero for a unary fold, the value of the expression is shown in
|
| 654 |
+
[[temp.fold.empty]]; if the operator is not listed in
|
| 655 |
[[temp.fold.empty]], the instantiation is ill-formed.
|
| 656 |
|
| 657 |
**Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
|
| 658 |
|
| 659 |
| Operator | Value when pack is empty |
|
|
|
|
| 661 |
| `&&` | `true` |
|
| 662 |
| `||` | `false` |
|
| 663 |
| `,` | `void()` |
|
| 664 |
|
| 665 |
|
| 666 |
+
The instantiation of any other pack expansion produces a list of
|
| 667 |
+
elements `E₁`, `E₂`, …, `E_N`.
|
| 668 |
+
|
| 669 |
+
[*Note 1*: The variety of list varies with the context:
|
| 670 |
+
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 671 |
+
etc. — *end note*]
|
| 672 |
+
|
| 673 |
+
When N is zero, the instantiation of the expansion produces an empty
|
| 674 |
+
list.
|
| 675 |
+
|
| 676 |
+
[*Example 7*:
|
| 677 |
+
|
| 678 |
+
``` cpp
|
| 679 |
+
template<class... T> struct X : T... { };
|
| 680 |
+
template<class... T> void f(T... values) {
|
| 681 |
+
X<T...> x(values...);
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
+
template void f<>(); // OK, X<> has no base classes
|
| 685 |
+
// x is a variable of type X<> that is value-initialized
|
| 686 |
+
```
|
| 687 |
+
|
| 688 |
+
— *end example*]
|
| 689 |
+
|
| 690 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 691 |
|
| 692 |
A friend of a class or class template can be a function template or
|
| 693 |
class template, a specialization of a function template or class
|
| 694 |
+
template, or a non-template function or class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
[*Example 1*:
|
| 697 |
|
| 698 |
``` cpp
|
| 699 |
template<class T> class task;
|
|
|
|
| 723 |
template has the class template specialization `task<int>` as a friend,
|
| 724 |
and has all specializations of the class template `frd` as friends.
|
| 725 |
|
| 726 |
— *end example*]
|
| 727 |
|
| 728 |
+
Friend classes, class templates, functions, or function templates can be
|
| 729 |
+
declared within a class template. When a template is instantiated, its
|
| 730 |
+
friend declarations are found by name lookup as if the specialization
|
| 731 |
+
had been explicitly declared at its point of instantiation.
|
| 732 |
+
|
| 733 |
+
[*Note 1*: They can introduce entities that belong to an enclosing
|
| 734 |
+
namespace scope [[dcl.meaning]], in which case they are attached to the
|
| 735 |
+
same module as the class template [[module.unit]]. — *end note*]
|
| 736 |
+
|
| 737 |
A friend template may be declared within a class or class template. A
|
| 738 |
friend function template may be defined within a class or class
|
| 739 |
template, but a friend class template may not be defined in a class or
|
| 740 |
class template. In these cases, all specializations of the friend class
|
| 741 |
or friend function template are friends of the class or class template
|
|
|
|
| 752 |
|
| 753 |
— *end example*]
|
| 754 |
|
| 755 |
A template friend declaration specifies that all specializations of that
|
| 756 |
template, whether they are implicitly instantiated [[temp.inst]],
|
| 757 |
+
partially specialized [[temp.spec.partial]] or explicitly specialized
|
| 758 |
[[temp.expl.spec]], are friends of the class containing the template
|
| 759 |
friend declaration.
|
| 760 |
|
| 761 |
[*Example 3*:
|
| 762 |
|
|
|
|
| 775 |
A template friend declaration may declare a member of a dependent type
|
| 776 |
to be a friend. The friend declaration shall declare a function or
|
| 777 |
specify a type with an *elaborated-type-specifier*, in either case with
|
| 778 |
a *nested-name-specifier* ending with a *simple-template-id*, *C*, whose
|
| 779 |
*template-name* names a class template. The template parameters of the
|
| 780 |
+
template friend declaration shall be deducible from *C*
|
| 781 |
+
[[temp.deduct.type]]. In this case, a member of a specialization *S* of
|
| 782 |
the class template is a friend of the class granting friendship if
|
| 783 |
deduction of the template parameters of *C* from *S* succeeds, and
|
| 784 |
substituting the deduced template arguments into the friend declaration
|
| 785 |
+
produces a declaration that corresponds to the member of the
|
| 786 |
+
specialization.
|
| 787 |
|
| 788 |
[*Example 4*:
|
| 789 |
|
| 790 |
``` cpp
|
| 791 |
template<class T> struct A {
|
|
|
|
| 821 |
}; // of those function templates
|
| 822 |
```
|
| 823 |
|
| 824 |
— *end example*]
|
| 825 |
|
|
|
|
|
|
|
|
|
|
| 826 |
A friend template shall not be declared in a local class.
|
| 827 |
|
| 828 |
Friend declarations shall not declare partial specializations.
|
| 829 |
|
| 830 |
[*Example 5*:
|
|
|
|
| 848 |
a template parameter from an enclosing template shall be a definition.
|
| 849 |
Such a constrained friend function or function template declaration does
|
| 850 |
not declare the same function or function template as a declaration in
|
| 851 |
any other scope.
|
| 852 |
|
| 853 |
+
### Partial specialization <a id="temp.spec.partial">[[temp.spec.partial]]</a>
|
| 854 |
|
| 855 |
+
#### General <a id="temp.spec.partial.general">[[temp.spec.partial.general]]</a>
|
| 856 |
+
|
| 857 |
+
A partial specialization of a template provides an alternative
|
|
|
|
|
|
|
| 858 |
definition of the template that is used instead of the primary
|
| 859 |
definition when the arguments in a specialization match those given in
|
| 860 |
+
the partial specialization [[temp.spec.partial.match]]. A declaration of
|
| 861 |
+
the primary template shall precede any partial specialization of that
|
| 862 |
+
template. A partial specialization shall be reachable from any use of a
|
| 863 |
+
template specialization that would make use of the partial
|
| 864 |
+
specialization as the result of an implicit or explicit instantiation;
|
| 865 |
+
no diagnostic is required.
|
|
|
|
| 866 |
|
| 867 |
+
Two partial specialization declarations declare the same entity if they
|
| 868 |
+
are partial specializations of the same template and have equivalent
|
| 869 |
+
*template-head*s and template argument lists [[temp.over.link]]. Each
|
| 870 |
+
partial specialization is a distinct template.
|
| 871 |
|
| 872 |
[*Example 1*:
|
| 873 |
|
| 874 |
``` cpp
|
| 875 |
template<class T1, class T2, int I> class A { };
|
|
|
|
| 883 |
template. The second and subsequent declarations declare partial
|
| 884 |
specializations of the primary template.
|
| 885 |
|
| 886 |
— *end example*]
|
| 887 |
|
| 888 |
+
A partial specialization may be constrained [[temp.constr]].
|
| 889 |
|
| 890 |
[*Example 2*:
|
| 891 |
|
| 892 |
``` cpp
|
| 893 |
template<typename T> concept C = true;
|
|
|
|
| 904 |
specialized because the template arguments are equivalent, but the
|
| 905 |
partial specialization is more constrained [[temp.constr.order]].
|
| 906 |
|
| 907 |
— *end example*]
|
| 908 |
|
| 909 |
+
The template argument list of a partial specialization is the
|
| 910 |
+
*template-argument-list* following the name of the template.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 911 |
|
| 912 |
+
A partial specialization may be declared in any scope in which the
|
| 913 |
+
corresponding primary template may be defined
|
| 914 |
+
[[dcl.meaning]], [[class.mem]], [[temp.mem]].
|
| 915 |
|
| 916 |
+
[*Example 3*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 917 |
|
| 918 |
``` cpp
|
| 919 |
template<class T> struct A {
|
| 920 |
struct C {
|
| 921 |
template<class T2> struct B { };
|
|
|
|
| 930 |
A<short>::C::B<int*> absip; // uses partial specialization #2
|
| 931 |
```
|
| 932 |
|
| 933 |
— *end example*]
|
| 934 |
|
| 935 |
+
Partial specialization declarations do not introduce a name. Instead,
|
| 936 |
+
when the primary template name is used, any reachable partial
|
| 937 |
+
specializations of the primary template are also considered.
|
|
|
|
|
|
|
|
|
|
| 938 |
|
| 939 |
+
[*Note 1*: One consequence is that a *using-declaration* which refers
|
| 940 |
+
to a class template does not restrict the set of partial specializations
|
| 941 |
+
that are found through the *using-declaration*. — *end note*]
|
| 942 |
+
|
| 943 |
+
[*Example 4*:
|
| 944 |
|
| 945 |
``` cpp
|
| 946 |
namespace N {
|
| 947 |
template<class T1, class T2> class A { }; // primary template
|
| 948 |
}
|
|
|
|
| 960 |
— *end example*]
|
| 961 |
|
| 962 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 963 |
parameter. All other non-type arguments are specialized.
|
| 964 |
|
| 965 |
+
Within the argument list of a partial specialization, the following
|
| 966 |
+
restrictions apply:
|
| 967 |
|
| 968 |
- The type of a template parameter corresponding to a specialized
|
| 969 |
+
non-type argument shall not be dependent on a parameter of the partial
|
| 970 |
specialization.
|
| 971 |
+
\[*Example 5*:
|
| 972 |
``` cpp
|
| 973 |
template <class T, T t> struct C {};
|
| 974 |
template <class T> struct C<T, 1>; // error
|
| 975 |
|
| 976 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 977 |
int array[5];
|
| 978 |
template< int X > class A<X,&array> { }; // error
|
| 979 |
```
|
| 980 |
|
| 981 |
— *end example*]
|
| 982 |
+
- The partial specialization shall be more specialized than the primary
|
| 983 |
+
template [[temp.spec.partial.order]].
|
| 984 |
+
- The template parameter list of a partial specialization shall not
|
| 985 |
+
contain default template argument values.[^8]
|
| 986 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 987 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 988 |
template argument list.
|
| 989 |
|
| 990 |
The usual access checking rules do not apply to non-dependent names used
|
| 991 |
to specify template arguments of the *simple-template-id* of the partial
|
| 992 |
specialization.
|
| 993 |
|
| 994 |
+
[*Note 2*: The template arguments can be private types or objects that
|
| 995 |
would normally not be accessible. Dependent names cannot be checked when
|
| 996 |
declaring the partial specialization, but will be checked when
|
| 997 |
substituting into the partial specialization. — *end note*]
|
| 998 |
|
| 999 |
+
#### Matching of partial specializations <a id="temp.spec.partial.match">[[temp.spec.partial.match]]</a>
|
| 1000 |
|
| 1001 |
+
When a template is used in a context that requires an instantiation of
|
| 1002 |
+
the template, it is necessary to determine whether the instantiation is
|
| 1003 |
+
to be generated using the primary template or one of the partial
|
| 1004 |
+
specializations. This is done by matching the template arguments of the
|
| 1005 |
+
template specialization with the template argument lists of the partial
|
| 1006 |
+
specializations.
|
| 1007 |
|
| 1008 |
+
- If exactly one matching partial specialization is found, the
|
| 1009 |
+
instantiation is generated from that partial specialization.
|
| 1010 |
+
- If more than one matching partial specialization is found, the partial
|
| 1011 |
+
order rules [[temp.spec.partial.order]] are used to determine whether
|
| 1012 |
+
one of the partial specializations is more specialized than the
|
| 1013 |
+
others. If such a partial specialization exists, the instantiation is
|
| 1014 |
+
generated from that partial specialization; otherwise, the use of the
|
| 1015 |
+
template is ambiguous and the program is ill-formed.
|
| 1016 |
- If no matches are found, the instantiation is generated from the
|
| 1017 |
primary template.
|
| 1018 |
|
| 1019 |
A partial specialization matches a given actual template argument list
|
| 1020 |
if the template arguments of the partial specialization can be deduced
|
|
|
|
| 1072 |
template <int I> struct B<I, I*2, 2> {}; // OK
|
| 1073 |
```
|
| 1074 |
|
| 1075 |
— *end example*]
|
| 1076 |
|
| 1077 |
+
In a name that refers to a specialization of a class or variable
|
| 1078 |
+
template (e.g., `A<int, int, 1>`), the argument list shall match the
|
| 1079 |
+
template parameter list of the primary template. The template arguments
|
| 1080 |
+
of a partial specialization are deduced from the arguments of the
|
| 1081 |
+
primary template.
|
| 1082 |
|
| 1083 |
+
#### Partial ordering of partial specializations <a id="temp.spec.partial.order">[[temp.spec.partial.order]]</a>
|
| 1084 |
|
| 1085 |
+
For two partial specializations, the first is *more specialized* than
|
| 1086 |
+
the second if, given the following rewrite to two function templates,
|
| 1087 |
+
the first function template is more specialized than the second
|
| 1088 |
+
according to the ordering rules for function templates
|
| 1089 |
[[temp.func.order]]:
|
| 1090 |
|
| 1091 |
- Each of the two function templates has the same template parameters
|
| 1092 |
and associated constraints [[temp.constr.decl]] as the corresponding
|
| 1093 |
partial specialization.
|
|
|
|
| 1141 |
The partial specialization \#2 is more specialized than \#1 because `B`
|
| 1142 |
is more specialized than `A`.
|
| 1143 |
|
| 1144 |
— *end example*]
|
| 1145 |
|
| 1146 |
+
#### Members of class template partial specializations <a id="temp.spec.partial.member">[[temp.spec.partial.member]]</a>
|
| 1147 |
|
| 1148 |
+
The members of the class template partial specialization are unrelated
|
| 1149 |
+
to the members of the primary template. Class template partial
|
| 1150 |
+
specialization members that are used in a way that requires a definition
|
| 1151 |
+
shall be defined; the definitions of members of the primary template are
|
| 1152 |
+
never used as definitions for members of a class template partial
|
| 1153 |
+
specialization. An explicit specialization of a member of a class
|
| 1154 |
+
template partial specialization is declared in the same way as an
|
| 1155 |
+
explicit specialization of a member of the primary template.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1156 |
|
| 1157 |
[*Example 1*:
|
| 1158 |
|
| 1159 |
``` cpp
|
| 1160 |
// primary class template
|
|
|
|
| 1191 |
— *end example*]
|
| 1192 |
|
| 1193 |
If a member template of a class template is partially specialized, the
|
| 1194 |
member template partial specializations are member templates of the
|
| 1195 |
enclosing class template; if the enclosing class template is
|
| 1196 |
+
instantiated [[temp.inst]], [[temp.explicit]], a declaration for every
|
| 1197 |
+
member template partial specialization is also instantiated as part of
|
| 1198 |
+
creating the members of the class template specialization. If the
|
| 1199 |
+
primary member template is explicitly specialized for a given (implicit)
|
| 1200 |
+
specialization of the enclosing class template, the partial
|
| 1201 |
specializations of the member template are ignored for this
|
| 1202 |
specialization of the enclosing class template. If a partial
|
| 1203 |
specialization of the member template is explicitly specialized for a
|
| 1204 |
given (implicit) specialization of the enclosing class template, the
|
| 1205 |
primary member template and its other partial specializations are still
|
|
|
|
| 1222 |
|
| 1223 |
— *end example*]
|
| 1224 |
|
| 1225 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 1226 |
|
| 1227 |
+
#### General <a id="temp.fct.general">[[temp.fct.general]]</a>
|
| 1228 |
+
|
| 1229 |
A function template defines an unbounded set of related functions.
|
| 1230 |
|
| 1231 |
[*Example 1*:
|
| 1232 |
|
| 1233 |
+
A family of sort functions can be declared like this:
|
| 1234 |
|
| 1235 |
``` cpp
|
| 1236 |
template<class T> class Array { };
|
| 1237 |
template<class T> void sort(Array<T>&);
|
| 1238 |
```
|
| 1239 |
|
| 1240 |
— *end example*]
|
| 1241 |
|
| 1242 |
+
[*Note 1*: A function template can have the same name as other function
|
| 1243 |
+
templates and non-template functions [[dcl.fct]] in the same
|
| 1244 |
+
scope. — *end note*]
|
| 1245 |
+
|
| 1246 |
+
A non-template function is not related to a function template (i.e., it
|
| 1247 |
+
is never considered to be a specialization), even if it has the same
|
| 1248 |
+
name and type as a potentially generated function template
|
| 1249 |
+
specialization.[^9]
|
| 1250 |
|
| 1251 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1252 |
|
| 1253 |
It is possible to overload function templates so that two different
|
| 1254 |
function template specializations have the same type.
|
|
|
|
| 1283 |
the relationship between the template parameters and the rest of the
|
| 1284 |
signature.
|
| 1285 |
|
| 1286 |
[*Note 1*:
|
| 1287 |
|
| 1288 |
+
Two distinct function templates can have identical function return types
|
| 1289 |
and function parameter lists, even if overload resolution alone cannot
|
| 1290 |
distinguish them.
|
| 1291 |
|
| 1292 |
``` cpp
|
| 1293 |
template<class T> void f();
|
| 1294 |
+
template<int I> void f(); // OK, overloads the first template
|
| 1295 |
// distinguishable with an explicit template argument list
|
| 1296 |
```
|
| 1297 |
|
| 1298 |
— *end note*]
|
| 1299 |
|
|
|
|
| 1330 |
expression. Two unevaluated operands that do not involve template
|
| 1331 |
parameters are considered equivalent if two function definitions
|
| 1332 |
containing the expressions would satisfy the one-definition rule, except
|
| 1333 |
that the tokens used to name types and declarations may differ as long
|
| 1334 |
as they name the same entities, and the tokens used to form concept-ids
|
| 1335 |
+
[[temp.names]] may differ as long as the two *template-id*s are the same
|
| 1336 |
+
[[temp.type]].
|
| 1337 |
|
| 1338 |
[*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
|
| 1339 |
type. — *end note*]
|
| 1340 |
|
| 1341 |
Two *lambda-expression*s are never considered equivalent.
|
| 1342 |
|
| 1343 |
[*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
|
| 1344 |
signature of a function template with external linkage. — *end note*]
|
| 1345 |
|
| 1346 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 1347 |
+
only the name itself is considered, not the result of name lookup.
|
| 1348 |
+
|
| 1349 |
+
[*Note 5*: If such a dependent name is unqualified, it is looked up
|
| 1350 |
+
from the first declaration of the function template
|
| 1351 |
+
[[temp.dep.candidate]]. — *end note*]
|
| 1352 |
|
| 1353 |
[*Example 3*:
|
| 1354 |
|
| 1355 |
``` cpp
|
| 1356 |
template <int I, int J> void f(A<I+J>); // #1
|
|
|
|
| 1359 |
template <class T> decltype(g(T())) h();
|
| 1360 |
int g(int);
|
| 1361 |
template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup…
|
| 1362 |
{ return g(T()); } // …{} although the lookup here does find g(int)
|
| 1363 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 1364 |
+
// not considered at the first declaration of h()
|
| 1365 |
|
| 1366 |
// ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
|
| 1367 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 1368 |
template <int N> void foo(const char (*s)[([]{}, N)]);
|
| 1369 |
|
|
|
|
| 1380 |
same value. Two unevaluated operands that are not equivalent are
|
| 1381 |
functionally equivalent if, for any given set of template arguments, the
|
| 1382 |
expressions perform the same operations in the same order with the same
|
| 1383 |
entities.
|
| 1384 |
|
| 1385 |
+
[*Note 6*: For instance, one could have redundant
|
| 1386 |
parentheses. — *end note*]
|
| 1387 |
|
| 1388 |
+
[*Example 4*:
|
| 1389 |
+
|
| 1390 |
+
``` cpp
|
| 1391 |
+
template<int I> concept C = true;
|
| 1392 |
+
template<typename T> struct A {
|
| 1393 |
+
void f() requires C<42>; // #1
|
| 1394 |
+
void f() requires true; // OK, different functions
|
| 1395 |
+
};
|
| 1396 |
+
```
|
| 1397 |
+
|
| 1398 |
+
— *end example*]
|
| 1399 |
+
|
| 1400 |
Two *template-head*s are *equivalent* if their
|
| 1401 |
*template-parameter-list*s have the same length, corresponding
|
| 1402 |
*template-parameter*s are equivalent and are both declared with
|
| 1403 |
*type-constraint*s that are equivalent if either *template-parameter* is
|
| 1404 |
declared with a *type-constraint*, and if either *template-head* has a
|
|
|
|
| 1418 |
rules above are used to compare expressions involving template
|
| 1419 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 1420 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 1421 |
template argument lists.
|
| 1422 |
|
| 1423 |
+
If the validity or meaning of the program depends on whether two
|
| 1424 |
+
constructs are equivalent, and they are functionally equivalent but not
|
| 1425 |
+
equivalent, the program is ill-formed, no diagnostic required.
|
| 1426 |
+
Furthermore, if two function templates that do not correspond
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1427 |
|
| 1428 |
+
- have the same name,
|
| 1429 |
+
- have corresponding signatures [[basic.scope.scope]],
|
| 1430 |
+
- would declare the same entity [[basic.link]] considering them to
|
| 1431 |
+
correspond, and
|
| 1432 |
+
- accept and are satisfied by the same set of template argument lists,
|
| 1433 |
+
|
| 1434 |
+
the program is ill-formed, no diagnostic required.
|
| 1435 |
+
|
| 1436 |
+
[*Note 7*:
|
| 1437 |
|
| 1438 |
This rule guarantees that equivalent declarations will be linked with
|
| 1439 |
one another, while not requiring implementations to use heroic efforts
|
| 1440 |
to guarantee that functionally equivalent declarations will be treated
|
| 1441 |
as distinct. For example, the last two declarations are functionally
|
|
|
|
| 1457 |
|
| 1458 |
— *end note*]
|
| 1459 |
|
| 1460 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 1461 |
|
| 1462 |
+
If multiple function templates share a name, the use of that name can be
|
| 1463 |
+
ambiguous because template argument deduction [[temp.deduct]] may
|
| 1464 |
+
identify a specialization for more than one function template. *Partial
|
| 1465 |
+
ordering* of overloaded function template declarations is used in the
|
| 1466 |
+
following contexts to select the function template to which a function
|
| 1467 |
+
template specialization refers:
|
|
|
|
| 1468 |
|
| 1469 |
- during overload resolution for a call to a function template
|
| 1470 |
specialization [[over.match.best]];
|
| 1471 |
- when the address of a function template specialization is taken;
|
| 1472 |
- when a placement operator delete that is a function template
|
| 1473 |
+
specialization is selected to match a placement operator new
|
| 1474 |
+
[[basic.stc.dynamic.deallocation]], [[expr.new]];
|
| 1475 |
- when a friend function declaration [[temp.friend]], an explicit
|
| 1476 |
instantiation [[temp.explicit]] or an explicit specialization
|
| 1477 |
[[temp.expl.spec]] refers to a function template specialization.
|
| 1478 |
|
| 1479 |
Partial ordering selects which of two function templates is more
|
|
|
|
| 1623 |
template<class T > void f(T); // #2
|
| 1624 |
template<class T, class... U> void g(T*, U...); // #3
|
| 1625 |
template<class T > void g(T); // #4
|
| 1626 |
|
| 1627 |
void h(int i) {
|
| 1628 |
+
f(&i); // OK, calls #2
|
| 1629 |
+
g(&i); // OK, calls #3
|
| 1630 |
}
|
| 1631 |
```
|
| 1632 |
|
| 1633 |
— *end example*]
|
| 1634 |
|
|
|
|
| 1678 |
|
| 1679 |
void f(C auto &, auto &) = delete;
|
| 1680 |
template <C Q> void f(Q &, C auto &);
|
| 1681 |
|
| 1682 |
void g(struct A *ap, struct B *bp) {
|
| 1683 |
+
f(*ap, *bp); // OK, can use different methods to produce template parameters
|
| 1684 |
}
|
| 1685 |
|
| 1686 |
template <typename T, typename U> struct X {};
|
| 1687 |
|
| 1688 |
template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
|
| 1689 |
template <C T, C U, C V> bool operator==(T, X<U, V>);
|
| 1690 |
|
| 1691 |
void h() {
|
| 1692 |
+
X<void *, int>{} == 0; // OK, correspondence of [T, U, V] and [U, V, T]
|
| 1693 |
}
|
| 1694 |
```
|
| 1695 |
|
| 1696 |
— *end example*]
|
| 1697 |
|
|
|
|
| 1712 |
[*Example 1*:
|
| 1713 |
|
| 1714 |
``` cpp
|
| 1715 |
template<class T> struct Alloc { ... };
|
| 1716 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 1717 |
+
Vec<int> v; // same as vector<int, Alloc<int>> v;
|
| 1718 |
|
| 1719 |
template<class T>
|
| 1720 |
void process(Vec<T>& v)
|
| 1721 |
{ ... }
|
| 1722 |
|
|
|
|
| 1729 |
|
| 1730 |
f(v); // error: Vec not deduced
|
| 1731 |
|
| 1732 |
template<template<class,class> class TT>
|
| 1733 |
void g(TT<int, Alloc<int>>);
|
| 1734 |
+
g(v); // OK, TT = vector
|
| 1735 |
```
|
| 1736 |
|
| 1737 |
— *end example*]
|
| 1738 |
|
| 1739 |
However, if the *template-id* is dependent, subsequent template argument
|
|
|
|
| 1785 |
A *concept* is a template that defines constraints on its template
|
| 1786 |
arguments.
|
| 1787 |
|
| 1788 |
``` bnf
|
| 1789 |
concept-definition:
|
| 1790 |
+
concept concept-name attribute-specifier-seqₒₚₜ '=' constraint-expression ';'
|
| 1791 |
```
|
| 1792 |
|
| 1793 |
``` bnf
|
| 1794 |
concept-name:
|
| 1795 |
identifier
|
| 1796 |
```
|
| 1797 |
|
| 1798 |
A *concept-definition* declares a concept. Its *identifier* becomes a
|
| 1799 |
+
*concept-name* referring to that concept within its scope. The optional
|
| 1800 |
+
*attribute-specifier-seq* appertains to the concept.
|
| 1801 |
|
| 1802 |
[*Example 1*:
|
| 1803 |
|
| 1804 |
``` cpp
|
| 1805 |
template<typename T>
|
|
|
|
| 1815 |
T f2(T x) { return x; }
|
| 1816 |
```
|
| 1817 |
|
| 1818 |
— *end example*]
|
| 1819 |
|
| 1820 |
+
A *concept-definition* shall inhabit a namespace scope
|
| 1821 |
[[basic.scope.namespace]].
|
| 1822 |
|
| 1823 |
A concept shall not have associated constraints [[temp.constr.decl]].
|
| 1824 |
|
| 1825 |
A concept is not instantiated [[temp.spec]].
|
| 1826 |
|
| 1827 |
[*Note 1*: A concept-id [[temp.names]] is evaluated as an expression. A
|
| 1828 |
concept cannot be explicitly instantiated [[temp.explicit]], explicitly
|
| 1829 |
+
specialized [[temp.expl.spec]], or partially specialized
|
| 1830 |
+
[[temp.spec.partial]]. — *end note*]
|
| 1831 |
|
| 1832 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 1833 |
operand [[expr.context]].
|
| 1834 |
|
| 1835 |
The first declared template parameter of a concept definition is its
|