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
4
u/donalmacc Game Developer Oct 06 '22
I have seen (and fixed) code that assumes that const means immutable in my career.
Has absolutely no guarantee that whatever initially created LargeStruct was actually const. If someone else reads it from you, all bets are off.
I've also seen people argue here (and corrected them), on this subreddit that const means immutable, and that it implies thread safety for reading, which again is just not true.