Auto keyword with const variable

bsd slackovsky at gmail.com
Wed Jul 24 17:37:08 PDT 2013


On Wednesday, 24 July 2013 at 11:26:32 UTC, Mike Parker wrote:
...
> This is the exact behavior I would expect. I think of auto as 
> "this variable is going to be the same type as that variable." 
> Since in is const int, then j also is going to be const int. If 
> you want to copy n into a nonconst variable, you have to cast 
> away the const.
>
> int j = cast( int )n;
> auto j = cast( int )n;

+1


This works fine for pods, no cast required

     int j = n; ++j;


...and this does what one would expect too

struct S {
     int* value;
}
void funky(const S s) {
     int* s1 = s.value;
     *s1 = 5;
}
/d535/f466.d(9): Error: cannot implicitly convert expression 
(s.value) of type const(int*) to int*

void funky(const S s) {
     S s1 = s;
     *s1.value = 5;
}
/d297/f947.d(9): Error: cannot implicitly convert expression (s) 
of type const(S) to S




More information about the Digitalmars-d-learn mailing list