This section of code gives an error:
template <class T>
void print_vector(vector<T>& v, string sep)
{
std::ostream_iterator<T> ostr_it(std::cout, sep) ;
std::copy(begin(v), end(v), ostr_it);
}
main.cpp:17:30: error: no matching constructor for initialization of 'std::ostream_iterator<float>' std::ostream_iterator<T> ostr_it(std::cout, sep);
I am confused because if I do it outside the template function and output the vector directly there is no error:
vector<float> result(elements);
std::copy(begin(result), end(result), ostream_iterator<float>(cout, ", "));
What is wrong? Do I need to specialize each template function?