- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
const int sum(int a, int b){
return a+b;
}
int a(int x)
{
const std::function<int(int)> sum4 = std::bind(sum,_1, 4);
return sum4(123);
}
int b(int x)
{
puts("bagor");
const std::function<int(int)> sum4 = std::bind(sum,_1, 4);
return sum4(123);
}
int c(int x)
{
const std::function<int(int)> sum4 = std::bind(sum,_1, 4);
puts("bagor");
return sum4(123);
}
// Функции a и b нормально инлайнятся. Ассемблерный выхлоп:
sum(int, int): # @sum(int, int)
lea eax, [rdi + rsi]
ret
a(int): # @a(int)
mov eax, 127
ret
b(int): # @b(int)
push rax
mov edi, offset .L.str
call puts
mov eax, 127
pop rcx
ret
//А вот int c(int x)
c(int): # @c(int)
push rbx
sub rsp, 32
mov edi, 16
call operator new(unsigned long)
mov rbx, rax
mov qword ptr [rax], offset sum(int, int)
mov dword ptr [rax + 8], 4
mov qword ptr [rsp], rax
mov qword ptr [rsp + 24], offset std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&)
mov qword ptr [rsp + 16], offset std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation)
mov edi, offset .L.str
call puts
mov esi, dword ptr [rbx + 8]
mov edi, 123
call qword ptr [rbx]
mov ebx, eax
mov rax, qword ptr [rsp + 16]
test rax, rax
je .LBB3_3
mov rdi, rsp
mov rsi, rdi
mov edx, 3
call rax
.LBB3_3:
mov eax, ebx
add rsp, 32
pop rbx
ret
mov rdi, rax
call __clang_call_terminate
mov rbx, rax
mov rax, qword ptr [rsp + 16]
test rax, rax
je .LBB3_6
mov rdi, rsp
mov rsi, rdi
mov edx, 3
call rax
.LBB3_6:
mov rdi, rbx
call _Unwind_Resume@PLT
mov rdi, rax
call __clang_call_terminate
__clang_call_terminate: # @__clang_call_terminate
push rax
call __cxa_begin_catch
call std::terminate()
std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&): # @std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&)
mov rax, qword ptr [rdi]
mov rcx, qword ptr [rax]
mov edi, dword ptr [rsi]
mov esi, dword ptr [rax + 8]
jmp rcx # TAILCALL
std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation): # @std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation)
Решил попробовать std::bind.
https://godbolt.org/z/eW5eT5oj4