升压:: shared_ptr的和std :: shared_ptr的同居 [英] Cohabitation of boost::shared_ptr and std::shared_ptr

查看:188
本文介绍了升压:: shared_ptr的和std :: shared_ptr的同居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的boost ::登录在某些时候,但我不能传递的std :: shared_ptr的作为参数,因为编译器(VS2010)无法将其转换成的boost :: 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.

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

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?

我不认为这是重复的<一个href=\"http://stackoverflow.com/questions/3831572/differences-between-tr1shared-ptr-and-boostshared-ptr\">this问题的,各国都是一样的。

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

推荐答案

您可以做到这一点是这样的:

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();});
}

编辑:请注意,这不与向源PTR弱引用工作。所以,要小心那些!

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

这篇关于升压:: shared_ptr的和std :: shared_ptr的同居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆