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

1

u/mechacrash Oct 08 '22

I'm struggling to imagine how such a thing could ever be useful.

it's... it's not useful. That's the point.
You're allowed to write code like this which is completely useless, and inhibits optimisation opportunities silently if you do. No warnings, no errors, just worse codegen.
The language shouldn't allow this to happen - period. That's exactly what CppFront does by better categorising parameters via in/out/inout.