C++ requires Keyword
The requires keyword in C++20 is used to define constraints for templates and concepts. It provides a way to specify conditions that must be satisfied for a template or function to be instantiated. This makes templates more expressive and easier to debug by explicitly stating the requirements for their usage.
The requires keyword can be used in two main ways:
- As a part of a
requiresclause to constrain templates. - In a
requiresexpression to evaluate conditions within a concept.
Syntax
Requires Clause: A requires clause specifies conditions that must be satisfied for the template to be instantiated.
template
requires condition
return_type function_name(T parameter);
Requires Expression: A requires expression defines a set of requirements for a type or a set of operations.
requires (condition1 && condition2) {
// Requirements
}
Examples
Example 1: Using requires Clause
This example uses a requires clause to constrain a template function that requires the type T to support addition.
#include <iostream>
#include <concepts>
using namespace std;
template<typename T>
requires requires (T a, T b) { a + b; }
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Sum of 3 and 5: " << add(3, 5) << endl;
// Uncommenting the following line will cause a compilation error
// because strings do not support direct addition.
// cout << "Sum of strings: " << add(string("Hello"), string("World")) << endl;
return 0;
}
Output:
Sum of 3 and 5: 8
Explanation:
- The
requiresclause ensures that the typeTsupports the+operator. - If a type does not satisfy this condition, a compile-time error occurs, improving error diagnostics.
- The function
addworks correctly for integers, as they support addition.
Example 2: Using requires Expression
This example demonstrates a requires expression inside a concept to define constraints for a type.
#include <iostream>
#include <concepts>
using namespace std;
template<typename T>
concept Multipliable = requires (T a, T b) {
{ a * b } -> convertible_to<T>;
};
template<Multipliable T>
T multiply(T a, T b) {
return a * b;
}
int main() {
cout << "Product of 4 and 5: " << multiply(4, 5) << endl;
return 0;
}
Output:
Product of 4 and 5: 20
Explanation:
- The
Multipliableconcept uses arequiresexpression to ensure that the type supports multiplication. - The template function
multiplyis constrained to types that satisfy theMultipliableconcept. - The program works for integers as they support multiplication, and the result is printed.
Key Points to Remember about requires Keyword
- The
requireskeyword is used to define constraints for templates and concepts in C++20. - It can be used in a
requiresclause or as arequiresexpression. - Constraints defined with
requireshelp ensure that templates are only instantiated with valid types. - Using
requiresimproves code clarity, maintainability, and error diagnostics. - The
requireskeyword is a powerful tool for modern C++ template programming.
