const in dmd v2.011

Steven Schveighoffer schveiguy at yahoo.com
Wed Feb 20 19:46:11 PST 2008


"Steven Schveighoffer" wrote
>> The forms 'const(char)[]' and 'const(char)*' ignore transitivity while
>> other forms enforce it. It could be confusing, no?
>
> This is not so, they do not ignore transitivity.  Transitivity means that 
> if you have a pointer to const data, anything that data points to must 
> also be const.  If the array was const but the data pointed to could be 
> changed through the array pointer, then transitivity would be broken.

I didn't say this right.  What I meant was

Transitivity means that if you have a pointer who defines itself as pointing 
to const data, any data that you access through that pointer, whether it be 
the data the pointer points to, or data pointed to by the const data, must 
be const.

For example:

int z = 1;
int * y = &z;
const(int*)* x = &y;

writefln(**x); // outputs 1, this is ok because i'm just reading the data, 
not modifying
*y = 2;
writefln(**x); // outputs 2, still ok, because I'm not modifying
**x = 3; // not ok, even though y isn't const, I'm trying to modify what y 
points to *through* x.

I hope this helps.

-Steve 




More information about the Digitalmars-d-learn mailing list