Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving from boost::bind to std::bind: Compile error

I had this code building and running perfectly:

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

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

When I happily changed boost by std in the code above, I started getting this error (my compiler is Visual Studio 2010 SP1's):

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.

What's going on? Maybe I should remove references to Boost headers?

like image 248
dario_ramos Avatar asked Apr 17 '12 17:04

dario_ramos


People also ask

Is std :: bind deprecated?

Yes: std::bind should be replaced by lambda For almost all cases, std::bind should be replaced by a lambda expression. It's idiomatic, and results in better code. There is almost no reason post C++11 to use std::bind .

What does std :: bind do in C++?

std::bind. std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

What is boost bind?

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.

What is the return type of std :: bind?

std::bind. Returns a function object based on fn , but with its arguments bound to args . Each argument may either be bound to a value or be a placeholder: - If bound to a value, calling the returned function object will always use that value as argument.


1 Answers

you probably need to include <functional> and explicitly use the placeholders namespace for the _1

using namespace std::placeholders;

Edit: As has been pointed out in comments, you would also need to remove the boost headers that bring in the placeholders into the global namespace.

like image 155
juanchopanza Avatar answered Sep 20 '22 04:09

juanchopanza