Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behavior between libstdc++ and libc++ when constructing std::function with lambda

This problem came from this question.

The following code compiles fine using clang 3.4 with libstdc++:

#include <functional>

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

But fails miserably using clang 3.4 and 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.

Which behavior is correct?

like image 464
T.C. Avatar asked Jun 10 '14 08:06

T.C.


1 Answers

I'm surprised that there's any doubt regarding what implementation that is doing it right, and which is doing it wrong; the snippet should of course compile. libc++ has the wrong behavior, and below is a link to the relevant bug report.

  • Bug 17798 - Regression: std::function cannot be assigned a lambda function

Note: Bug 17798 has been fixed in newer versions of the library implementation.

like image 98
Filip Roséen - refp Avatar answered Oct 19 '22 17:10

Filip Roséen - refp