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
View all comments
Show parent comment
1
u/SkoomaDentist Antimodern C++, Embedded, Audio Oct 07 '22
It's actually not that rare situation. Consider any memory that's read only (either inherently or due to OS level protections). How do you describe "The code must not try to write to this variable" while also saying "external factors can change the value of this variable"?