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

--- Comment #20 from user202729 <user202729 at protonmail dot com> ---
(In reply to Jonathan Wakely from comment #19)
> That's easily solved by accessing the new object through the pointer
> returned by the new expression:
> 
> std::vector<Base> v(1);
> Base* p = v.data();
> p->~Base();
> p = ::new((void*)p) Derived();
> p->f();
> p->~Base();
> ::new((void*)p) Base();
> 
> By the time anything is accessed through the vector's internal pointers, an
> object of the original type has been restored at that location, which meets
> all the requirements of [basic.life] p7.


Trying codes related to it, do you think the following is valid C++?

void k(){
    Base b;
    b.~Base();
    Base* p=new (&b) Derived;
    p->~Base(); // this cannot be devirtualized to Base::~Base()
    new (p) Base;
}

Currently gcc devirtualize the commented function to Base::~Base() anyway.
https://godbolt.org/z/bsnEdxMeK

Reply via email to