c – std :: bind和boost :: bind与多态性之间的区别

我有一个派生类,我绑定了一个虚函数,我没有在这个类中重写,所以我希望调用父类的一个.

它适用于boost(1.55),但如果我从C 11切换到std :: bind,它拒绝编译

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> ]

这是最低限度的代码

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 

这不是一个重大问题,因为我可以坚持提升,但我真的想知道两者之间有什么区别.

我在这里检查了几个问题,但我不认为它与this有什么关系.
检查了stroustrup的网站也是here,但我没有看到任何可以解释这种行为的东西.
我在这里错过了什么?

Ps:我使用VS2012 Update 4,如果这可以帮助

最佳答案 Visual Studio 2012有很多与std :: function和std :: bind相关的错误.这是其中之一;该代码将在Visual Studio 2010和Visual Studio 2013中都有效.

最好的选择是独占使用Boost.Function和Boost.Bind.

点赞