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

            Bug ID: 95975
           Summary: -Wstrict-aliasing=1 false negative for std::pair
                    member at -O2
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: v-thbutl at microsoft dot com
  Target Milestone: ---

The code example below will result in a warning of type-punning for a cast from
a void* std::pair member reference to uintptr_t&. This only occurs at
optimization levels greater than -O1. This warning is not reported if x.first
is replaced with std::get<0>(x).

code example:

#include <stdint.h>
#include <utility>

uintptr_t& foo(std::pair<void*, void*>& x)
{
  return (uintptr_t&)x.first;
}

compiler output:

$ g++ -O2 -Wstrict-aliasing=1 main.cc
main.cc: In function ‘uintptr_t& foo(std::pair<void*, void*>&)’:
main.cc:6:24: warning: dereferencing type-punned pointer might break
strict-aliasing rules [-Wstrict-aliasing]
    6 |   return (uintptr_t&)x.first;
      |                      ~~^~~~~

Reply via email to