Casting away immutability

lobo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 1 21:19:23 PDT 2015


On Wednesday, 2 September 2015 at 04:04:54 UTC, Sergei Degtiarev 
wrote:
> On Wednesday, 2 September 2015 at 02:50:30 UTC, Jonathan M 
> Davis wrote:
>> is undefined behavior. So, don't do it.
> I don't. Actually, I'm looking for opposite - to protect data, 
> like it is a class with two methods, returning void[] and 
> immutable(void)[] as memory buffer, which user of the class 
> supposed to cast to desired type. I naively assumed that void[] 
> to be cast to, say, int[]; and immutable(void)[] to 
> immutable(int)[] respectively. Unfortunately, D cast notices no 
> difference between these two types.
> I seems a little bit too easy to to shoot yourself in the foot.

No, I think your design is unsafe because you're throwing away 
type information and returning void[], then telling the compiler 
not to worry about it.

>
>
> And still,
>>   assert(&a == b);
>>   assert(a != *b);
>> 
> How these two lines would look in assembler? Defined behavior 
> or undefined behavior, I can't imagine that.

why don't you have a look?

bye,
lobo




More information about the Digitalmars-d-learn mailing list