mapbox / variant

C++11/C++14 Variant
BSD 3-Clause "New" or "Revised" License
371 stars 101 forks source link

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

Closed jplatte closed 7 years ago

jplatte commented 7 years ago

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
kkaefer commented 7 years ago

Duplicate of https://github.com/mapbox/variant/issues/148

jplatte commented 7 years ago

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