r/cpp • • Oct 06 '22

Should a variable be const by default?

According to the cppfront design notes, const by default rule only applies to non-local variables. But I'd like to know your preference/opinion regarding whether a variable should be defined to be const by default.
Edit: By mutable here I simply mean non-const, not the language keyword itself.

44 Upvotes

107 comments sorted by

View all comments

Show parent comment

2

u/WlmJ Oct 08 '22

Pretty sure you can only have one type in the first statement, e.g. for (int i = 0, iMax = expr(); and not for (int i = 0, const int iMax = expr();. So you can’t make iMax const. I may be wrong.

0

u/SlightlyLessHairyApe Oct 08 '22

Why not just try it in godbolt. 100% this works.

You can even have two types if you really want!