| 1 |
When return types of operations must satisfy certain requirements then |
| 2 |
em(compound requirements) should be used. Compound requirements define type |
| 3 |
constraints on expressions embedded in compound statements. The C2a standard |
| 4 |
defines several concepts that can be used to specify such requirements (see |
| 5 |
also section ref(CXXCONCEPTS) below). Here is an example: |
| 6 |
verb( template <typename Type, typename ReturnType> |
| 7 |
concept Return = |
| 8 |
requires(Type par) |
| 9 |
{ |
| 10 |
// par[..] must return a `ReturnType' |
| 11 |
{ par[0] } -> std::same_as<ReturnType>; |
| 12 |
};) |
| 13 |
|
| 14 |
This concept can now be used to specify requirements of template type |
| 15 |
parameters. E.g., |
| 16 |
verb( template <typename Type, typename RetType> |
| 17 |
requires Return<Type, RetType> |
| 18 |
RetType fun(Type tp) |
| 19 |
{ |
| 20 |
return tp[0]; |
| 21 |
}) |
| 22 |
|
| 23 |
Here arguments passed to tt(fun) must satify two requirements: |
| 24 |
itemization( |
| 25 |
it() they must provide an index operator accepting integral argument |
| 26 |
values; |
| 27 |
it() their index operators must return tt(std::string) values. |
| 28 |
) |
| 29 |
|
| 30 |
You may have noticed that the tt(std::same_as) concept receives only one |
| 31 |
template type argument, which (as if by magic) compares it with the type |
| 32 |
returned by the tt(par[0]) expression. When peeking at the available concepts |
| 33 |
in section ref(CXXCONCEPTS) you will see that several of those concepts in |
| 34 |
fact define two template type parameters. When these concepts are used in |
| 35 |
compound requirements then the compiler passes the deduced type of the |
| 36 |
expression in the concept's compound statement (so that's the type of |
| 37 |
tt(par[0]) in the above example) to the concept's first type, and passes the |
| 38 |
explicitly specified type to the concept's second type. |
| 39 |
|
| 40 |
Knowing this we can define our own concepts to use in compound |
| 41 |
expressions. We may define our own tt(same_as) concept as follows, using a |
| 42 |
separate class template tt(SameTypes). tt(SameTypes) defines a |
| 43 |
tt(bool) value `tt(value)' which is used to decide about the concept's |
| 44 |
requirement. The class template tt(SameTypes) uses a specialization to handle |
| 45 |
the situation where both types are equal. Note that concepts |
| 46 |
hi(concept: specialization) themselves cannot be specialized: |
| 47 |
verb( template <typename Lhs, typename Rhs> |
| 48 |
struct SameTypes // generic: any two types |
| 49 |
{ |
| 50 |
static bool const value = false; |
| 51 |
}; |
| 52 |
template <typename Lhs> |
| 53 |
struct SameTypes<Lhs, Lhs> //specialization: equal types |
| 54 |
{ |
| 55 |
static bool const value = true; |
| 56 |
}; |
| 57 |
|
| 58 |
template<typename Compound, typename Specified> |
| 59 |
concept Same = SameTypes::value;) |
| 60 |
|
| 61 |
Now the concept tt(Same) can be used instead of tt(std::same_as) by merely |
| 62 |
specifying the required type: |
| 63 |
verb( template <typename Type, typename ReturnType> |
| 64 |
concept Return = |
| 65 |
requires(Type par) |
| 66 |
{ |
| 67 |
// par[..] must return a `ReturnType' |
| 68 |
{ par[0] } -> Same<ReturnType>; |
| 69 |
};) |
| 70 |
Although in this case it isn't important which actual type is used as |
| 71 |
argument for which concept type parameter, the compiler specifies the compound |
| 72 |
expression's type as template argument for tt(Same's Compound) parameter |
| 73 |
whereas tt(ReturnType) is used as template argument for tt(Same's Specified) |
| 74 |
parameter. |
| 75 |
|
| 76 |
Multiple type requirements can be specified by providing multiple compound |
| 77 |
requirements as in the following example: |
| 78 |
verb( template <typename Type> |
| 79 |
concept MultiArgs = |
| 80 |
requires(Type lhs, Type rhs) |
| 81 |
{ |
| 82 |
{ lhs + rhs } -> std::same_as<Type>; |
| 83 |
{ lhs += rhs } -> std::same_as<Type &>; |
| 84 |
{ lhs.c_str() } -> std::same_as<char const *>; |
| 85 |
};) |
| 86 |
|
| 87 |
If it is required that the compound operation doesn't throw exceptions then |
| 88 |
tt(noexcept) can be written immediately following the compound requirement's |
| 89 |
late return type arrow (tt(->)). The tt(noexcept) specification itself may |
| 90 |
then optionally be followed by a type constraint. |
| 91 |
|
| 92 |
Finally, the late return type specifications itself is optional, in which case |
| 93 |
the compound requirement acts like a simple requirement: it requires the |
| 94 |
existence of the expression that's specified in the compound statement. In |
| 95 |
this case: don't forget to add the semicolon following the closing parenthesis |
| 96 |
of the compound requirement: |
| 97 |
verb( template <typename Type> |
| 98 |
concept Increment = |
| 99 |
requires(Type par) |
| 100 |
{ |
| 101 |
{ ++par }; |
| 102 |
// same as: |
| 103 |
++par; |
| 104 |
};) |
| 105 |
|