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

            Bug ID: 126639
           Summary: Undiagnosed undefined behaviour in pointer-to-member
                    conversion
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

```cpp
struct A { char a; };
struct B { char b; };
struct AB : A, B {};

constexpr auto ap = &A::a;
constexpr auto abp = (char AB::*)ap;
constexpr auto bp = (char B::*)abp; // this is UB

static_assert(bp == nullptr);
```

This code compiles, because after pointer-to-member conversion
gcc only tracks the offset, the offset in bp becomes -1,
which coincides with nullptr representation in ptr-to-members.

https://eel.is/c++draft/conv.mem#2
> If class D does not contain the original member and is not a base class of 
> the class containing the original member, the behavior is undefined

clang diagnoses this

Godbolt: https://godbolt.org/z/64hfcjKq6
  • [Bug c++/126639] New: Undia... ivan.lazaric.gcc at gmail dot com via Gcc-bugs

Reply via email to