Difference between std::bind and boost::bind with polymorphism

一曲冷凌霜 提交于 2019-12-10 17:28:18

问题


I have a derived class from which I bind a virtual function that I did not override in this class, so I'm hoping to call the one of the parent class.
It works nice with boost (1.55), but if I switch to std::bind from C++11, it refuse to compile with

error C2100: illegal indirection 1> functional(1152) : see reference to function template instantiation '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::operator ()(_Wrapper &) const' being compiled 1> with 1> [ 1> _Rx=bool, 1> _Pmf_t=bool (__thiscall Base::* )(void), 1> _Farg0=Base, 1> _V0_t=std::_Nil, 1> _V1_t=std::_Nil, 1> _V2_t=std::_Nil, 1> _V3_t=std::_Nil, 1> _V4_t=std::_Nil, 1> _V5_t=std::_Nil, 1> =std::_Nil, 1> _Wrapper=Derived 1> ]

Here is a minimum code

class Runnable { virtual bool Run() =0;};
class Base : public Runnable { bool Run() {/*do stuff*/ return true;}};
class Derived : public Base {};

Derived d;
std::function<bool()> f = std::bind(&Derived::Run,std::ref(d)); //Do not compile
std::function<bool()> f = boost::bind(&Derived::Run,boost::ref(d)); //compile 

It's not a major issue, since I can stick with boost, but I would really want to know what's the difference between the two.

I've checked few questions in here but I don't think how it will be related to this. Checked stroustrup's site too here, but I did not see anything that could explain this behaviour.
What am I missing here ?

Ps: I work with VS2012 Update 4, if this can help


回答1:


Visual Studio 2012 has a lot of bugs relating to std::function and std::bind. This is one of them; the code will work in both Visual Studio 2010 and Visual Studio 2013.

The best choice is to use Boost.Function and Boost.Bind exclusively.



来源:https://stackoverflow.com/questions/22048090/difference-between-stdbind-and-boostbind-with-polymorphism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!