我有一个派生类,我从该派生类中绑定了一个在此类中没有覆盖的虚函数,因此我希望调用父类中的一个。
它适用于 boost (1.55),但如果我从 C++11 切换到 std::bind,它会拒绝编译
错误 C2100:非法间接 1> 功能(1152):参见对函数模板实例化的引用 '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::operator ()( _Wrapper &) const' 正在编译 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=派生 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
这不是一个主要问题,因为我可以坚持使用 boost,但我真的很想知道两者之间有什么区别。
我在这里检查了几个问题,但我不认为它与this有什么关系。在这里也检查了 stroustrup 的网站,但我没有看到任何可以解释这种行为的东西。
我在这里想念什么?
Ps:我使用VS2012 Update 4,如果这可以帮助