开发者

Can boost::smart_ptr be used in std containers?

开发者 https://www.devze.com 2023-02-05 20:44 出处:网络
Can boost::smart_ptr such as scoped_ptr and shared_ptr be used in std containers such as std::map? class SomeClass

Can boost::smart_ptr such as scoped_ptr and shared_ptr be used in std containers such as std::map?

class SomeClass
{
    std::map<int,boost::scoped_ptr<SomeOtherClass> > a_map;
};

As boost::smart_ptr can be used for polymorphism, is it true in this case as well? Will the destruction of the container, trigger the c开发者_JS百科orrect destruction of the subclasses?


scoped_ptr cannot be used in standard containers because it cannot be copied (which is required by the containers' interfaces). shared_ptr may be used, however.

If you can't use C++11 and you're using boost already, consider the pointer containers which will perform somewhat better than a container of shared pointers.

If you're using C++11, consider a container of unique_ptr, which should perform similarly to boost's pointer containers.

0

精彩评论

暂无评论...
验证码 换一张
取 消