Requirements on Return Types
Explore how to specify return type constraints in C++20 Concepts using compound requirements. Understand how to enforce precise type conditions on function return values for safer, more reliable generic programming.
We'll cover the following...
We'll cover the following...
We’ve seen how to write a requirement expressing that certain functions must exist in a class API.
Constrain the return type
But did we constrain the return type of those functions?
template <typename T>
concept HasSquare = requires (T t) {
t.square();
t.sqrt();
};
No, we didn’t. A class would satisfy the constraints of the HasSquare concept with int square() and void square() both.
If we want to specify the expected return type, we must use a compound requirement. ...