18

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?

quant
  • 21,507
  • 32
  • 115
  • 211
  • 2
    I'd say this comes from at least historical reasons in compliance with compatibility to the [tag:c] language. – πάντα ῥεῖ Oct 20 '13 at 13:45
  • 22
    Meanwhile, in another language: "*Why is everything const by default when I need so much mutable state?*" – syam Oct 20 '13 at 13:45
  • It comes from good old times, when programming was done just to make things working. – Alex F Oct 20 '13 at 13:51
  • @syam What's your point? Language design is full of trade offs, but apparently the gurus agree that `const`-by-default is good for C++ (interacts well with the rest of the language, fits the applications it's used for, etc.). –  Oct 20 '13 at 13:53
  • You can read this [question](http://stackoverflow.com/questions/18157523/why-would-you-use-the-keyword-const-if-you-already-know-variable-should-be-const) – ST3 Oct 20 '13 at 13:59
  • 2
    @delnan My point is that you need much more mutable objects than const ones. And IMHO, const-by-default wouldn't play that well with the rest of the language (eg. concerning move semantics, const-by-default would mean inefficient-by-default). I do agree that constness is good and is an integral part of a well designed program, but I disagree on the fact that it should be the default, it would be too impractical. – syam Oct 20 '13 at 14:01
  • @delnan IMO the only place where const-by-default would perhaps be useful is for function parameters. But no more. And I'm not even sure of that. – syam Oct 20 '13 at 14:08
  • 1
    Is the question talking about self-proclaimed gurus? – 6502 Oct 20 '13 at 14:15
  • Well, C++ is not the language you are looking for. The gurus that advocate const everywhere also ought to be suggesting more suitable languages. Trying to adapt C++ to a const everywhere paradigm will end in tears. Now "const correctness" on the other hand is a worthwhile endeavor for C++; but I've seen large projects that are pretty much const-free, and they still work just fine. – Eljay Feb 16 '18 at 16:03

3 Answers3

18

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Are you suggesting things like `const int foo(const int)`? – 6502 Oct 20 '13 at 14:16
  • 2
    @6502: `const` on return type doesn't make sense, but in the parameter, it does. I'm talking about your specific example. – Nawaz Oct 20 '13 at 14:21
16

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.
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • How does this answer the question? –  Oct 20 '13 at 13:47
  • @delnan: Does it now? – Nawaz Oct 20 '13 at 13:50
  • I don't see any reason to include the first half, but yes, the second part is an answer (whether it's a good one is a different matter, I'm not sure yet). –  Oct 20 '13 at 13:51
  • @delnan: The first is in support of the statement (because I've not seen many people talking about immutability as default), because I've to first agree with the statement first. The second part depends on how I think of the first part. – Nawaz Oct 20 '13 at 13:53
1

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.

Pascal T.
  • 3,866
  • 4
  • 33
  • 36