Delete expression

Link here

This destructor also implicitly call the destructor of the auto_ptr object. And that will remove the pointer that it holds, which points to a C object - not knowing the definition of C! This appeared in the .cpp file, where a struct A constructor is defined.

It was curious and then

5.3.5 / 5 state - "If the deleted object has an incomplete class type with a delete point and the full class has a nontrivial destructor or deallocation function, the behavior is undefined."

My question is: why isn’t such a program trying to remove a pointer to an incomplete type, which is considered to be poorly formed? Why is it being pushed into the realm of conditionals ( and , does the full class have a nontrivial destructor ..) 'undefined behavior'?

What does the < and ?

EDIT 2:

Is the code correctly generated? VS and Gcc / CLang compile, but Como gives a warning. I think all of this is part of the undefined behavior mentioned in the standard. My question is: "Why is it not badly formed, but there is undefined"?

#include <iostream>
#include <memory>
using namespace std;

struct C;
                        // Is this the POI for auto_ptr<C>? $14.6.4.1/3
struct A{
    A();
    auto_ptr<C> mc;
    ~A(){}             // how does it link to C::~C at this point?
};

struct C{};

A::A():mc(new C){}

int main(){
    A a;
}
+3
source share
1 answer

As I write this, your text says "Link [here] [1]" without a link.

But, in fact, the standard allows a deletepointer to an incomplete type so that you can use the knowledge that the compiler does not have, namely, that the type destructor does nothing.

std::auto_ptr - , , PIMPL ( Herb Sutter GOTW PIMPL, std::auto_ptr). boost::shared_ptr - , (). , boost::shared_ptr , .

hth.,

+5

Source: https://habr.com/ru/post/1770045/


All Articles