我有一个派生类,我从中绑定(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
这不是什么大问题,因为我可以坚持使用 boost,但我真的很想知道两者之间有什么区别。
我在这里检查了几个问题,但我不认为它与 this 有什么关系.
也检查了 stroustrup 的网站 here ,但我没有看到任何可以解释这种行为的东西。
我在这里错过了什么?
Ps:我使用的是 VS2012 Update 4,如果这有帮助的话
最佳答案
Visual Studio 2012 有很多与 std::function
和 std::bind
相关的错误。这是其中之一;该代码将在 Visual Studio 2010 和 Visual Studio 2013 中运行。
最好的选择是专门使用 Boost.Function 和 Boost.Bind。
关于c++ - std::bind 和 boost::bind 与多态性的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048090/