mapbox/variant

GCC 7: dereferencing type-punned pointer will break strict-aliasing rules

Closed this issue · 2 comments

After upgrading to GCC 7, when compiling at -O2 I now get a warning about a strict aliasing violation:

mapbox/variant.hpp:701:16: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
         return *reinterpret_cast<T*>(&data);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

libstdc++ implements the storage type through a variadic union and I think that might be the only solution for variant storage that guarantees no undefined behaviour. But then again, I can't see how one would obtain two references to the variant storage of different types without their code being broken anyway (the highest risk would probably be some code inside this library being miscompiled because of strict aliasing optimizations). So maybe it's enough to disable this warning (which is what I am currently doing myself)?

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
[relevant code]
#pragma GCC diagnostic pop

Duplicate of #148

I don't know how I missed that one, sorry ^^°