boost::p tr_vector vs. std::vector<std::unique_ptr<T>>?

boost::ptr_vector vs. std::vector<std::unique_ptr<T>>?

本文关键字:lt gt std vector ptr tr boost vs unique      更新时间:2023-10-16

他们做类似的事情。

在两者之间进行选择时应该考虑什么?

在哪种情况下,哪一种是首选?

我更喜欢std::vector<std::unique_ptr<T>>,原因有几个:

  1. 类型安全——虽然这在boost::ptr_vector中已经很好地抽象了,但ptr_vector仍然是根据std::vector<void*>实现的
  2. 明确支持自定义deleter——我认为您可以使用boost::ptr_vector获得自定义deleter行为,但使用std::unique_ptr时支持更清晰(更明确)
  3. 它是标准的,并得到兼容的C++11编译器的良好支持