rodrykbyk
rodrykbyk

Reputation: 105

C++ no type named ‘value_type’ in ‘struct std::iterator_traits<int>'

Hello. I am trying to run the following code (just for training purposes):

#include<iostream>
#include <list>

template<class T,
        template<class ,class=std::allocator<T> >class kont > 
typename std::iterator_traits<T>::value_type foo_test(typename kont<T>::iterator b){return *b;}


template <class Iter>
typename std::iterator_traits<Iter>::value_type minimum(Iter b, Iter e)
{    
      Iter m = b;
    /*
     CODE
     */
    return *m;
}

int main(void){
    std::list<int> x;
    x.push_back(10);
    x.push_back(100);
    std::cout <<minimum(x.begin(),x.end());
    //std::cout <<foo_test<int,std::list>(x.begin());
}

The function minimum is working fine and there are no issues. However, when I uncomment last line I recieve the following error:

main.cpp:33:50: error: no matching function for call to ‘foo_test(std::__cxx11::list<int>::iterator)’
     std::cout <<foo_test<int,std::list>(x.begin());                                                                        
main.cpp:7:46: note:   template argument deduction/substitution failed:
main.cpp:33:50:   required from here
main.cpp:7:46: error: no type named ‘value_type’ in ‘struct std::iterator_traits<int>’

Whats wrong with the first template then? I would be more than grateful for explanation.

Upvotes: 9

Views: 16699

Answers (1)

aschepler
aschepler

Reputation: 72431

You pass int as your first template parameter T. So std::iterator_traits<T>::value_type is std::iterator_traits<int>::value_type, which is incorrect. You meant

typename std::iterator_traits<typename kont<T>::iterator>::value_type.

Upvotes: 7

Related Questions