Generic lambdas are lambdas whose parameter types are auto and deduced by the argument types when called. For example:
#include <iostream>
int main(int argc, char const *argv[])
{
auto adder{
[](const auto &a, const auto &b)
{
return a + b;
}};
std::cout << adder(1, 2) << '\n';
return 0;
}This is similar to function templates where actual function is instantiated from when function is called. Similarly, adder(1, 2) calls to adder(int, int).
Same thing happens when static data variables are used with generic lambdas as happens with function templates. When generic lambda is called with different parameters, independent static variable is created for each type. For example:
auto print{
[](const auto &a)
{
static int times{0};
std::cout << ++times << ": " << a << '\n';
}};
print(1);
print(3);
print(5);
print(1.2);
print(1.4);
print(4.1);It will give result as:
1: 1
2: 3
3: 5
1: 1.2
2: 1.4
3: 4.1