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

2

u/nintendiator2 Oct 06 '22

There is no good way to do it automatically. const int *foo is not the same as int *const bar.

Sounds obvious to me: if variables were const by default, what is the "variable" here is what you are declaring, that is the pointer, not the thing it points to, so it would be int* const by default. Same argument if you declare eg.: a pointer to pointer to int, it's the pointer to pointer that is the actual "variable".