On 8/28/2011 4:52 PM, Andrei Alexandrescu wrote:
It is quite easy to define C++-like cast operators as D functions. If
there's enough demand for that, we may include such in the standard
library.
Andrei
I think we should define at least the following and probably put them in
std.conv, since these would add real value over the cast operator, the
first in terms of straightforwardness and readability and the second in
terms of error checking:
reinterpretCast: Reinterprets the bit pattern at a given address as
some other type. This should be broadened from C++ to allow
reinterpretCasting from any type to any type. For example:
// Fancy way of making a floating point number negative.
float f = 5;
auto i = reinterpretCast!int(f);
// Flip the sign bit.
i &= ~(1 << 31);
f = reinterpretCast!float(i);
dynamicCast: Cast a class to another. This should throw something
derived from Error (not Exception) on failure instead of just returning
null. If you want null, just use the cast operator. Maybe we should
change its name, though. How about checkedCast or downCast?