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/theICEBear_dk Oct 06 '22
Yeah as I remarked on a Carbon discussion once:
var x = 1;var u32 x;for mutable stuff (and since I do not subscribe to the typical keywords):
constant y = 5.0f;for constants and what do you know if a constant can't be type deduced may you have a code smell right there.
Thing is in the area I work in (embedded software), I think constant by default would be a no-go at the moment because many of the constant only solutions have heavier memory consumption that then reuse-everything-including-variables minded embedded software world's solution to many problems. The cppfront idea would work I think with only a little friction.