I am trying to use std::for_each to output the contents of vectors, which may contain different types. So I wrote a generic output function like so:
template<typename T> void output(const T& val)
{
cout << val << endl;
}
which I would like to use with:
std::for_each(vec_out.begin(), vec_out.end(), output);
but the compiler complains with "could not deduce template argument" in the for_each statement. Also complains with "A function template cannot be an argument to another function template".
Is this not possible? I would have thought the compiler would know the type of vec_out (it's vector) and so should instantiate the function "output(const double& val)"?
If this doesn't work how can I get similar STL functionality without writing manual loops?
I am quite new to C++ and still learning the ropes :-)