#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;
}
간단한 라이브러리를 작성 중이며 std::function
을 콜백으로 사용하고 있습니다.std :: function을 std :: function으로 구축 할 때 스택 오버플로가 발생했습니다.
콜백 기능을 간소화하기 위해 f4
과 같은 것을 원합니다.
나는 std::bind
내부에 익숙하지 않은입니다.
왜 f3
은 f4
을 만들 수 없습니까?
내가 ++
연타를 사용하고 있습니다 : 애플 LLVM 버전 5.0 (연타-500.2.79)를 함께 컴파일 OSX10.9 맥북
에
(LLVM의 3.3svn 기준) :
그 소리 ++ -g -std = C + +0 Test.cpp에 F4에서
스택 오버 플로우 때문에 흐르는 것은 무한한 전화 :
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
런타임 스택 오버 플로우, 또는 컴파일 시간? 어떤 컴파일러이고 어떤 버전입니까? 당신이 바인드 표현 주민을 생략하면 – Yakk
그것이 발생합니까, 즉 직접'표준 : function' 변수에'표준 : bind'의 결과를 할당? –
clang ++ 3.4 및 g ++ 4.8.1 (스택 오버플로 없음)에 대한 예상 결과를 생성합니다. – dyp