Call multiple lambdas with the same input
You can easily create multiple instances of a lambda with different capture values by wrapping the lambda in a function. This allows you to call different versions of a lambda with the same input.
How to do it…
This is a simple example of a lambda that wraps a value in different types of braces:
- We'll start by creating the wrapper function
braces()
:auto braces (const char a, const char b) { Â Â Â Â return [a, b](const char v) { Â Â Â Â Â Â Â Â cout << format("{}{}{} ", a, v, b); Â Â Â Â }; }
The braces()
function wraps a lambda that returns a three-value string, where the first and last values are characters passed to the lambda as captures, and the middle value is passed as a parameter.
- In the
main()
function, we usebraces()
to create four lambdas, using four different sets of braces:auto a = braces('(', &apos...