2012-04-20 59 views
1

我在MSVC2010中遇到了lambdas問題。C++ 0x lambda + boost :: function trouble

它是成功的編譯:

auto f1 = []() { }; 
boost::function<void()> a = f1; 

但它帶來的錯誤C2440:

auto f2 = []() 
{ 
    auto f3 = []() { }; 
    boost::function<void()> b = f3; 
}; 

錯誤C2440:

'initializing' : cannot convert from 'int' to 'boost::function<Signature>' 
    with 
    [ 
     Signature=void (void) 
    ] 
    No constructor could take the source type, or constructor overload resolution was ambiguous 

爲什麼?它在GCC4.6中工作嗎?

+2

爲什麼你使用boost :: function而不是std :: function? – 2012-04-20 11:40:41

+0

因爲我的項目的依賴庫使用boost :: function。它是否適用於std :: function? – dizel3d 2012-04-20 11:44:34

+0

我用vc10測試了你的代碼,但是使用了'std :: function'代替。它編譯好。因此,使用clang 3.2和gcc 4.6.2的代碼。我沒有助推庫方便,所以我不能評論你的確切問題。 HTH! – dirkgently 2012-06-07 01:26:45

回答

0

可能是MSVC中的錯誤(錯誤消息看起來非常可疑)。它適用於GCC 4.7。

+0

decltype(f3)沒問題。它不是'int',它是'lambda0'。 – dizel3d 2012-04-20 11:20:33