In the case of using a std::unique_ptr to automatically deallocate memory upon exiting a scoped block, why not just use the stack?
问题 This is a great answer about smart pointers, such as unique pointers: What is a smart pointer and when should I use one?. Here is an example they provide as the simplest use of a unique pointer: void f() { { std::unique_ptr<MyObject> ptr(new MyObject(my_constructor_param)); ptr->DoSomethingUseful(); } // ptr goes out of scope -- // the MyObject is automatically destroyed. // ptr->Oops(); // Compile error: "ptr" not defined // since it is no longer in scope. } However, this begs the question: