The Requires Clause
The requires clause can take four kinds of requirements:
- Simple requirements
- Nested requirements
- Compound requirements
- Type requirements
Simple requirements: expressions only checked for valid syntax
template <typename T>
concept TinyType = requires(T t) {
sizeof(T) <= 4;
};
Nested requirements: Also checks if the expression is true
template <typename T>
concept TinyType = requires(T t) {
sizeof(T) <= 4;
requires sizeof(T) <= 4;
};
Compound requirements: Checks for valid syntax, doesn't throw exceptions, and the result is convertible
#include <concepts>
template <typename T>
concept Addable = requires(T a, T b) {
{ a + b } noexcept -> std::convertible_to<int>;
};