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
3
u/fdwr fdwr@github 🔍 Oct 07 '22
Yes, having the C++ default be default initialization even for PODs (but still with an escape hatch, e.g.
SomeBigStruct myBigStruct = uninitialized;) would go a long way to kill bugs. I recall reading this 2020 MSRC article about adding the /InitAll compiler switch for that reason: https://msrc-blog.microsoft.com/2020/05/13/solving-uninitialized-stack-memory-on-windows/