| 1 |
As we have seen (cf. section ref(CXXMULTI)) concepts may process |
| 2 |
template parameter packs. Such concepts are called |
| 3 |
hi(concept: variadic) emi(variadic concepts). When defining |
| 4 |
concept-protected variadic function or class templates variadic concepts |
| 5 |
aren't always required. Consider the following function: |
| 6 |
verbinsert(-s4 //sumfun examples/parampacks.cc) |
| 7 |
|
| 8 |
Here we see a variadic template, but it defines all its parameters as |
| 9 |
constrained types by simply mentioning the concept tt(HasSize) instead of just |
| 10 |
tt(typename). The tt(HasSize) concept is very basic: it merely requires that |
| 11 |
tt(type.size()) exists, returning a tt(size_t): |
| 12 |
verbinsert(-s4 //hassize examples/parampacks.cc) |
| 13 |
|
| 14 |
Once tt(fun) has verified that all its argument types satisfy the tt(HasSize) |
| 15 |
requirements no additional checks are necessary. The tt(fun) function |
| 16 |
template merely forwards its arguments to tt(sum), a variadic template, that |
| 17 |
simply adds the return values of the tt(size()) members of its arguments: |
| 18 |
verbinsert(-s4 //sum examples/parampacks.cc) |
| 19 |
|
| 20 |
The wrapper function tt(fun) isn't really required. The variadic template |
| 21 |
function summing the various tt(size()) values itself can also be defined so |
| 22 |
that its types themselves must satisfy the tt(HasSize) concept. Here is the |
| 23 |
definition of the variadic function template tt(sum2) requiring precisely |
| 24 |
that: |
| 25 |
verbinsert(-s4 //sum2 examples/parampacks.cc) |
| 26 |
|
| 27 |
And here is a tt(main) function calling tt(fun) and tt(sum2): |
| 28 |
verbinsert(-s4 //main examples/parampacks.cc) |
| 29 |
|
| 30 |
On the other hand, the predefined concept tt(std::constructible_from) em(is) a |
| 31 |
variadic concept, as it accepts a LHS template parameter and a RHS parameter |
| 32 |
pack. This concept is satisfied if the LHS parameter can be constructed |
| 33 |
from the types specified in its RHS parameter pack. After including |
| 34 |
tt(type_trait) defining and using such a concept is not very hard: |
| 35 |
verbinsert(-s4 //constructible examples/constructiblefrom.cc) |
| 36 |
|
| 37 |
The recipe for writing variadic concepts is not very complex: |
| 38 |
itemization( |
| 39 |
it() start the concept's definition with a template header specifying a |
| 40 |
parameter pack; |
| 41 |
it() pass the parameters to a type trait handling the pack; |
| 42 |
) |
| 43 |
To use the variadic concept in a function or class template its template |
| 44 |
parameters are simply forwarded to the concept (as shown in the above |
| 45 |
example). |
| 46 |
|
| 47 |
When no predefined variadic type trait is available the variadic concept must |
| 48 |
use other means to determine whether its constraints are satisfied or not. In |
| 49 |
those cases define your own variadic type traits. For illustration let's |
| 50 |
assume we are looking for a variadic concept that can be used to verify that |
| 51 |
the types of all the arguments that are passed to a variadic function template |
| 52 |
are integral types. In this case there is no predefined type trait we can use, |
| 53 |
so we have to define it ourselves. We define the concept tt(IntegralOnly) as a |
| 54 |
variadic concept using our self-defined type trait tt(allIntegralTypes), and |
| 55 |
thereupon use it when defining a function requiring that all of its arguments |
| 56 |
are integral values: |
| 57 |
verbinsert(-s4 //concept examples/variadic.cc) |
| 58 |
|
| 59 |
The generic type trait tt(allIntegralTypes) merely specifies that it accepts |
| 60 |
any number of type parameters and uses specializations to handle specific |
| 61 |
cases. One specific case is the case were no types are specified which simply |
| 62 |
defines a tt(true static bool const) value: |
| 63 |
verbinsert(-s4 //generic examples/variadic.cc) |
| 64 |
|
| 65 |
The type trait's partial specialization does the hard work: it determines |
| 66 |
whether the first type is integral and combines that (using tt(and)) with the |
| 67 |
tt(value) made available by the tt(struct allIntegralType) receiving the |
| 68 |
remaining types: |
| 69 |
verbinsert(-s4 //partial examples/variadic.cc) |
| 70 |
|
| 71 |
The function tt(fun) can now be called with any number of arguments. As long |
| 72 |
as the arguments are integral types the compilation succeeds and tt(fun) can |
| 73 |
safely do its job. |