I am trying to use std :: for_each to output the contents of vectors that may contain different types. Therefore, I wrote a general output function as follows:
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 about "failed to print the template argument" in the for_each statement. Also complains about "function template cannot be an argument for another function template".
Is it impossible? I would think that the compiler would know the type vec_out (it is a vector) and therefore should create an instance of the function "output (const double & val)"?
If this does not work, how can I get similar STL functions without writing manual loops?
I am new to C ++ and still learning the ropes :-)