Strict pointer aliasing: any solution for a specific problem?
- by doublep
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.)