简体   繁体   中英

Why aren't C++ types const by default?

Gurus everywhere tell us to const everything unless we need to modify it, yet the standard makes everything mutable until we declare it const .

Am I missing something here, or is this a contradiction? Why aren't C++ types const by default when, according to the experts (some of whom presumably designed the standard), they should be made const by default?

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.

Gurus everywhere tell us to const everything unless we need to modify it,

That's been conventional wisdom for a decade or two, yes.

yet the standard makes everything mutable until we declare it const.

The standard has evolved from languages developed around fifty years ago, when const-correctness, and even type checking, were largely regarded as being only of academic interest.

Am I missing something here, or is this a contradiction?

It's not a contradiction; just a language that doesn't work how some would say it should. You still can (and should) declare things const where appropriate, and we just have to put up with a language that doesn't push us towards safe practices.

Why aren't C++ types const by default?

Because changing such a fundamental aspect of the language will break just about all existing code. That's a much bigger concern than slightly reducing the scope for mistakes.

I had the same question a few years ago, and I wrote some helper functions to make the use of const variable more terse.

You can see it here : https://github.com/pthom/cpp_terse_const

And my post on codereview at stackechange : https://codereview.stackexchange.com/questions/106074/const-by-default

Beware, this was just an exercice, I do not use this in production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM