| 1 |
When defining members of class templates outside of their class interfaces the |
| 2 |
members' template headers must match the class templates' template |
| 3 |
headers. This is no different when using concepts. |
| 4 |
|
| 5 |
In the following example the concept tt(Addable) is used when defining the |
| 6 |
class template tt(Data). The class tt(Data) declares a |
| 7 |
member tt(process), implemented below the class interface. Like the class of |
| 8 |
which it is a member its header must also specify tt(Addable) (cf. section |
| 9 |
ref(CONDEF)): |
| 10 |
verb( template <Addable Type> |
| 11 |
class Data |
| 12 |
{ |
| 13 |
void process(); |
| 14 |
}; |
| 15 |
|
| 16 |
template <Addable Tp> // The concept must be specified, |
| 17 |
void Data<Tp>::process() // but the formal type name |
| 18 |
{ // doesn't have to be `Type' |
| 19 |
... |
| 20 |
}) |
| 21 |
|
| 22 |
Comparably, if a class template member function can only be used when a |
| 23 |
constraint has been satisfied (but no additional constraints apply to other |
| 24 |
class members), the class template's header can use tt(typename) and the |
| 25 |
(additional) constraint can be tailored to members where applicable: |
| 26 |
verb( template <typename Type> // generic template type parameter |
| 27 |
class Data |
| 28 |
{ |
| 29 |
void process() requires Addable<Type>; // additional requirement |
| 30 |
}; |
| 31 |
|
| 32 |
template <typename X> |
| 33 |
void Data<X>::process() requires Addable<X> |
| 34 |
...) |
| 35 |
|
| 36 |
Types of member templates themselves may also be constrained. Here too the |
| 37 |
rule applies that the template headers of member implementations must match |
| 38 |
those of their declarations: |
| 39 |
verb( template <typename Type> |
| 40 |
class Data |
| 41 |
{ |
| 42 |
template <Addable Tp> // constraint applied to |
| 43 |
void process(Tp par); // a member template |
| 44 |
}; |
| 45 |
|
| 46 |
template <typename Type> |
| 47 |
template <Addable Tp> |
| 48 |
void Data<Type>::process(Tp par) |
| 49 |
{ |
| 50 |
... |
| 51 |
}) |
| 52 |
|