safer casts - take II

Janice Caron caron800 at googlemail.com
Tue May 13 12:29:34 PDT 2008


On 13/05/2008, terranium <spam at here.lot> wrote:
>  1) throw InvalidCastException on invalid cast

We already have that.

    import std.conv;

    dst = to!(T)(src);

I quote:

<quote>
The to family of functions converts a value from type Source to type
Target. The source type is deduced and the target type must be
specified, for example the expression to!(int)(42.0) converts the
number 42 from double to int. The conversion is "safe", i.e., it
checks for overflow; to!(int)(4.2e10) would throw the
ConvOverflowError exception. Overflow checks are only inserted when
necessary, e.g., to!(double)(42) does not do any checking because any
int fits in a double.
</quote>

If std.conv.to doesn't do dynamic casting, it can easily be made to,
so your base is already covered.



More information about the Digitalmars-d mailing list