Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

Non-copyable attribute means that class have copy constructor private, so you can't call it outside of class. When operating on stl containers, items are copied or moved, therefore it won't work together.

If you need to use smart pointer with stl containers, you should use one, that implements move operations (such as c++11 unique_ptr).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top