使用enable_if将数字作为函数参数进行匹配

时间:2018-04-30 08:19:13

标签: c++ c++11 enable-if

我想使用std::enable_if来构建匹配数字的构造函数。我尝试了以下代码,但找不到构造函数。主要功能有我想要如何使用它的例子。我应该改变什么来使这项工作?

#include <type_traits>
#include <string>

class A {
public:
    A(const std::wstring&, const std::wstring&)
    {}

    template<typename T>
    A(const std::wstring& n, typename std::enable_if<std::is_arithmetic<T>::value, T>::type v)
    {}
};

int main() {
    A(L"n", 1234); // does not compile
    A(L"n", 2.7); // does not compile
    A(L"n", L"n"); // compiles
    return 0;
}

错误on Ideone模板参数扣除/替换失败

1 个答案:

答案 0 :(得分:6)

您的dt.dayofweek无法扣除(由于T),一种方法是:

::type