Composing constraints
We have seen multiple examples of constraining template arguments but in all the cases so far, we used a single constraint. It is possible though for constraints to be composed using the &&
and ||
operators. A composition of two constraints using the &&
operator is called a conjunction and the composition of two constraints using the ||
operator is called a disjunction.
For a conjunction to be true, both constraints must be true. Like in the case of logical AND operations, the two constraints are evaluated from left to right, and if the left constraint is false, the right constraint is not evaluated. Let’s look at an example:
template <typename T> requires std::is_integral_v<T> && std::is_signed_v<T> T decrement(T value) { return value--; }
In this snippet, we have a function template that returns the decremented value of the received argument. However, it only accepts signed integral...