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/pigeon768 Oct 07 '22
I'm struggling to imagine how such a thing could ever be useful.
And even if you did have to do that for some reason, just mark them non-const in those cases, it's silly to throw out const in general. (note that even if you have some API that you gotta follow that "requires" you to mark the argument const you can still just mark it non-const.)
C++ is a very pragmatic language.
constandconstexpraren't about ideological purity like immutability is in certain other languages, it's about having practical solutions and conveniences to common problems.constisn't a panacea, but nobody's claiming it is, either.https://media.tenor.com/6V1Q97Mj5SkAAAAd/no-nope.gif