使用std :: enable_if

时间:2016-06-30 18:55:39

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

我有一些C ++ 11代码无法在Visual Studio 2015(Update 2)上编译,但在Clang和GCC上编译时没有错误。因此,我怀疑Visual Studio中的编译器错误,但也许我的代码在某种程度上是不正确的。

我的真实班级BaseUnit是一个double值的模板包装类,关注数量的物理维度(表示为SI单位m,kg,s,K)。例如,Speed与Time模板实例的乘法自动给出Distance实例。当前使用标量实现乘法时会出现问题。我尽可能地简化了课程以显示问题。

#include <type_traits>

template<int M>
class BaseUnit
{
public:
    constexpr explicit BaseUnit(double aValue) : value(aValue) {}
    template<typename U, typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
        BaseUnit operator*(U scalar) const { return BaseUnit(value * scalar); }
    template<typename U, typename std::enable_if<std::is_arithmetic<U>::value, int>::type = 0>
        friend BaseUnit operator* (U scalar, BaseUnit v) { return BaseUnit(scalar*v.value); }
protected:
    double value;
};

int main()
{
    BaseUnit<1> a(100);
    a = 10 * a;  // <-- error C1001 here
    return 0;
}

在Visual Studio上进行编译时,无论命令行选项如何,都会出现内部错误C1001:

C:\temp>cl bug.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23918 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

bug.cpp
bug.cpp(19): fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'msc1.cpp', line 1433)
 To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
 Help menu, or open the Technical Support help file for more information
Internal Compiler Error in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe.  You will be prompted to send an error report to Microsoft later.
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe'
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information

从某些实验中,出现错误时需要operator*个定义。如果删除前缀或后缀版本,示例代码可以正常编译。

如果确认此行为是一个错误,我可能会填写一份关于Microsoft的错误报告,而且还不是编译器的一个众所周知的问题。

1 个答案:

答案 0 :(得分:4)

根据目前的C ++标准草案:

14.1 Template parameters [temp.param]
1 The syntax for template-parameters is:
template-parameter:
   type-parameter
   parameter-declaration
type-parameter:
  type-parameter-key ...opt identifieropt
  type-parameter-key identifieropt= type-id
  template < template-parameter-list > type-parameter-key ...opt identifieropt
  template < template-parameter-list > type-parameter-key identifieropt= id-expression
type-parameter-key:
   class
   typename

结果你有语法错误(你可以报告MS关于编译器没有检测到这样的错误)。因此,在您的情况下,正确的格式良好的代码是:

template<int M>
class BaseUnit
{
public:
  constexpr explicit BaseUnit(double aValue) : value(aValue) {}
  template<typename U, typename T = typename std::enable_if<std::is_arithmetic<U>::value, int>::type>
  BaseUnit<M> operator*(U scalar) const { return BaseUnit<M>(value * scalar); }
  template<typename U, typename T = typename std::enable_if<std::is_arithmetic<U>::value, int>::type>
  friend BaseUnit operator* (U scalar, BaseUnit v) { return BaseUnit(scalar*v.value); }
protected:
  double value;
};

int main()
{
  BaseUnit<1> a(100);
  a = 10 * a;  // ok
  a = "19" * a;  // error
  return 0;
}