Monday, 26 August 2013

How is std::function implemented?

How is std::function implemented?

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.
An std::function should 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::function internally uses a pointer to its target,
then what happens, when the std::function instance is copied or moved? Are
there any heap allocations involved?

No comments:

Post a Comment