How to write a concept for member function taking a callback as an argument?

40 views Asked by At

I want to write a concept for a class which has a member function and this member function takes an invocable as its argument:

#include <concepts>

struct Foo {
    void process(std::invocable<int> auto&& callback);
};

template<typename T>
concept C = requires(T t) {
    // ??
};
static_assert(C<Foo>);

I can't write requires(T t, std::invocable<int> f) as compiler doesn't allow to do so. I don't want to put invocable as template argument because I won't be able to write void foo(C auto&& c) without specifying invocable's type.

For now I came up with this solution but it additionally restricts return type:

template<typename T>
concept C2 = requires(T t) {
    t.process(std::declval<std::function<void(int)>>());
};
static_assert(C2<Foo>);

Are there any better solutions?

Thanks for you help.

0

There are 0 answers