In C++20, we can now use concepts instead of SFINAE to figure out whether a function exists in a template typename:
template<typename T> concept fooable = requires (T a) {
    a.foo();
};
class Foo {
public:
    // If commented out, will fail compilation.
    void foo() {}
    void bar() {}
};
template <typename T> requires fooable<T>
void foo_it(T t) {
    t.bar();
}
int main()
{
    foo_it(Foo());
}
How do we do this with functions that have non-empty arguments?
You might have extra parameters in requires:
template<typename T> concept fooable = requires (T a, int i) {
    a.foo(i);
};
Demo
The best option seems to be declval:
template<typename T> concept fooable = requires (T a) {
    a.foo(std::declval<int>());
};
class Foo {
public:
    void foo(int x) {}
    void bar() {}
};
template <typename T> requires fooable<T>
void foo_it(T t) {
    t.bar();
}
int main()
{
    foo_it(Foo());
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With