std::function . template std::function .
- (Y-combinator vs std::function) . std::function 3,5 , Y-combinator. , , std::function , template.
gcc.godbolt.org, .
:
#if defined(STDFN)
void pass_by_stdfn(std::function<void()> f)
{
f();
}
#else
template <typename TF>
void pass_by_template(TF&& f)
{
f();
}
#endif
volatile int state = 0;
int main()
{
#if defined(STDFN)
pass_by_stdfn([i = 10]{ state = i; });
#else
pass_by_template([i = 10]{ state = i; });
#endif
}
STDFN , :
main:
mov DWORD PTR state[rip], 10
xor eax, eax
ret
state:
.zero 4
STDFN , 48 .