尝试使用 enable_if 启用 SFINAE 时出现“无法重载”错误

use*_*996 2 c++ templates sfinae template-meta-programming c++11

我只是std::enable_if在 C++ 中尝试使用 SFINAE 。我以为我理解了理论部分,直到我无法编译以下代码。更令人困惑的是 Visual Studio 和 Linux 上的不同行为。只要您不取消注释 ( Calculator<int> cInt;),此粘贴的代码就会在 VS 上编译。但是,使用 GCC 它会给我编译错误。我已经在 STL 实现中看到了这种代码,我真的很期待到处都有更标准化的实现。无论如何,您能否检查并建议我在这里的理解有什么差距?

template<typename T>
class Calculator
{
public:
    typename enable_if<is_arithmetic<T>::value, T>::type 
    addition(T a, T b)
    {
        return a + b;
    }
    typename enable_if<!is_arithmetic<T>::value, T>::type
    addition(T a, T b)
    {
        cout << "Default\n";
        return a;
    }
};

void SFINAE()
{
//  Calculator<int> cInt; 
}


int main ()
{
    SFINAE();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC 8.1 的错误日志:j

doodle.cpp:30:3: error: 'typename std::enable_if<(! std::is_arithmetic<_Tp>::value), T>::type Calculator<T>::addition(T, T)' cannot be overloaded with 'typename std::enable_if<std::is_arithmetic<_Tp>::value, T>::type Calculator<T>::addition(T, T)'
   addition(T a, T b)
   ^~~~~~~~
jdoodle.cpp:25:3: note: previous declaration 'typename std::enable_if<std::is_arithmetic<_Tp>::value, T>::type Calculator<T>::addition(T, T)'
   addition(T a, T b)
Run Code Online (Sandbox Code Playgroud)

当您使用 int 取消对 Calculator 类初始化的注释时,VS 上的错误日志:

sfinae.h(17): error C3646: 'addition': unknown override specifier
sfinae.h(17): error C2059: syntax error: '('
sfinae.h(18): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Run Code Online (Sandbox Code Playgroud)

max*_*x66 5

在一个类中,SFINAE 适用于模板方法和方法的模板参数。

所以

typename enable_if<is_arithmetic<T>::value, T>::type 
addition(T a, T b)
{
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

无法工作,因为您试图将 SFINAE 应用于非模板的方法和is_arithmetic<T>::value类的模板参数上的测试 ( )。

你应该尝试一些东西

template <typename U = T>
typename enable_if<is_arithmetic<U>::value, T>::type 
addition(T a, T b)
{
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

这样,模板就变成了模板参数 ( U) 和默认类型 ( T) 的模板,并且您可以对方法的模板参数进行 SFINAE 测试。

其他addition()方法也一样。

为避免有人“劫持”您的代码以解释错误的模板参数

Calculator<std::string>  cs;

cs.add("a", "b");  // call the Default version
cs.template add<int>("a", "b"); // call the arithmetic version!!!
Run Code Online (Sandbox Code Playgroud)

你可以强加那个U并且T是相同的类型

template <typename U = T>
typename std::enable_if<std::is_arithmetic<U>::value
                     && std::is_same<T, U>::value, T>::type 
addition(T a, T b) //   ^^^^^^^^^^^^^^^^^^^^^^^^^
{
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)