Casts and conversions done right

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Mon Jul 20 03:26:26 PDT 2009


Michiel Helvensteijn wrote:
> Lars T. Kyllingstad wrote:
> 
>>    int i = to!int(pi);    // The to function already exists in std.conv
> 
> But what does the to function do, exactly? I'd prefer to use one of floor,
> round or ceil. Wouldn't you? I'm sure they are available as well.
> 

I could perhaps have found a better example than rounding conversions 
since there are so many ways to perform those. But that is beside the 
point, which was that safe type conversions should be clearly separated 
from unsafe ones.

Regarding your question, currently I think std.conv.to just checks for 
over-/underflow and performs a cast. :) If such casts were to be 
disallowed, it would of course need to be rewritten.

In any case, the documentation for both cast and std.conv.to should say 
which rounding mode is used.

-Lars



More information about the Digitalmars-d mailing list