dimanche 16 août 2020

Why compiler cannot deduce template template argument?

I want to write a function that takes a generic container with any type and prints it.
Lets leave for a moment that it wont work for some associative containers and focus on the issue:

template<template<typename> typename Cont, typename T>
void print(const Cont<T>& cont) 
{
    for (const auto it : cont) {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    vector<string> v;
    print(v);
}

The error states:

error C2672: 'print': no matching overloaded function found    
error C3207: 'print': invalid template argument for 'Cont', class template expected

Can anyone please explain why compiler cannot deduce the types here?
Even when I explicitly state print<vector<string>>(v)??

Aucun commentaire:

Enregistrer un commentaire