This is the price you pay for not needing to know the type of the function. All std::function<void()>
s are interchangeable no matter which lambda they came from. If you want to store lots of the same type of function (with different captures) in a vector, you can make it a functor instead of a lambda (so that it has a name) and make a vector of that type.
Example: With lambda:
std::vector<std::function<void()>> funcs;
for(int i = 0; i < 10000; i++)
funcs.push_back([i]() {std::cout << i << std::endl;});
for(auto& func : funcs)
func();
With functor:
struct number_printing_function {
int i;
number_printing_function(int i) : i(i) {}
void operator()() {
std::cout << i << std::endl;
}
};
std::vector<number_printing_function> funcs;
for(int i = 0; i < 10000; i++)
funcs.push_back(number_printing_function(i));
// or funcs.emplace_back(i);
for(auto& func : funcs)
func();
IMO this is a bit useless, because we might as well store a vector of ints and stop pretending they are functions. When you have many functors of the same type you already know what they do, so just do it. Really, the code above is just the code below, but with extra steps:
std::vector<int> ints;
for(int i = 0; i < 10000; i++)
ints.push_back(i);
for(auto& i : ints)
std::cout << i << std::endl;