Safer casts

Steven Schveighoffer schveiguy at yahoo.com
Mon May 12 07:11:28 PDT 2008


"Simen Kjaeraas" wrote
> On Sat, 10 May 2008 07:59:23 +0200, Janice Caron wrote:
>> 1) convert or downcast: cast(Foo)bar
>> 2) reinterpret: cast!(Foo)bar
>> 3) constancy: cast(invariant)bar, cast(!const)bar
>
> For 3), what if I want to cast const(int*) to const(int)*? 
> cast(const(int)*)cast(!const)foo?
>
> -- Simen

I'm not really joining this discussion (yet), but I just wanted to respond 
to this.

If the purpose of your cast is to assign to a const(int) * variable, you 
need no casts to do this.  const(int*) implicitly casts to const(int)*:

int m = 5;
const(int *) ptr = &m;
const(int)* ptr2 = ptr; // OK

Why?  Because I can't change anything through ptr2 that I couldn't change 
through ptr.

Now, if you casting to be able to rebind ptr, then I think you would need 
some crazy casting like you stated.

-Steve 





More information about the Digitalmars-d mailing list