Search code examples
c++booststlscoped-ptr

boost::scoped_ptr and STL in C++


I'm reading about boost smart pointers, and one thing I'm not able to grasp is why boost::scoped_ptr can't be used with STL containers? I have read it's non-copyable, but what exactly does that mean and why does STL need that?


Solution

  • Some container operations, such as std::vector's constructor

    vector(size_type n, const T& value, const Allocator& = Allocator());
    

    or std::vector::resize, require that T be CopyInsertable. This requires that the vector be able to call T's copy constructor. So you could not construct an std::vector<boost::scoped_ptr<U>> using this constructor.

    Before C++11, the requirements on T were more strict. Since the latest standard, the requirements are tailored to each container operation, rather than the container as a whole. So there are probably many situations in which you could use a vector of boost::scoped_ptrs.