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

            Bug ID: 81152
           Summary: False strict-aliasing warning
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rrrlasse at hotmail dot com
  Target Milestone: ---

Version 8.0.0 20170620 (and also version 7.1) gives following false warning:


me@ubuntu:~$ g++ -Wall -Wextra -O3 -std=c++14 -fstrict-aliasing
-Wstrict-aliasing /mnt/hgfs/D/c.cpp
/mnt/hgfs/D/c.cpp: In constructor ‘BasicArray<T>::BasicArray()’:
/mnt/hgfs/D/c.cpp:18:40: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
  char* p = reinterpret_cast<char*>(tmp.m_data);
                                        ^~~~~~

Below is a minimal code sample. I can't reduce it further - you need both the
inheritance and also the templates for it to trigger:


struct Array
{
        char* m_data = nullptr;
};

template <class T> struct BasicArray : public Array
{
        BasicArray();
};

template <class T> BasicArray<T>::BasicArray()
{
        BasicArray<T> tmp;

        // aliasing warning
        char* p = reinterpret_cast<char*>(tmp.m_data);

        // avoid unused variable warning
        static_cast<void>(p);
}

int main()
{
        BasicArray<char> f;
}

Reply via email to