BullseyeCoverage Up Contents Search

Microsoft C++ Error C2131

SYMPTOMS

The error below occurs with BullseyeCoverage enabled, but does not occur without BullseyeCoverage.

error C2131: expression did not evaluate to a constant

CAUSE

The compiler normally detects that the indicated expression is compile-time constant. With instrumentation, the compiler cannot make this determination.

RESOLUTION

Either use the constexpr keyword to explicitly declare the expression as a compile-time constant, or exclude the code from instrumentation.

For example, instrumenting the code below results in error C2131.

template<unsigned n>
void f()
{
    const unsigned length = n < 10 ? 20 : 30;
    //constexpr unsigned length = n < 10 ? 20 : 30; // uncomment to resolve
    int array[length];                              // C2131 with BullseyeCoverage enabled
}
int main()
{
    f<1>();
}

MORE INFORMATION

Microsoft introduced support for constexpr with Visual Studio 2015.

Updated: 24 Apr 2017