I have some weird problem with templates. When trying to pass a parameterised iterator it complains that no function can be found. The code snippet is here, forget about the functionality, it’s using the reference to the templatized iterator what interests me
#include <list> #include <iostream> template<typename T> void print_list_element(typename std::list<T>::iterator& it){ std::cout << *it << std::endl; } int main() { std::list<int> theList; theList.push_back(1); std::list<int>::iterator it = theList.begin(); print_list_element(it); return 0; }
If you try to compile this with g++ v4.3.2 it complains with a message saying that:
main.cpp:14: error: no matching function for call to 'print_list_element(std::_List_iterator<int>&)'
Is there something wrong with the code I wrote or is that g++ needs more information?
g++ can’t figure out which template overload of
print_list_elementit should use. If you explicitly specify the template parameter it works: