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

            Bug ID: 89960
           Summary: Implicit derived to base conversion considered type
                    punning.
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cassio.neri at gmail dot com
  Target Milestone: ---

Consider:

struct base {
    int i;
    void f(){}
};

template <void(base::*F)()>
struct derived : base {
    void g1() {
        return (this->*F)();
    }
    void g2() {
        base* p = this;
        return (p->*F)();
    }
};

void h() {
    derived<&base::f> x;
    x.g1();
    x.g2();
}

Compiling with -O2 -Wstrict-aliasing gives a warning

warning: dereferencing type-punned pointer will break strict-aliasing rules
[-Wstrict-aliasing]
         return (this->*F)();
               ~~~~~~~~~~^~

It looks like the implicit conversion from derived to base is considered
type-punning. 

Remarks: The warning goes away if either:
1) -O2 is not used.
2) -Wstrict-aliasing is not used.
3) base has no non-static data members.
4) F is not a template parameter.
5) x.g1()) is not called. (In contrast, x.g2() compiles fine and this is a
workaround for the issue.)
6) if another compiler is used (other vendor's but also gcc 4.6.4 or earlier)

Reply via email to