What is the actual type of f
? so I can declare a vector of this type?
Type of f
can only be deduced by using auto
. You can declare a vector
of this type using
std::vector<decltype(f)> v;
However, it is not very useful. Lambda functions that look strikingly similar have different types. To make matters worse, lambda functions that have identical 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 can't use
std::vector<decltype(f)> v;
v.push_back(f); // OK
v.push_back(g); // Not OK
v.push_back(h); // Not OK
Your best option is to create a std::vector
of std::function
s. You can add the lambda functions to that 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);