On 7 June 2011 15:39, Richard Guenther <richard.guent...@gmail.com> wrote: > On Tue, Jun 7, 2011 at 4:31 PM, Jonathan Wakely <jwakely....@gmail.com> wrote: >> On 7 June 2011 15:20, Richard Guenther wrote: >>>> >>>> However, for my construct, which appears to be completely legal, I get a >>>> warning, which I'd like to disable. How can I do that? Currently I'm >>>> using >>>> -Wno-strict-aliasing, but I'd like to have a better solution. I tried to >>>> cast (void*) before the cast to (OBJECT*), it didn't help. Is it possible >>>> to disable this warning for this line only (maybe with some GCC specific >>>> tricks)? >>> >>> Try >>> >>> void *temp = (void *)data; >>> reinterpret_cast<int *>(temp) >> >> Should that be static_cast not reinterpret_cast? >> >> A reinterpret_cast from void* is technically undefined in C++03, see >> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1120 and >> http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/reinterpret-cast-and-pointer-to-void.html >> >> Although GCC will do the right thing, if a static_cast will suffice >> then it should generally be preferred to reinterpret_cast. > > I thought static_cast might do pointer adjustments while reinterpret_cast > will never do that but is value-preserving.
static_cast won't do adjustments for casts to/from void, only for pointers to object types. In C++0x the reinterpret_cast<OBJECT*>(temp) above is defined to be the same as: static_cast<OBJECT*>(static_cast<void*>(temp)) and obviously the first static_cast does nothing because it's already void*, so only the second is needed. Feel free to ignore me - the fact reinterpret_cast doesn't work with void is a defect in the standard that was only discovered quite recently and has been corrected for C++0x, so I'm mostly being pedantic and you can forget I said anything :)