Strict pointer aliasing: any solution for a specific problem?

Posted by doublep on Stack Overflow See other posts from Stack Overflow or by doublep
Published on 2010-06-05T15:23:43Z Indexed on 2010/06/05 15:52 UTC
Read the original article Hit count: 264

Filed under:
|

I have a problem caused by breaking strict pointer aliasing rule. I have a type T that comes from a template and some integral type Int of the same size (as with sizeof). My code essentially does the following:

T x = some_other_t;
if (*reinterpret_cast <Int*> (&x) == 0)
  ...

Because T is some arbitary (other than the size restriction) type that could have a constructor, I cannot make a union of T and Int. (This is allowed only in C++0x only and isn't even supported by GCC yet).

Is there any way I could rewrite the above pseudocode to preserve functionality and avoid breaking strict aliasing rule? Note that this is a template, I cannot control T or value of some_other_t; the assignment and subsequent comparison do happen inside the templated code.

(For the record, the above code started breaking on GCC 4.5 if T contains any bit fields.)

© Stack Overflow or respective owner

Related posts about c++

Related posts about strict-aliasing