Answers for "C++ concept simple requirements"

C++
0

C++ concept simple requirements

/*
A simple requirement is an arbitrary expression statement that does not 
start with the keyword requires. It asserts that the expression is valid. 
The expression is an unevaluated operand; only language correctness is checked.
*/

template<typename T>
concept Addable = requires (T a, T b)
{
    a + b; // "the expression a+b is a valid expression that will compile"
};
 
template<class T, class U = T>
concept Swappable = requires(T&& t, U&& u)
{
    swap(std::forward<T>(t), std::forward<U>(u));
    swap(std::forward<U>(u), std::forward<T>(t));
};

/*
A requirement that starts with the keyword requires is always interpreted
as a nested requirement. Thus a simple requirement cannot start with an 
unparenthesized requires-expression.
*/
Posted by: Guest on April-17-2022

Browse Popular Code Answers by Language