Type-erasure and lambda expressions

Type-erasure is a well known and documented topic in C++. Just google ‘type-erasure C++’ and the first page of results will present articles discussing the topic from simple examples to advanced scenarios. Oh, and be sure to have C++ in the query since type-erasure in Java means a completely different thing.

Many of these articles create some kind of Variant type that is capable of holding objects whose type is defined and known upfront. In this post I’m creating something that can hold and later invoke a capturing lambda expression (whose type is not defined by standard). This is what std::function does (and much more), and I was wondering what magic might happen inside std::function. There are no new concepts in this post compared to those articles, but handling lambdas is something (I think) worth writing about.
Continue reading