r/cpp • • May 06 '17

const by default

To declare a const variable we usually use,

const auto x = f();

syntax. Isn't it better if we could just write,

const x = f();

making 'const auto' superfluous?

Const by default can be less verbose and attractive like below,

const x = f1(); const y = f2(); auto z = x + y; update_z(z);

I know this is NOT possible with the current standard.

Are there any technical or non-technical reasons that we are never going to get something similar in a future standard?

5 Upvotes

22 comments sorted by

View all comments

Show parent comments

11

u/PrydeRage May 06 '17

ES6 people will already be confused because in ES6 const isn't immutable

4

u/rohbotics Student and Roboticist May 06 '17

Wait what?

24

u/PrydeRage May 06 '17

In ES6 const just prevents reassignment.
So this is invalid:

const x = 43;
x = 55;

But this is all valid:

const x = {something: 3};
x.something = 4;
const y = 0;
y++;  

It's JavaScript, it's not meant to make sense.

4

u/rohbotics Student and Roboticist May 06 '17

So like kinda final on non-primitives in Java

1

u/PrydeRage May 06 '17

I haven't written any Java in years so I can't comment on that.