I would like to create a function that generates lambdas, where each of the lambdas has its own static variable. However, counter to what I thought would happen, the static variable seems to be shared between instances. For instance,
#include <iostream>
auto make_lambda(){
return [](){
static auto count = 0;
return count++;
};
}
int main() {
auto a = make_lambda();
auto b = make_lambda();
std::cout << &a << ", " << a() << std::endl;
std::cout << &b << ", " << b() << std::endl;
}
returns
0x7ffc229178df, 0
0x7ffc229178de, 1
So a and b seem to be unique instances, but are sharing that static count. I thought that I would see, and indeed want to see, something like
0x7ffc229178df, 0
0x7ffc229178de, 0
What is the simplest way to make sure that the lambdas each have their own static variable?