编译器错误 C2131

表达式的计算结果不是常数

声明为 constconstexpr 的表达式在编译时计算结果不是常数。 编译器必须能够在表达式被使用时确定表达式的值。

示例

此示例演示了导致错误 C2131 的方法以及如何修复此错误。

// c2131.cpp
// compile by using: cl /EHsc /W4 /c c2131.cpp

struct test
{
    static const int array_size; // To fix, init array_size here.
    int size_array[array_size];  // C2131
};

const int test::array_size = 42;
c2131.cpp
c2131.cpp(7): error C2131: expression did not evaluate to a constant
c2131.cpp(7): note: failure was caused by non-constant arguments or reference to a non-constant symbol
c2131.cpp(7): note: see usage of 'array_size'

另请参阅

const
constexpr