D2.0: an example of use-case for casting invariant away

Jascha Wetzel firstname at mainia.de
Thu Jun 21 03:10:15 PDT 2007


Don Clugston wrote:
> It sounds that in D, it will be too easy to cast away constness 
> accidentally.
> With C++, at least you can grep for const_cast and detect potentially 
> dangerous code, and you get a strong visual clue.
> Suppose I've written a D function like this:
> 
> void f(int *b, uint c)
> {
>   // maybe I'm avoiding a compiler warning or something.
>   uint *d = cast(uint *)b;
>   d += c;
> }
> 
> Months later, I'm refactoring the code, and I convert the int * 
> parameter to an invariant, without recognising that it's changing the 
> value of b. Oops.

a syntactical search would be needed instead of a grep. until 
refactoring tools for D surface, an optional compiler warning for casts 
resulting in undefined behaviour would be nice...



More information about the Digitalmars-d mailing list