1

I'm trying to compile the following snippet with VS2017 but it is throwing an error C2131: expression did not evaluate to a constant without any further detail in the output log.

class Example { public: constexpr Example() : m_int() { for (int i = 0; i < 256; ++i) { for (int j = 0; j < 256; ++j) { m_int[i] = i; } } } private: int m_int[256]; }; int main() { constexpr Example vv; // <-- error C2131 here return 0; } 

However for some reason unknown to me, if I stop the j counter at < 255 (instead of 256), the code compile fine. What's the explanation behind this and how can I fix it?

4
  • Don't see anything obviously wrong, so this looks like a compiler bug. gcc compiles the above without any issues. Commented Dec 7, 2019 at 4:48
  • Compiles fine with MSVC Commented Dec 7, 2019 at 4:59
  • I have just found the solution, I'll post the answer right away, thanks! Commented Dec 7, 2019 at 5:02
  • What does the inner loop even do? It’s just assigning to the same element over and over again. Commented Dec 7, 2019 at 5:28

1 Answer 1

1

Further searching landed me on the following link and after some trial and error with the parameters values I found the solution.

Adding /constexpr:steps10000000 to the project additional options in C++ compiler settings has fixed the error and now it compiles fine. I wish VS2017 would throw the error C4593 in this case instead of C2131 as it would have saved me a headache!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.