boost :: shared_ptr和std :: shared_ptr的同居

[英]Cohabitation of boost::shared_ptr and std::shared_ptr


I want to use boost::log at some point, but I cannot pass a std::shared_ptr as a parameter, because the compiler (VS2010) cannot convert it into a boost::shared_ptr.

我想在某些时候使用boost :: log,但是我不能将std :: shared_ptr作为参数传递,因为编译器(VS2010)无法将其转换为boost :: shared_ptr。

I don't really like the fact that they are aliens to one another.

我真的不喜欢他们是外星人的事实。

Is there a safe and transparent way to convert one into the another, so as they don't stumble over each other?

是否有安全透明的方式将一个转换为另一个,因为它们不会相互绊倒?

I don't think it is duplicate of this question that states both are the same.

我不认为这个问题的重复是两个都是相同的。

1 个解决方案

#1


71  

You could do it like this:

你可以这样做:

template<typename T>
boost::shared_ptr<T> make_shared_ptr(std::shared_ptr<T>& ptr)
{
    return boost::shared_ptr<T>(ptr.get(), [ptr](T*) mutable {ptr.reset();});
}

template<typename T>
std::shared_ptr<T> make_shared_ptr(boost::shared_ptr<T>& ptr)
{
    return std::shared_ptr<T>(ptr.get(), [ptr](T*) mutable {ptr.reset();});
}

EDIT: Note that this does not work with weak references to the source ptr. So be careful with those!

编辑:请注意,这不适用于源ptr的弱引用。所以要小心那些!


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/09/07/51599fa3341096705243ccabd69d0845.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号