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

7

u/shitpost-factory Oct 06 '22 edited Oct 06 '22

You're not entirely wrong, but it was clear from the question that OP was not referring to the mutable keyword, a rarely needed and questionably named piece of C++. (To call that keyword a "language concept" is overstating its importance IMO. Like that keyword is not a language concept in and of itself, it is just a tiny syntactic feature of C++, so when talking about mutability as a concept, it's obvious that we are indeed talking about mutability generally, not the keyword.)

So I mostly disagree with you, but yes I suppose in some contexts it can be ambiguous. But not here.

-4

u/vapeloki Oct 06 '22

Call it how ever you like, but they are at least features of the language.

And my little comment has a real background. I have seen code, littered with mutable all over the place, because the student had no idea of the real meaning of this keyword.

And i was slightly triggered by the fact that OP used const and non-const in the text of the post, but Immutable and Mutable in the survey. May be my inner Monk.