typesafe downward casting

Chris Torek torek at elf.ee.lbl.gov
Mon Apr 15 03:04:46 AEST 1991


In article <13100 at ms.maus.de> Kai_Henningsen at ms.maus.de (Kai Henningsen) writes:
>... C uses Casts for two completely different purposes: one, to convert
>values (as in (double)1) ... and another, to make the compiler *interpret*
>the *same* value different (as in (int *)"blabla").

The latter is a conversion as well---for instance, when sizeof(int) ==
sizeof(long), `int x; (long)x' converts x by very carefully taking each
bit, turning it over twice, and putting it in the same place it came
from.  The result, of course, is just the original value again.

Yes, I am kidding about `turning it over twice'.

Seriously, even casts that do not cause any change in actual bit
pattern are still conversions, and the result of every cast is a value,
not an object.  Many compilers delete identity-conversions too early in
compilation, and wind up permitting assignments that should be
rejected, which makes people think casts are like unions.  Nonetheless,
every cast is a conversion, and every cast produces a new value, even
if the compiler figures out a way to do this using no machine code.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list