How to write a template function that works on an arbitrary container of an arbitrary type? For example, how can I generalize this dummy function
template <typename Element>
void print_size(const std::vector<Element> & a)
{
cout << a.size() << endl;
}
to
template <template<typename> class Container, typename Element>
void print_size(const Container<Element> & a)
{
cout << a.size() << endl;
}
Here is a typical use
std::vector<std::string> f;
print_size(f)
It gives an error
tests/t_distances.cpp:110:12: error: no matching function for call to ‘print(std::vector<std::basic_string<char> >&)’. I'm guessing I must tell the compiler something more specific about what types that are allowed.
What is the name of this use case for the template and how to fix it?
source
share