Behaviour of unsigned cast

Henning Hasemann hhasemann at web.de
Tue Apr 10 05:06:02 PDT 2007


I've had several issues with this implicit cast unsigned and already
posted some here (the "is this inteded behaviour" is also a good example).

Now it has come to my mind that no matter if you like the implicity of such
a cast it is a bit strange that cast(uint)some_int in fact is a
*reinterpretation* of the same binary data whereas other casts (for example
cast(int)some_float) look at the meaning of the value and try
to find something close in the new type.

cast(uint) (and equivalents for other numeric types) is the only cast
I know of that does change meaning of the value in such a hard way
(all other casts that I'm aware of try to preserve meaning).

Im *not* saying it would be wise to have cast(uint) throw a runtime
error when it is called on a negative value (though there are situations
when this might be useful).
But maybe the compiler should warn whenever cast(uint) is used
(especially when this happens implicitly).

Henning




More information about the Digitalmars-d mailing list