[英]boost::ptr_container and std::vector<shared_ptr>
After reading timdays answer to this question I am curious about the difference between boost::ptr_container
and a std::vector<shared_ptr>
. 阅读timdays回答 这个问题后,我很好奇
boost::ptr_container
和std::vector<shared_ptr>
之间的区别。 I was under the impression that a boost::ptr_container
had ownership over the pointers given to it , and upon release would call the destructors of all the pointers it contained regardless of other references to its inhabitants. 我的印象是
boost::ptr_container
对赋予它的指针拥有所有权 ,并且在释放时会调用它包含的所有指针的析构函数,而不管其居民的其他引用。 Which is contrary to the purpose of a std::vector<shared_ptr>
, which after release would only release the pointers themselves if the ref count was 0? 这与
std::vector<shared_ptr>
的目的相反,它在释放之后只会在ref计数为0时自行释放指针?
If this is the case (I assume it isn't), why would even the Boost documentation example compare the two as though they are similar in purpose, and why would timday's answer propose a boost::ptr_container
when it is very different to the purpose of a std::vector<shared_ptr>
. 如果是这种情况(我认为不是这样),为什么甚至Boost文档示例都会比较两者 ,就好像它们的目的相似,为什么timday的答案提出了一个
boost::ptr_container
当它与它非常不同时std::vector<shared_ptr>
。
You are right, the two are widely different. 你是对的,两者差别很大。
The first difference, as you noticed, is the ownership semantics. 正如您所注意到的,第一个区别是所有权语义。 The ownership of items in a Pointer Container is NOT shared.
不共享指针容器中项目的所有权。 In this regard, a
boost::ptr_vector<T>
is much closer to a std::vector<std::unique_ptr<T>>
. 在这方面,
boost::ptr_vector<T>
更接近std::vector<std::unique_ptr<T>>
。
But this is not the only difference! 但这不是唯一的区别!
new_clone
method), and can only be copied if the object held is copyable new_clone
方法),并且只有在持有的对象是可复制的情况下才能复制 const
then one cannot mutate one of its element. const
那么就不能改变其中一个元素。 As for why @timday
felt compelled to mention Boost Pointer Container, I think it's because he wanted to broaden the question somewhat. 至于为什么
@timday
感到被迫提到Boost Pointer Container,我认为这是因为他想在某种程度上扩大这个问题。 Boost Pointer Container are very much like Smart Pointers that could hold multiple objects, and provide a nicer syntax that containers of pointers in general. Boost Pointer Container非常像Smart Pointers,它可以容纳多个对象,并提供一个更好的语法,通常是指针的容器。
Regarding his comparison to a std::vector< boost::shared_ptr<T> >
I think it is simply because this is the traditional way of implementing a vector of pointers in the absence of move semantics (no unique_ptr
) since auto_ptr
cannot be used in STL container. 关于他与
std::vector< boost::shared_ptr<T> >
比较,我认为这只是因为这是在没有移动语义(没有unique_ptr
)的情况下实现指针向量的传统方法,因为不能使用auto_ptr
在STL容器中。 People just don't know about Pointer Containers most of the time... 人们大多数时候都不了解指针容器......
There are situations where both can be applied: say a bunch of functions act as clients of container, taking pointers to polymorphic objects out and doing operations on them. 在某些情况下,两者都可以应用:比如说一堆函数充当容器的客户端,指出多态对象并对它们进行操作。 If the container outlives all the functions, it can be replaced by a pointer container.
如果容器超过所有函数,则可以用指针容器替换它。
Timday answered the question "What is the difference between the following set of pointer[s]" by pointing out an omission in the list. Timday通过指出列表中的遗漏回答了“下面的一组指针[s]之间有什么区别”的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.