I'm using std::aligned_storage
as the backing storage for a variant template. The problem is, once I enable -O2
on gcc I start getting warnings of 'dereferencing type-punned pointer will break strict aliasing`.
The real template is much more complex (type checked at runtime), but a minimal example to generate the warning is:
struct foo
{
std::aligned_storage<1024> data;
// ... set() uses placement new, stores type information etc ...
template <class T>
T& get()
{
return reinterpret_cast<T&>(data); // warning: breaks strict aliasing rules
}
};
I'm pretty sure boost::variant
is doing essentially the same thing as this, but I can't seem to find how they avoid this issue.
My questions are:
- If using
aligned_storage
in this way violates strict-aliasing, how should I be using it? - Is there actually a strict-aliasing problem in
get()
given that there are no other pointer based operations in the function?- What about if
get()
is inlined? - What about
get() = 4; get() = 3.2
? Could that sequence be reordered due toint
andfloat
being different types?
- What about if