【问题标题】:Visual Studio 2013 - std::enable_if warning 4544Visual Studio 2013 - std::enable_if 警告 4544
【发布时间】:2015-07-28 13:49:05
【问题描述】:

我已经写了这段代码

在标题中的类中

template <typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
T GetResultValueAsNumber(char * result);

比内联文件中

 template <
        typename T,
        typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
    >
    T PostgreSQLWrapper::GetResultValueAsNumber(char * result)
    {    
        double value = strtod(result, NULL);

        return static_cast<T>(value);
    };

在 Visual Studio 2013 中,我收到了这个警告(但代码可以正常工作)

warning C4544: '<unnamed-symbol>' : default template argument ignored on this template declaration - see declaration of '<unnamed-symbol>'

什么意思?

【问题讨论】:

    标签: c++ visual-studio c++11


    【解决方案1】:

    VC++ 在这里不符合要求;该程序格式不正确。 GCC 和 Clang refuse to compile similar code

    §14.1/10:

    获得了可供使用的默认模板参数的集合 通过合并所有先前声明的默认参数 模板默认函数参数的方式与 (8.3.6) 相同

    §8.3.6/4:

    默认参数不应被以后的声明重新定义(不是 甚至到相同的值)。

    /6 也显示了一个示例:

    除了类模板的成员函数,默认参数 在出现在类之外的成员函数定义中 定义被添加到由 类定义中的成员函数声明; […] [例子

    class C {
        void f(int i = 3);
    };
    
    void C::f(int i = 3) {   // error: default argument already
    }                        // specified in class scope
    
    // […]
    

    结束示例 ]

    【讨论】:

      【解决方案2】:

      一个默认值只能给出一次,所以如果它在头文件中,它就不能用函数的定义重新声明。只需删除(或注释掉)默认值即可。

      【讨论】:

      • 能否请您发布样本。我完全不明白,你的意思。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多