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

            Bug ID: 63179
           Summary: Does not work virtuality
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: isak50 at mail dot ru

// g++ 1.cpp -O2 -Wall -std=c++11 -Wall
#include <stdio.h>
struct Base{
    virtual void f() {printf("base\n");}
};
struct Child : Base{
    void f()override {printf("child\n");}
};
alignas(Child)
char buf[sizeof(Child)];
union Ptr{
    decltype(::buf) buf;
    Base base;
};
int main(){
    new(buf) Child;
    Ptr *ptr = reinterpret_cast<Ptr*>(buf);
    //-------------------printf: base---//
    ptr->base.f();                      //
    Base &b = ptr->base;                //
    b.f();                              // clang print "child".
    //----------------------------------//
    //-------------------printf: child--//
    Base *b2 = &ptr->base;              //
    b2->f();                            //
    //----------------------------------//
}

Reply via email to