Safer casts

Simen Kjaeraas simen.kjaras at gmail.com
Mon May 12 11:05:12 PDT 2008


Steven Schveighoffer <schveiguy at yahoo.com> wrote:

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

Actually, I had no other intent than to see if I could break the system.
For a better (or worse, mayhaps) version: const(C*)* (or whatever, really)
to const(invariant(C)*)*. For this, I'd prefer Janice's suggestion of
const cast, as I understand it:

   const(C*)* foo;

   cast(const(invariant(C)*)*)foo; // error, cast can't change constancy

   const cast(const(invariant(C)*)*)foo; // works like a charm

The const cast looks good to me, whereas what I used in the last example
was downright ugly. Casting to a different constancy should be one cast,
not first making it all mutable, and then put const back in there.

-- Simen



More information about the Digitalmars-d mailing list