Skip to content

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

@jplatte

Description

@jplatte

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions