Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.lang.c Subject: Re: typesafe downward casting Message-ID: <12068@dog.ee.lbl.gov> Date: 14 Apr 91 17:04:46 GMT References: <13100@ms.maus.de> Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Distribution: world,comp Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 22 X-Local-Date: Sun, 14 Apr 91 10:04:46 PDT In article <13100@ms.maus.de> Kai_Henningsen@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@ee.lbl.gov