Unqual fails with pointer?

bearophile bearophileHUGS at lycos.com
Thu Jun 13 15:25:15 PDT 2013


Namespace:

> Just out of interest: how safe is something like this:
>
> ----
> UnqualPtr!From unqual_cast(From : U*, U)(From from) {
> 	union Cast {
> 		From from;
> 		UnqualPtr!From to;
> 	}
> 	
> 	Cast c;
> 	c.from = from;
> 	
> 	return c.to;
> }
>
> const B* cb = ...;
> B* b = unqual_cast(cb);
> ----
>
> compared to this:
>
> ----
> const B* cb = ...;
> B* b = cast(B*) cb;
> ----

Both are quite unsafe. As you know modifying const data in D 
causes bugs. The second is more bug-prone because you have to 
state the type again, so it's not DRY.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list