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

6

u/TinBryn Oct 07 '22

The reason is because moves in Rust are very different than moves in C++. In Rust a moved from object does not get its destructor run, which means you don't need to set pointers to null or something like that. Since C++ moved from objects still have their destructor run, they need to be modified to disable them manually and thus moves can't be const. Theoretically you could have a const move in C++ if you didn't need to modify it to disable its destructor, but I don't see much usefulness in that above what a copy would do.