C++ 使用std::shared_ptr对象实例创建boost::thread
我有以下两个代码段。第一个块按预期编译和工作。但是,第二个块不编译 我的问题是,给定下面的代码,当试图基于共享的ptr代理的对象实例创建线程时,正确的语法是什么C++ 使用std::shared_ptr对象实例创建boost::thread,c++,boost,c++11,shared-ptr,boost-thread,C++,Boost,C++11,Shared Ptr,Boost Thread,我有以下两个代码段。第一个块按预期编译和工作。但是,第二个块不编译 我的问题是,给定下面的代码,当试图基于共享的ptr代理的对象实例创建线程时,正确的语法是什么 #include <iostream> #include <new> #include <memory> #include <boost/thread.hpp> struct foo { void boo() {} }; int main() { //This work
#include <iostream>
#include <new>
#include <memory>
#include <boost/thread.hpp>
struct foo
{
void boo() {}
};
int main()
{
//This works
{
foo* fptr = new foo;
boost::thread t(&foo::boo,fptr);
t.join();
delete fptr;
}
//This doesn't work
{
std::shared_ptr<foo> fptr(new foo);
boost::thread t(&foo::boo,fptr);
t.join();
}
return 0;
}
#包括
#包括
#包括
#包括
结构foo
{
void boo(){}
};
int main()
{
//这很有效
{
foo*fptr=新的foo;
boost::线程t(&foo::boo,fptr);
t、 join();
删除fptr;
}
//这不管用
{
std::共享ptr fptr(新foo);
boost::线程t(&foo::boo,fptr);
t、 join();
}
返回0;
}
编译器错误:
Error 5 error C2784: 'T *boost::get_pointer(T *)' : could not deduce template argument for 'T *' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 3 error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 4 error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 8 error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 9 error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 1 error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 2 error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 6 error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 7 error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 10 error C2784: 'T *boost::get_pointer(const boost::intrusive_ptr<T> &)' : could not deduce template argument for 'const boost::intrusive_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 11 error C2784: 'T *boost::get_pointer(const boost::intrusive_ptr<T> &)' : could not deduce template argument for 'const boost::intrusive_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest
Error 5 Error C2784:'T*boost::get_pointer(T*)':无法从'std::tr1::shared_ptr'c:\program files(x86)\boost\boost\boost\u 1\u 47\boost\bind\mem\u fn\u template.hpp 40 1 htest中推断出'T*'的模板参数
错误3错误C2784:“T*boost::get_pointer(const std::auto_ptr&)”:无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1\u 47\boost\bind\mem\u fn_template.hpp 40 1 htest推断出“const std::auto_ptr&”的模板参数
错误4错误C2784:“T*boost::get_pointer(const std::auto_ptr&)”:无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1\u 47\boost\bind\mem\u fn_template.hpp 40 1 htest推断出“const std::auto_ptr&”的模板参数
错误8错误C2784:“T*boost::get_pointer(const boost::shared_ptr&)”:无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest中推断出“const boost::shared_ptr&”的模板参数
错误9错误C2784:“T*boost::get_pointer(const boost::shared_ptr&)”:无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest中推断出“const boost::shared_ptr&”的模板参数
错误1错误C2784:“T*boost::get_pointer(const boost::scoped_ptr&”):无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest中推断出“const boost::scoped_ptr&”的模板参数
错误2错误C2784:“T*boost::get_pointer(const boost::scoped_ptr&”):无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest推断出“const boost::scoped_ptr&”的模板参数
错误6错误C2784:“T*boost::get_pointer(const boost::reference_wrapper&)”:无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest中推断出“const boost::reference_wrapper&”的模板参数
错误7错误C2784:“T*boost::get_pointer(const boost::reference_wrapper&”):无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest推断出“const boost::reference_wrapper&”的模板参数
错误10错误C2784:“T*boost::get_pointer(const boost::intrusive_ptr&”):无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest推断出“const boost::intrusive_ptr&”的模板参数
错误11错误C2784:“T*boost::get_pointer(const boost::intrusive_ptr&”):无法从“std::tr1::shared_ptr”c:\program files(x86)\boost\boost\u 1_47\boost\bind\mem\u fn_template.hpp 40 1 htest推断出“const boost::intrusive_ptr&”的模板参数
问题在于boost::thread
依赖boost::mem\u fn
来处理成员函数,以及boost::mem\u fn
(或者至少是您正在使用的版本)不知道如何使用std::shared_ptr
调用成员函数,因为它希望您使用boost::shared_ptr
或错误列表中无数其他智能指针类型之一
原始指针之所以有效,是因为boost::mem_fn
已经具有该重载。解决方案是使用boost::shared_ptr
或std::mem_fn
。后者之所以有效,是因为std::mem\u fn
知道如何与std::shared\u ptr
boost::线程t(std::mem_fn(&foo::boo),fptr)代码>戴夫答案的另一种选择是定义这个(在包含
之前):
namespace boost
{
模板
内联T*
获取指针(const std::shared\u ptr&p)
{返回p.get();}
}
它“教导”boost::mem\u fn
从std::shared\u ptr获取原始指针
在C++11中,std::mem_fn
需要使用任何类似指针的类型,只需取消引用即可,即*fptr
,但boost::mem_fn
使用*boost::get_pointer(fptr)
。我不知道它是否在最新版本的Boost中得到了修复,但我认为它应该使用SFINAE来检测get\u pointer
是否有效,否则应该取消对它的引用
namespace boost
{
template<typename T>
inline T*
get_pointer(const std::shared_ptr<T>& p)
{ return p.get(); }
}