std :: bind和boost :: bind与多态性之间的区别 [英] Difference between std::bind and boost::bind with polymorphism

查看:869
本文介绍了std :: bind和boost :: bind与多态性之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个派生类,我绑定一个虚拟函数,我没有在这个类重写,所以我希望调用父类之一。

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

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


错误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>与
1> [
1> _Rx = bool,
1> _Pmf_t = bool(__thiscall Base :: *)(void),
1> _Farg0 = Base,
1> _V0_t = std :: _ Nil,
1> _V1_t = std :: _
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>]

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,

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.

我已经在这里检查了几个问题,但我不认为它将如何与
Checked stroustrup的网站太这里,但我没有看看任何可以解释这个行为的东西。

我在这里缺少什么?

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:我使用VS2012 Update 4,如果这可以帮助

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

推荐答案

Visual Studio 2012有很多与 std :: function std :: bind 。这是其中之一;该代码将在Visual Studio 2010和Visual Studio 2013中工作。

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.

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

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

这篇关于std :: bind和boost :: bind与多态性之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆