Topic: reinterpret cast<char *>( &pod ) -- does this always work?


Author: kuyper@wizard.net (James Kuyper)
Date: Thu, 3 Jun 2004 21:49:55 +0000 (UTC)
Raw View
falk.tannhauser@crf.canon.fr (Falk Tannh   user) wrote in message news:<40BF1B13.1848166F@crf.canon.fr>...
..
> Apart from breaking C compatibility for pointers to POD types (5.4/5
> says that, barring const cast, C-style cast is equivalent to reinterpret
> cast,
> unless static cast is possible),

The C standard does not specify where a pointer points when it is
converted to a pointer to an unrelated non-character type. The only
thing it says is that the new pointer, if converted back to the
original type, must compare equal to the original pointer value.

The C standard does specify, for instance, that converting a pointer
to a structure type into a pointer to the type of it's first member
produces a pointer to that member of the structure pointed at by the
original pointer. Similar guarantees apply to unions and arrays.
That's what I meant by "related types". However, for unrelated types
there's no guarantees.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]