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

--- Comment #6 from Ng YongXiang <yongxiangng at gmail dot com> ---
That is interesting. Thanks for the reply.

However, I'd argue that the 2 bugs mentioned are different from what I am
proposing. The 2 bugs linked access virtual functions via ptr (delete p;
val->f();) and are invoked by the user directly, and so I think it makes sense
for the compiler to access the vtable. 

However, the array issue I'm putting forth is caused by the destruction of the
array with automatic storage duration, and the destruction code is generated by
the compiler not by the user via some kind of ptr or reference.

Moreover, clang does it right and devirtualizes the destruction of the array
while gcc still doesn't.

https://godbolt.org/z/f33Gh5EGM

#include <iostream>
#include <memory>

struct A
{
    A()
    {
        std::cout << "A()" << std::endl;
    }
    virtual ~A() 
    {
        std::cout << "~A()" << std::endl;
    }
};

struct B : public  A
{
    B()
    {
        std::cout << "B()" << std::endl;
    }
    virtual ~B()
    {
        std::cout << "~B()" << std::endl;
    }
};

int main()
{
    A a[3];
    a[0].~A();
    :: new(std::addressof(a[0])) B();
}

Reply via email to