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

时间:2014-02-26 16:56:46

标签: c++ visual-studio-2012 c++11 boost-bind stdbind

我有一个派生类,我绑定了一个我没有在这个类中重写的虚函数,所以我希望调用父类之一。
它适用于boost(1.55),但是如果我从C ++ 11切换到std :: bind,它拒绝使用

进行编译
  

错误C2100:非法间接   1 GT; functional(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 GT;同   1 GT; [   1 GT; _Rx =布尔,   1 GT; _Pmf_t = bool(__ thiscall Base :: *)(void),   1 GT; _Farg0 =基地   1 GT; _V0_t =的std :: _无,   1 GT; _V1_t =的std :: _无,   1 GT; _V2_t =的std :: _无,   1 GT; _V3_t =的std :: _无,   1 GT; _V4_t =的std :: _无,   1 GT; _V5_t =的std :: _无,   1 GT; =标准:: _无,   1 GT; _Wrapper =派生   1 GT; ]

这是最小代码

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,如果这可以帮助

1 个答案:

答案 0 :(得分:2)

Visual Studio 2012存在许多与std::functionstd::bind相关的错误。这是其中之一;代码将在Visual Studio 2010和Visual Studio 2013中都有效。

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