#include <stdio.h>
#include <functional>
void foo(int a, int b)
{
printf("%d %d\n", a, b);
}
int main()
{
using namespace std::placeholders;
auto f1 = std::bind(foo, 10, _1); // fine
std::function<void (int)> f2 = f1; // fine
auto f3 = std::bind(f2, 20); // fine
std::function<void ()> f4 = f3; // stack overflow
std::function<void ()> f5 = [=](){f3();}; // stack overflow
f3();
return 0;
}
I am writing a simple library and using std::function
as callback for the library user.
I want something like f4
to simplify the callback function.
I am not familiar with the std::bind
internals.
Why f3
could not construct f4
?
I am using
clang++ : Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
on a OSX10.9 macbook
compiled with :
clang++ -g -std=c++0x test.cpp
stack overflow at f4 because infinite call of the flowing thing:
frame #0:
frame #1:
...
frame #6361: 0x0000000100001ff2 a.out`std::__1::function<void ()>::function<std::__1::__bind<std::__1::function<void (int)>&, int> >(std::__1::__bind<std::__1::function<void (int)>&, int>, std::__1::enable_if<__callable<std::__1::__bind<std::__1::function<void (int)>&, int> >::value, void>::type*) [inlined] std::__1::unique_ptr<std::__1::__function::__base<void (this=0x00007fff5fbfeb00, this=0x0000000100103a90, __f=0x00007fff5fbff4e0, __a=0x00007fff5fbfeae8)>, std::__1::__allocator_destructor<std::__1::allocator<std::__1::__function::__func<std::__1::__bind<std::__1::function<void (int)>&, int>, std::__1::allocator<std::__1::__bind<std::__1::function<void (int)>&, int> >, void ()> > > >::get() const + 42 at functional:1007
frame #6362: 0x0000000100001fc8 a.out`std::__1::function<void (this=0x00007fff5fbff530, __f=0x00007fff5fbff4e0, =0x0000000000000000)>::function<std::__1::__bind<std::__1::function<void (int)>&, int> >(std::__1::__bind<std::__1::function<void (int)>&, int>, std::__1::enable_if<__callable<std::__1::__bind<std::__1::function<void (int)>&, int> >::value, void>::type*) + 776 at functional:1285
frame #6363: 0x0000000100001a3d a.out`std::__1::function<void (this=0x00007fff5fbff530, __f=<unavailable>, =0x0000000000000000)>::function<std::__1::__bind<std::__1::function<void (int)>&, int> >(std::__1::__bind<std::__1::function<void (int)>&, int>, std::__1::enable_if<__callable<std::__1::__bind<std::__1::function<void (int)>&, int> >::value, void>::type*) + 29 at functional:1289
frame #6364: 0x000000010000189c a.out`main + 956 at test.cpp:15
This was a bug in libc++. It has been fixed. If you're not using the latest version of Xcode, update. If you are using the latest version of Xcode, you can find the very latest libc++ here: http://libcxx.llvm.org . This bug is definitely fixed in the svn repository.