What is the actual type f ? so can i declare a vector of this type?
Type f can only be inferred using auto . You can declare a vector this type using
std::vector<decltype(f)> v;
However, this is not very useful. Lambda functions that seem strikingly similar have different types. Even worse, lambda functions having the same body also have different types.
auto f = [] (std::string msg) -> void { std::cout << msg << std::endl; }; auto g = [] (std::string msg) -> void { std::cout << msg << std::endl; }; auto h = [] (std::string msg) -> void { std::cout << msg << '+' << msg << std::endl; };
Given the above functions, you cannot use
std::vector<decltype(f)> v; v.push_back(f);
Your best option is to create std::vector from std::function s. You can add lambda functions to this std::vector . Given the above definitions of f and g , you can use:
std::vector<std::function<void(std::string)>> v; v.push_back(f); v.push_back(g); v.push_back(h);
source share