Is a multiple assignment into unique < T > pointer valid? Based on output it is, but is T destructor calling guaranteed when make_unique is called and value is assigned to already allocated memory?
#include <iostream>
#include <string>
#include <memory>
class A{
public:
A(){ std::cout << "Construcor" << std::endl;}
~A(){ std::cout << "Destrucor" << std::endl;}
void foo(){std::cout << "foo" << std::endl;}
};
int main()
{
std::unique_ptr<A> pointer;
for(auto i = 0; i< 2; ++i){
pointer = std::make_unique<A>();
pointer->foo();
}
}
output:
Construcor
foo
Construcor
Destrucor // Destructor is called because first instance of A is out of scope?
foo
Destrucor
Aucun commentaire:
Enregistrer un commentaire