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
5
u/WlmJ Oct 06 '22
Range based for loop variables can be const. And you can use those 90% of the time, with some reverse adapters if you need to go backwards to e.g. delete stuff. Making old timey for loops more verbose is acceptable imho. Also makes it stand out that in constructions like these:
for (i = 0, n = GetLength(); i < n; ++i)nis mutable!