如何在C ++中检查编译时是否存在特定的运算符重载?

a_p*_*han 3 c++ templates sfinae c++11

我有一个类,它为多个输入类型重载了()运算符

struct Type {
    void operator()(int);
    void operator()(std::string);
};
Run Code Online (Sandbox Code Playgroud)

现在,我想使用SFINAE来检查()运算符是否存在特定的重载,即

if (Type()(std::string) overload exists) {
    // do something...
}
Run Code Online (Sandbox Code Playgroud)

在C ++ 11中可以做到吗?(我不能使用C ++ 14或C ++ 17)。

注意:在实际代码中,有一个模板类,该类接受具有某些属性的类类型。该模板中有一个成员函数,该成员函数将根据是否存在参数类型的()运算符的某些特定重载进行专门化。

Hol*_*Cat 5

使用SFINAE来检查是否有可能使您的成员指向operator()您要查找的重载。

这是我的方法:

#include <type_traits>

template <typename A, typename B, typename = void>
struct has_overload : std::false_type {};
template <typename A, typename B>
struct has_overload<A, B, decltype((void)(void (A::*)(B))&A::operator())> : std::true_type {};
Run Code Online (Sandbox Code Playgroud)

用法:has_overload<Type, std::string>::value