boost::scoped_ptr与std::unique_ptr都是类模板,封装了指针
两者都禁用了拷贝构造和赋值函数,因此不能作为STL容器中的元素,因为在执行push_back()时需要调用赋值函数。
std::unique_ptr实际上与boost::scoped_ptr是等价的,只是std将boost::scoped_ptr拿来名字改为了std::unique_ptr,两者的实现方式是一致的。 boost::scoped_ptr源码如下:
template<class T>
class scoped_ptr
{
private:ScopedPtr(const ScopedPtr<T>& ap);ScopedPtr<T>& operator=(const ScopedPtr<T>& ap);public:ScopedPtr(T* ptr = nullptr):_ptr(ptr){}~ScopedPtr(){if(_ptr){delete _ptr;_ptr = nullptr;}}private:T* _ptr;
};
C++11实现的std::unique_ptr以后,并没有去实现std::unique_array,是因为标准库中有vector,其实是很好用的