lundi 26 juin 2017

unique_ptr and callable object to free it's object

Here I have simple program:

struct A{
    int t=0;

    operator() (int *p) {cout << "operator() (int *p)\n";delete p;};

    ~A() {cout << "~A\n";};
};

int main(int argc, char** argv) {
    {
        unique_ptr<int, A> u(new int[9]{0});
    }
    return 0;
}

The program creates an unique_ptr on array of int. And use callable object A to destroy pointer. All is simple. But when I run the program the output is:

~A
operator() (int *p)
~A

I don't understand why destructor calling two time. If there is some idea?

Aucun commentaire:

Enregistrer un commentaire