Assume the following C++ concept
template <typename T>
concept has_set = requires(T t, std::string s) {
{ t.set(s) } -> std::same_as<void>; };
};
I cannot use a concept for parameter s
, e.g.
template <typename T>
concept has_set = requires(T t, std::convertible_to<std::string_view> s) {
{ t.set(s) } -> std::same_as<void>; };
};
will fail to compile. Is there any workaround or trick which can be applied here to achieve precisely that goal ?
Note:
This question has a follow-up one here:
How to apply a concept to a member function in a concept and then use it?.
The follow-up question adds a requirement that the usage of the concept should be possible without specifying the concept template arguments explicitly.
In this case you can add another template parameter to the concept, and require it to be convertible to std::string_view
:
#include <type_traits>
#include <string_view>
template <typename T, typename S>
concept has_set = requires(T t, S s) {
requires std::convertible_to<S, std::string_view>;
{ t.set(s) } -> std::same_as<void>;
};