2014-06-10 23 views
6

此問題來自this question使用lambda構造std :: function時,libstdC++和libC++之間的不同行爲

下使用鐺3.4用的libstdC++代碼compiles fine:使用

#include <functional> 

int main() { 
    std::function<void()> f = []() {}; 
} 

fails miserably鐺3.4)和libc(++:

In file included from main.cpp:1: 
In file included from /usr/include/c++/v1/functional:465: 
In file included from /usr/include/c++/v1/memory:599: 
/usr/include/c++/v1/tuple:320:11: error: rvalue reference to type '<lambda at main.cpp:4:31>' cannot bind to lvalue of type '<lambda at main.cpp:4:31>' 
     : value(__t.get()) 
     ^ ~~~~~~~~~ 
/usr/include/c++/v1/tuple:444:8: note: in instantiation of member function 'std::__1::__tuple_leaf<0, <lambda at main.cpp:4:31> &&, false>::__tuple_leaf' requested here 
struct __tuple_impl<__tuple_indices<_Indx...>, _Tp...> 
    ^
/usr/include/c++/v1/functional:1278:26: note: in instantiation of member function 'std::__1::__function::__func<<lambda at main.cpp:4:31>, std::__1::allocator<<lambda at main.cpp:4:31> >, void()>::__func' requested here 
      ::new (__f_) _FF(_VSTD::move(__f)); 
         ^
main.cpp:4:31: note: in instantiation of function template specialization 'std::__1::function<void()>::function<<lambda at main.cpp:4:31> >' requested here 
    std::function<void()> f = []() {}; 
          ^
In file included from main.cpp:1: 
In file included from /usr/include/c++/v1/functional:465: 
In file included from /usr/include/c++/v1/memory:599: 
/usr/include/c++/v1/tuple:321:10: error: static_assert failed "Can not copy a tuple with rvalue reference member" 
     {static_assert(!is_rvalue_reference<_Hp>::value, "Can not copy a tuple with rvalue reference member");} 
     ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/usr/include/c++/v1/tuple:320:11: error: rvalue reference to type 'allocator<[...]>' cannot bind to lvalue of type 'allocator<[...]>' 
     : value(__t.get()) 
     ^ ~~~~~~~~~ 
/usr/include/c++/v1/tuple:444:8: note: in instantiation of member function 'std::__1::__tuple_leaf<0, std::__1::allocator<<lambda at main.cpp:4:31> > &&, false>::__tuple_leaf' requested here 
struct __tuple_impl<__tuple_indices<_Indx...>, _Tp...> 
    ^
/usr/include/c++/v1/functional:1286:34: note: in instantiation of member function 'std::__1::__function::__func<<lambda at main.cpp:4:31>, std::__1::allocator<<lambda at main.cpp:4:31> >, void()>::__func' requested here 
      ::new (__hold.get()) _FF(_VSTD::move(__f), allocator<_Fp>(__a)); 
           ^
main.cpp:4:31: note: in instantiation of function template specialization 'std::__1::function<void()>::function<<lambda at main.cpp:4:31> >' requested here 
    std::function<void()> f = []() {}; 
          ^
In file included from main.cpp:1: 
In file included from /usr/include/c++/v1/functional:465: 
In file included from /usr/include/c++/v1/memory:599: 
/usr/include/c++/v1/tuple:321:10: error: static_assert failed "Can not copy a tuple with rvalue reference member" 
     {static_assert(!is_rvalue_reference<_Hp>::value, "Can not copy a tuple with rvalue reference member");} 
     ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
4 errors generated. 

哪些行爲是正確的?

回答

7

令人驚訝的是,有什麼執行是正確的,哪一個做錯了;該片段當然應該編譯。 libc++有錯誤的行爲,下面是相關錯誤報告的鏈接。


注意Bug 17798已經固定在圖書館實施的新版本。

+0

是的,一旦減少到'[](){}'形式,正確的行爲是相當明顯的。感謝您提供錯誤報告的鏈接。 –

相關問題