Back to questions
JuniorTheoryCommon
Which line of this program does not compile?
int main() {
int count = 3;
const int total = count + 1;
constexpr int kLimit = 2 * 5;
int copy = total;
copy = kLimit;
total = kLimit;
return total + copy;
}total = kLimit;: a const variable cannot get a new value
- Expecting
constto need a constant: that rule isconstexpr's; aconsttakes any value once,count + 1included, and forbids only later changes. - Expecting
copyto inheritconst:constbelongs to a variable, not to its value, so a plainintcopy oftotalcan change.
- Why can
kLimitbe declaredconstexpr, whiletotalcannot? - When should a variable be
constrather thanconstexpr?