The problem with const (and a solution)

Sönke Ludwig ludwig at informatik_dot_uni-luebeck.de
Fri Dec 7 12:07:03 PST 2007


Steven Schveighoffer schrieb:
> "Sönke Ludwig" wrote
>> Steven Schveighoffer schrieb:
>>>> // struct references
>>>> S a = S(1);
>>>> S& b = a;
>>> This would not compile, a is a value type, b is a pointer type, you need:
>>> S&b = &a;
>> Hm, ok, so S& is the same as S* then?
> 
> Yes
> 
>> The class example should just show that it might be unexpected for a C++ 
>> programmer that the assert fails. But then again, D's classes already 
>> behave differently anyway.
> 
> Yeah, I was intending the X& meaning to be 'pointer to value', not for it to 
> mean 'C++ reference'.  This works for D because d's pointer to values 
> already act like C++ references when using the object, but act like pointers 
> when assigning.
> 
> For example:
> 
> S s1;
> S* sp = new S;
> sp = &s1; // acts like a pointer
> s.x = 5; // acts like a reference, sets s1.x
> 
> for classes, there is an implied pointer, which is what makes this 
> tail-const problem so hard...
> 
> -Steve 
> 
> 

Ok, see what you mean then. But apart from that, I actually understand the type 
system and this very problem perfectly well :)



More information about the Digitalmars-d mailing list