better use emplace_back on new A(j) directly, then you don't need to create a temporary unique_ptr variable. the vector is a member variable of class B, it will not be destroyed until its parent object is destroyed. If it is not destroyed, the memory hold by those unique_ptrs will not be released.
【在 y**b 的大作中提到】 : 请看伪码: : class A; : class B { : std::vector> aVec; : public: : do() { : for (int i = 0; i < 1000000; ++i) { : for (int j = 0; j < 1000; ++j) { : // method 1, rValue : // aVec.push_back()std::unique_ptr(new A(j));
when the destructor of a container is called, the destructor of all its elements will be called automatically. For smart pointer, its destructor will release the resource it hold, for raw pointer, its destructor is a no- op.