Types of Constraints
1. Conjunctions
It combines multiple constraints using AND (&) operator. It specifies that all the constraints within the conjunction must be satisfied for the overall constraint to be satisfied.
template <typename T> requires Constraint1 && Constraint2 && ... && ConstraintN void functionName(T parameter) { // Function body }
2. Disjunctions
It is used to combine multiple constraints with the help of the OR (||) operator. It specifies that at least one of the constraints within the disjunction must be satisfied for the overall constraint to be satisfied.
template <typename T> requires Constraint1 || Constraint2 || ... || ConstraintN void functionName(T parameter) { // Function body }
3. Atomic constraints
They are individual constraints that define specific requirements on types, expressions, or template arguments.
template <typename T> concept ConceptName = Constraint1 && Constraint2 && ... && ConstraintN; template <typename T> requires ConceptName<T> void functionName(T parameter) { // Function body }
Constraints and Concepts in C++ 20
In this article, we will learn about the constraints and concepts with the help of examples in a simple way. So, it is a feature that was introduced in the latest version i.e. C++20 that specifies requirements on template arguments and enables more expressive and readable code.