在没有 C++11 的情况下使用 unique_ptr

Using a unique_ptr without C++11

我想将指向 d 的指针从一个容器传递到下一个容器。在任何时候,指针的所有者(或 d 本身)都不会超过一个。我希望当最后一个指针超出范围时,delete d 自动调用。

在 C++11 中,我将使用 unique_ptr 来完成此操作。但是,唉,我不能使用 C++11。 C++ 中最好的等价物是什么?升压就好了或者,如果有 none,处理这个问题的适当方法是什么?

嗯,boost::movelib::unique_ptr is part of the Boost.Move library 提供 "Portable move semantics for C++03 and C++11 compilers"。由于 unique_ptr 显然需要移动语义,这看起来是您的最佳选择。