Taming lambdas' uniqueness

Speaker: Dawid Zalewski

Audience level: [ Intermediate | Advanced ]

There's nothing more different than two lambda expressions. Not even [](){} is the same as [](){}, at least not from the compiler's point of view. By the rules of the C++ language, each lambda results in a unique type. This leads to problems, and not of the trivial kind. Making a couple of lambdas of the same type but with different capture values? Nope. Writing a function that takes lambdas with identical signatures as an argument? Not easy. Storing merely two lambdas in a vector? Impossible. And doing any of this while maintaining readability and efficiency? Better give it up already.

But there are techniques and tricks that you can use to work around this seemingly impregnable limitation. By using indirection, weaving abstraction layers, applying type erasure or switching to static polymorphism, it is possible to turn lambdas into well-behaved language constructs that play nicely with existing facilities. In this talk, we will show how to create multiple lambdas of the same type, write functions that can accept different lambdas, and make lambdas work with standard containers. Not only that, but we'll try to keep the code readable while sacrificing little or no runtime performance.