Consider the following code:
#include <memory>
#include <iostream>
#include <string>
struct Data {
std::string name;
Data(std::string aName): name(aName) {
std::cout << name << " Constructor is called\n";
}
~Data() {
std::cout << name << " Destructor is called \n";
}
};
int main() {
std::unique_ptr<Data> ptr1(new Data("Data1"));
std::unique_ptr<Data> ptr2(new Data("Data2"));
// Will this always call the destructor of the object owned by ptr2 ?
ptr2 = std::move(ptr1);
return 0;
}
Wil the line ptr2 = std::move(ptr1)
always call the destructor of the previously owned object of ptr2? If this is true, can we replace the code below:
std::unique_ptr<Data> ptr1(new Data("Data1"));
ptr2 = std::move(ptr1);
with
ptr2.reset(new Data("Data1"));
?
I tried searching in cpp reference doc and the similar questions in other forums, but I could not get a clarity on this.
Link to godbolt : https://godbolt.org/z/cYhKMWdsT