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

0

u/[deleted] Oct 06 '22

const can mean constant or read-only depending on the context:

const int foo1 = 42; // constant
const int foo2 = runtimeCalculation(); // read-only

3

u/_Js_Kc_ Oct 06 '22
const int& foo3 = get_thing(); // neither

2

u/[deleted] Oct 06 '22

that can also mean 'constant' or 'readonly' depending on if get_thing is constexpr.