https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94951

            Bug ID: 94951
           Summary: dereferencing type-punned pointer will break
                    strict-aliasing rules when using super class for a
                    template type
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jorgen.lind at gmail dot com
  Target Milestone: ---

The following program issues the warning when compiled with -O2 or -O3.

The workaround is to static_cast usage of members to super class.
In the godbolt example its sufficient to add brackets around the class instance
before member access, but was not sufficient in a more complex example. 

The behavior is present from 7.1 until trunk.
Godbolt link:https://godbolt.org/z/Kfp-ac

#include <stdint.h>

struct A
{
    uint32_t someData[32];
};

template<int N>
struct B : public A
{
    static B<N> createB(uint32_t (&otherData)[32])
    {
        B<N> toReturn;
        toReturn.someData[2] = 4;
        return toReturn;
    }
};

Reply via email to