Reputation: 3101
Imagine I have a callable template-parameter "Fn fn". Now I want to check that its return-type is a signed scalar. How can I do that with C++20 concepts ?
Upvotes: 0
Views: 144
Reputation: 170153
To check if it can be called and return something of a category, just call it and examine the expression. That's what concepts are after all, a tool to write regular C++ code into and examine some of its properties.
template<typename T>
concept signed_scalar = std::signed_integral<T> || std::floating_point<T>;
template<typename Fn, typename... Args>
concept signed_result = requires(Fn fn, Args... args) {
{ fn(args...) } -> signed_scalar;
};
That's the only generic way given that overloading is always possible in any C++ program, including on a functor's operator()
.
You can also be fancy and use std::invoke
in lieu of the bare function call expression, to handle more "invokable" types. But you did not give an indication that's a requirement.
Upvotes: 5