mercredi 26 juillet 2017

Does release call the destructor

When calling release on a unique_ptr does this call the destructor of that class?

My Answer

The following program compiles with g++ -std=c++14 -o main main.cpp

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

struct A{
    ~A(){
        cout << "destructor of A was called" << endl;
    }
};

int main(){
    auto p_a = std::make_unique<A>();
    p_a.release();
    cout << "end of program" << endl;
}

and gives the following output

>main.exe
end of program

The cppreference article on release gives the following description:

Releases the ownership of the managed object if any. get() returns nullptr after the call.

So as far as I can tell it does not call the destructor but simply relinquishes the responsibility of lifetime management of that object.

Aucun commentaire:

Enregistrer un commentaire