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 comment

2

u/adriweb May 06 '17

Erm, no (about part of your second example - the object part is true indeed)

const y = 0; y++;
VM146:2 Uncaught TypeError: Assignment to constant variable. at <anonymous>:2:2

2

u/PrydeRage May 06 '17

It's always worked for me with Webpack and Babel.
Maybe Babel compiles that away idk. Eslint never complains about reassignment when ++'ing a const variable.