3
votes

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 :-)

3
sorry, vec_out is of type <code>vector<double></code>, not vector.PowerApp101
Both dirkgently and Naveen provided the correct answer, which I understand now...thanks!PowerApp101
Flag the answers as accepted so that later readers know (from the main page list)David Rodríguez - dribeas

3 Answers

9
votes

Try:

std::for_each(vec_out.begin(), vec_out.end(), output<T>);

where vec_out is a container (vector) of type T.

Note: The for_each algorithm expects an unary functor for its last argument. See the link for an example using functors.

7
votes

You have to pass an instantiation of the template. Something like output<int> if your vector is vector of integers.

For example:

template<typename T> void output(const T& val)
{
    cout << val << endl;
}



void main(int argc,char *argv[])
{
    std::vector<int> vec_out;
    std::for_each(vec_out.begin(), vec_out.end(), output<int>);
}   
7
votes

I'd just like to add to the correct answers: the compiler can deduce the type if you wrap up your template function in a function object (aka functor):

struct OutputFunctor
{
  template <typename T>
  void operator()(const T& val) const { output(val); }
};

void test()
{
  std::vector<int> ints;
  std::vector<float> floats;

  std::for_each(ints.begin(), ints.end(), OutputFunctor());
  std::for_each(floats.begin(), floats.end(), OutputFunctor());
}