I have seen how to declare a vector of functions (see calling a function from a vector).
But that answer users pointers. How can I create a vector of functions/lambdas using the new syntax in modern C++?
the examples of functions using the new syntax typically use auto:
auto f = [] (std::string msg) -> void {
std::cout << msg << std::endl;
};
What is the actual type of f? so I can declare a vector of this type?
thank you very much for any help
Use std::function
with the corresponding type:
std::vector<std::function<void(void)>> vec;
vec.push_back([]()->void{});
In your case it would be std::function<void(std::string)>
.
The exact type of a lambda is meaningless per standard ([expr.prim.lambda]/3):
The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With