Quick A: Using type erasure.
Quick Q2: ... Um, what's that?
Quick A2: Read this post...
According to the sources I have found, a lambda expression is essentially implemented by the compiler creating a class with overloaded function call operator and the referenced variables as members. This suggests that the size of lambda expressions varies, and given enough references variables that size can be arbitrarily large.
std::functionshould have a fixed size, but it must be able to wrap any kind of callables, including any lambdas of the same kind. How is it implemented? If
std::functioninternally uses a pointer to its target, then what happens, when the
std::functioninstance is copied or moved? Are there any heap allocations involved?