I was thinking a lot what title to put on my question and failed anyway, so if you find a good one, please edit it.
I am trying to write a print function for a vector
or other container<T>
and have another print function for container<container<T>>
, so here what I came up with:
template<typename T>
void print(T const& cont){
for (const auto& i : cont) {
cout << i << " ";
}
cout << endl;
}
template<typename T, template<typename> typename Cont>
void print(Cont<T> const& cont) {
for (const auto& i : cont) {
print(i);
}
}
and I have my 2 target containers here:
vector<vector<int>> subsets;
vector<int> subset;
When I invoke print(subset);
the program works as expected, but when i invoke print(subsets)
, compiler starts complaining:
error C2679: binary '<<': no operator found which takes a right-hand operand of type 'const std::vector<int,std::allocator<int>>' (or there is no acceptable conversion)
My conclusion is that its still trying to call the non-nested template print function and failing on cout
as I am trying to cout a vector.
Can anyone explain why the overload resolution is not working as I expect and what I did wrong here? Even when I rename the nested-template function to printn, its started to complain for a different reason:
error C2784: 'void prints(const Cont<T> &)': could not deduce template argument for 'const Cont<T> &' from 'std::vector<std::vector<int,std::allocator<int>>,std::allocator<std::vector<int,std::allocator<int>>>>'
Aucun commentaire:
Enregistrer un commentaire