可以在std容器中使用boost :: smart_ptr吗?

Jon*_*han 6 c++ polymorphism containers boost

可以在std容器中使用boost :: smart_ptr,例如scoped_ptr和shared_ptr,例如std :: map吗?

class SomeClass
{
    std::map<int,boost::scoped_ptr<SomeOtherClass> > a_map;
};
Run Code Online (Sandbox Code Playgroud)

由于boost :: smart_ptr可以用于多态,在这种情况下也是如此吗?是否会破坏容器,触发子类的正确销毁?

Bil*_*eal 20

scoped_ptr不能在标准容器中使用,因为它无法复制(容器的接口需要).shared_ptr但是,可以使用.

如果您不能使用C++ 11并且您已经使用了boost,请考虑指针容器,它将比共享指针容器更好地执行.

如果您正在使用C++ 11,请考虑一个容器unique_ptr,它应该与boost的指针容器类似地执行.