在 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());
}
Run Code Online (Sandbox Code Playgroud)
我们如何使用具有非空参数的函数来做到这一点?