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?
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.
Note: Bug 17798 has been fixed in newer versions of the library implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With