Gurus everywhere tell us to const everything unless we need to modify it,
This makes sense to me. Immutability is a very noble idea.
Rust is a new language (developed by Mozilla) in which variables, by default, are immutable. If you want your variables to be mutable, then you've to make it mutable explicitly by using the keyword mut
which is exactly opposite of what C++ does — in C++, you've to make things immutable explicitly by using the keyword const
, otherwise they're mutable by default.
Especially with the advent of multicore processors and more multi-threading software than ever before, it makes more sense to me. I think by default there should be more restriction and the restriction should be lifted (when you need to) consciously and explicitly (as opposed to implicitly). For example, members of a class are private
by default; the inheritance is private
by default. You make them public
or protected
explicitly. So should be the case with the-right-to-modify a variable. You should not have right to modify a variable, by default (in my opinion). It requires a bit of maturity to appreciate immutability/restriction — by putting restriction, you avoid a whole class of bugs in your software. That is my thought, in support of the statement.
Now coming back to the actual question,
Why aren't C++ types const by default when, according to the const-people they should be made const by default?
It is because
- Not many programmers realized that immutability (as the default) makes their life easy, as it is a sort of new trend, at least it is catching up very recently.
- backward-compatibility. C has mutable types by default.