C ++ 20概念如何定义带参数的函数的存在?

C++20 concepts how to define existence of a function with arguments?

在 C++20 中,我们现在可以使用概念而不是 SFINAE 来确定函数是否存在于模板类型名中:

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());
}

我们如何使用具有非空参数的函数执行此操作?

最好的选择似乎是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());
}

您可能在 requires 中有额外的参数:

template<typename T> concept fooable = requires (T a, int i) {
    a.foo(i);
};

Demo