Auto keyword with const variable

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 24 17:03:33 PDT 2013


On Wednesday, July 24, 2013 10:07:54 Alex H wrote:
> This code:
> 
> void test(const int n)
> {
> auto j = n;
> j++;
> }
> 
> Gives this error:
> cannot modify const expression j
> 
> 
> Is this considered a feature or a bug? I would assume most people
> wouldn't want new variables inheriting const.

The behavior is correct and desirable. It would be a big problem for generic 
code if the type of auto didn't match the right-hand side of the expression. 
In some cases, it might be okay if auto gave you the tail-const type, but even 
that could be problematic depending on the type.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list