Suppose I have a function that does something on an arbitrary container type (C++11):
template<class containerType>
void bar( containerType& vec ) {
for (auto i: vec) {
std::cout << i << ", ";
}
std::cout << '\n';
}
I can call this function from another function like this:
void foo() {
std::vector<int> vec = { 1, 2, 3 };
bar(vec);
}
Now suppose I have different functions just like bar, and I want to pass one of these functions to foo, then foo would look something like this:
template<class funcType>
void foo( funcType func ) {
std::vector<int> vec = { 1, 2, 3 };
func(vec);
}
However, calling foo like this:
foo(bar);
does not work (pretty clear, since bar is not a function but a function template). Is there any nice solution to this? How must I define foo to make this work?
EDIT: here is a minimal compileable example, as demanded in the comments…
#include <iostream>
#include <vector>
#include <list>
template<class containerType>
void bar( containerType& vec ) {
for (auto i: vec) {
std::cout << i << ", ";
}
std::cout << '\n';
}
template<typename funcType>
void foo(funcType func) {
std::vector<int> vals = { 1, 2, 3 };
func(vals);
}
int main() {
// foo( bar ); - does not work.
}
Something like this? (not fully awake, might miss the point)