C2057:应为常量表达式
C2057: expected constant expression
我无法在VS2013上编译它,但它确实在gcc 4.7.2上编译。我知道VC++在功能方面落后于GCC和CLang,但它到底是什么功能?如果不是,这就是一个bug,不是吗?
template <int N>
struct factorial
{
static const long value;
static const long previous = factorial<N - 1>::value; //C2057: expected constant expression
};
template <int N>
const long factorial<N>::value = N*factorial<N - 1>::value;
template <>
struct factorial<0>
{
static const long value = 1;
};
请不要告诉我如何解决这个问题,因为这不是生产代码,我也不需要。例如,我可以在类中定义值,问题就解决了,但假设我必须在类外定义它(VC2013仍然不支持constexpr)
MSVC没有正确的两阶段模板编译。GCC正确地将factorial<N - 1>::value
视为依赖名称,并在第二阶段进行解析,但MSVC必须在第一阶段进行解析。
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 使用自动推导的 lambda 参数作为常量表达式
- 生成提升::hana::set 的常量表达式问题
- 为什么不能用常量表达式声明数组?
- 不是 lambda 函数中的常量表达式
- 函数调用在常量表达式中必须具有常量值
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题
- 生成 constexpr 字符串表,不能产生常量表达式
- 整体模板参数。错误:在常量表达式中使用'this'
- 如何在满足常量表达式的同时将整数传递给指针,传递给 std::array<double、integer>?
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:'new'不能出现在常量表达式中
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- C++ 使用变量而不是常量表达式初始化数组
- 使用函数参数作为常量表达式的一部分 - gcc vs clang
- 片段着色器中的"错误:在 GLSL 1.30 及更高版本中禁止使用非常量表达式索引的采样器数组"