gpt4 book ai didi

c++ - 从 boost::bind 移动到 std::bind:编译错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:26 25 4
gpt4 key购买 nike

我构建并完美运行了这段代码:

boost::function<void(string)> bar = boost::bind(&Bar::BarHandler, this, _1);

//Somewhere else in Bar.cpp
void Bar::BarHandler( std::string message ){
//Do stuff
}

当我愉快地将上面代码中的 boost 更改为 std 时,我开始收到此错误(我的编译器是 Visual Studio 2010 SP1 的):

c:\program files\microsoft visual studio 10.0\vc\include\xxpmfcaller(42): error C2664: 

'void (std::string)' : cannot convert parameter 1 from 'boost::arg<I>' to 'std::string'
1> with
1> [
1> I=1
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\program files\microsoft visual studio 10.0\vc\include\xxpmfcaller(52) : see reference to function template instantiation '_Ret std::tr1::_Pmf_caller2<_Ret,_Arg0>::_Call_pmf<_Pmf,_Ty,_Farg1>(volatile const void *,_Pmf,_Farg0 &,_Farg1 &)' being compiled
1> with
1> [
1> _Ret=_Rx,
1> _Arg0=BarLib::Bar,
1> _Pmf=void (__thiscall BarLib::Bar::* const )(std::string),
1> _Ty=BarLib::Bar *,
1> _Farg1=boost::arg<1>,
1> _Farg0=BarLib::Bar *
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxcallpmf(9) : see reference to function template instantiation '_Ret std::tr1::_Pmf_caller2<_Ret,_Arg0>::_Apply_pmf<const _Ty,_Arg,boost::arg<I>>(_Pmf,_Farg0 &,_Farg1 &)' being compiled
1> with
1> [
1> _Ret=_Rx,
1> _Arg0=BarLib::Bar,
1> _Ty=void (__thiscall BarLib::Bar::* const )(std::string),
1> _Arg=BarLib::Bar *,
1> I=1,
1> _Pmf=void (__thiscall BarLib::Bar::* const )(std::string),
1> _Farg0=BarLib::Bar *,
1> _Farg1=boost::arg<1>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxbind1(292) : see reference to function template instantiation '_Ret std::tr1::_Callable_pmf<_Ty,_Memty,_Indirect>::_ApplyX<_Ret,_Arg&,boost::arg<I>&>(_Arg0,_Arg1) const' being compiled
1> with
1> [
1> _Ret=_Rx,
1> _Ty=void (__thiscall BarLib::Bar::* const )(std::string),
1> _Memty=BarLib::Bar,
1> _Indirect=false,
1> _Arg=BarLib::Bar *,
1> I=1,
1> _Arg0=BarLib::Bar *&,
1> _Arg1=boost::arg<1> &
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxbind0(31) : see reference to function template instantiation '_Ret std::tr1::_Bind2<_Callable,_Arg0,_Arg1>::_ApplyX<_Rx,std::basic_string<_Elem,_Traits,_Ax>&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Barg0,_Barg1,_Barg2,_Barg3,_Barg4,_Barg5,_Barg6,_Barg7,_Barg8,_Barg9)' being compiled
1> with
1> [
1> _Ret=_Rx,
1> _Callable=std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,
1> _Arg0=BarLib::Bar *,
1> _Arg1=boost::arg<1>,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>,
1> _Barg0=std::string &,
1> _Barg1=std::tr1::_Nil &,
1> _Barg2=std::tr1::_Nil &,
1> _Barg3=std::tr1::_Nil &,
1> _Barg4=std::tr1::_Nil &,
1> _Barg5=std::tr1::_Nil &,
1> _Barg6=std::tr1::_Nil &,
1> _Barg7=std::tr1::_Nil &,
1> _Barg8=std::tr1::_Nil &,
1> _Barg9=std::tr1::_Nil &
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxcallobj(13) : see reference to function template instantiation 'void std::tr1::_Bind_base<_Ret,_BindN>::operator ()<_Arg0&>(_Carg0)' being compiled
1> with
1> [
1> _Ret=void,
1> _BindN=std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>,
1> _Arg0=std::string,
1> _Carg0=std::string &
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(65) : see reference to function template instantiation '_Ret std::tr1::_Callable_obj<_Ty>::_ApplyX<_Rx,_Arg0&>(std::basic_string<_Elem,_Traits,_Ax>)' being compiled
1> with
1> [
1> _Ret=void,
1> _Ty=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>,
1> _Rx=void,
1> _Arg0=std::string,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(64) : while compiling class template member function 'void std::tr1::_Impl_no_alloc1<_Callable,_Rx,_Arg0>::_Do_call(_Arg0)'
1> with
1> [
1> _Callable=_MyWrapper,
1> _Rx=void,
1> _Arg0=std::string
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(386) : see reference to class template instantiation 'std::tr1::_Impl_no_alloc1<_Callable,_Rx,_Arg0>' being compiled
1> with
1> [
1> _Callable=_MyWrapper,
1> _Rx=void,
1> _Arg0=std::string
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(369) : see reference to function template instantiation 'void std::tr1::_Function_impl1<_Ret,_Arg0>::_Reset0o<_Myimpl,_Fty,std::allocator<_Ty>>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void,
1> _Arg0=std::string,
1> _Fty=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>,
1> _Ty=std::tr1::_Function_impl1<void,std::string>,
1> _Alloc=std::allocator<std::tr1::_Function_impl1<void,std::string>>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\functional(113) : see reference to function template instantiation 'void std::tr1::_Function_impl1<_Ret,_Arg0>::_Reset<_Fx>(_Fty)' being compiled
1> with
1> [
1> _Ret=void,
1> _Arg0=std::string,
1> _Fx=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>,
1> _Fty=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>
1> ]
1> d:\svn.dra.workingcopy\acquisition\native\xraygenerator\gemammogenerator.cpp(101) : see reference to function template instantiation 'std::tr1::function<_Fty>::function<std::tr1::_Bind<_Result_type,_Ret,_BindN>>(_Fx)' being compiled
1> with
1> [
1> _Fty=void (std::string),
1> _Result_type=void,
1> _Ret=void,
1> _BindN=std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>,
1> _Fx=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>
1> ]
1>
1>Build FAILED.

这是怎么回事?也许我应该删除对 Boost header 的引用?

最佳答案

您可能需要包含 <functional>并为 _1 显式使用占位符命名空间

using namespace std::placeholders;

编辑:正如评论中所指出的,您还需要删除将占位符引入全局命名空间的 boost header 。

关于c++ - 从 boost::bind 移动到 std::bind:编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10195734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com