簡體   English   中英

std :: bind和boost :: bind與多態性之間的區別

[英]Difference between std::bind and boost::bind with polymorphism

我有一個派生類,我綁定了一個虛函數,我沒有在這個類中重寫,所以我希望調用父類的一個。
它適用於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> 1> [1> _Rx = bool,1> _Pmf_t = bool(__ thishisall 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 

這不是一個重大問題,因為我可以堅持提升,但我真的想知道兩者之間有什么區別。

我在這里檢查了幾個問題,但我不認為它會如何與相關。 在這里檢查了stroustrup的網站,但我沒有看到任何可以解釋這種行為的東西。
我在這里錯過了什么?

Ps:我使用VS2012 Update 4,如果這可以幫助

Visual Studio 2012有很多與std::functionstd::bind相關的錯誤。 這是其中之一; 該代碼將在Visual Studio 2010和Visual Studio 2013中都有效。

最好的選擇是獨占使用Boost.Function和Boost.Bind。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM