How do you pass a link when using typename as an argument to a function in C ++?

I have some kind of weird problem with templates. When trying to pass a parameterized iterator, it complains that no function can be found. Code snippet here, forget about the functionality, it uses a link to the templatized iterator that 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 about the message 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 that g ++ needs more information?

+3
source share
4 answers

g++ , print_list_element . , :

print_list_element<int>(it);
+9

​​:

template<typename Iter>
void print_element(Iter it){
    std::cout << *it << std::endl;
}

, std::list<T>::iterator. , .

, , , , list<T>::iterator . , , ListType, .

+5

, std:: list <T> :: iterator - ,

+4

, , ++ , .

, , . std::list<T>::iterator , typedef (, T*), , - " ", T* std::list<T>::iterator T . , , iterator, typedef ed T* - T*in, and there is no choice between them. Obviously, any auto-deduction policy that breaks when an unrelated class adds a specific type member typedefis too unstable to work.

+4
source

Source: https://habr.com/ru/post/1706449/