[Solved] Why not have all variables be a constexpr? [closed]


First of all not all variables can be made constexpr. For example

int x;
std::cin >> x;

Obviously x cannot be constexpr. The value it will have after it has been read from input is not a compile-time constant.

Second, constexpr is part of the contract of a variable or function. By marking it constexpr, you allow other code to use it in a context that requires a constant expression. That means even if it might be possible to declare it constexpr now, you should not do so if you are planning on potentially changing the code so that it will not be constexpr later. If you were to mark it constexpr now, other code might come to rely on this property, and that code will stop compiling when constexpr is removed in a future version of the program.

solved Why not have all variables be a constexpr? [closed]