我有一些C++11代码无法在Visual 2015上编译(更新2),但是编译时Clang和GCC都没有错误。因此,我怀疑Visual中存在编译器错误,但可能我的代码格式不正确。
我真正的类BaseUnit是double值上的模板包装类,它关注数量的物理维度(以SI单位m、kg、s、K表示)。例如,速度与时间模板实例的乘法自动给出一个距离实例。这个问题发生在当前用标量实现乘法的过程中。我已尽量简化了这门课,以说明问题。
#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上编译时,无论使用什么命令行选项,都会出现一个内部错误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*定义才能出现错误。如果移除前缀或后缀版本,则示例代码可以很好地编译。
如果这个行为被确认为一个bug,而不是一个众所周知的编译器问题,我可能会在Microsoft上填写一个bug报告。
发布于 2016-06-30 20:14:14
根据现行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;
}https://stackoverflow.com/questions/38131189
复制相似问题