Proceeding with my studies on concepts
, and after looking here, here and here, among other pages, I wonder how could I declare a function type that returns a std::optional
of another concept
, like below:
template <typename t>
concept event = requires(t p_t) {
{ t::id } -> std::same_as<const size_t &>;
requires std::default_initializable<t>;
};
using func = std::function<std::optional<event>(int)>;
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
reports Too few template arguments for concept 'event'
I tried
template <typename t>
using func = std::function<std::optional<event<t>>(int)>;
which generated the error Template argument for template type parameter must be a type
Is there a way to do it?
That is not possible. A concept is not a type. It's purpose is to make setting constraints easier (for example in contexts where you'd use std::enable_if
pre C++20).
Example:
// Pre C++20 SFINAE
template<typename T>
struct event_sfinae {
static constexpr bool value = ...;
};
template<typename T, typename = std::enable_if_t<event_sfinae<T>::value>>
void foo1(T arg) {
...
}
// C++20 concept version
template<typename T>
concept event_concept = ...; // Define concept
template<event_concept T> // only types that satisfy the constraints of 'event_concept' allowed
void foo2(T arg) {
...
}