Quick Q: How is std::function implemented? -- StackOverflow

Quick A: Using type erasure.

Quick Q2: ... Um, what's that?

Quick A2: Read this post...

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?

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.