Because you don't understand the difference between a function and a function template. In your code print_all()
is a function template that takes type T. It's only instantiated if the compiler detects a call to print_all(T)
, and your print_all()
code assumes that T has defined operator[]
. f is a function (not a function template) that takes type std::vector, which is ill-formed, as all vectors require a type they hold, i.e. std::vector<int>
, std::vector<char*>
, std::vector<something_i_found_under_thecouch>
, there is no such thing as just std::vector
. If you want your print_all()
function to work with any vector, you'll need to redeclare it as a function template:
template <class T>
void f(const std::vector<T>& v);
and/or
template <class T>
void f(std::vector<T>&& v);
Cheers!