fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <type_traits>
  5. #include <iterator>
  6.  
  7. template<typename Iterator, typename U = typename std::vector<typename std::iterator_traits<Iterator>::value_type>::iterator, typename = void>
  8. auto f(Iterator beg, U end)
  9. -> typename std::enable_if<!std::is_same<Iterator, U>::value, void>::type
  10. {
  11. for(auto it = beg; it != end; it = std::next(it))
  12. {
  13. std::cout << *it << " iterator!" << std::endl;
  14. }
  15. }
  16. /*
  17. template<typename Iterator, typename U = typename std::vector<typename std::iterator_traits<Iterator>::value_type>::iterator>
  18. auto f(Iterator beg, U end)
  19. -> typename std::enable_if<std::is_same<Iterator, U>::value, void>::type
  20. {
  21.   for(auto it = beg; it != end; it = std::next(it))
  22.   {
  23.   std::cout << *it << " vector!" << std::endl;
  24.   }
  25. }*/
  26.  
  27. int main()
  28. {
  29. std::list<int> x = {1,2,3};
  30. std::cout << "List" << std::endl;
  31. f(x.begin(), x.end());
  32.  
  33. std::cout << "Vector" << std::endl;
  34. std::vector<int> v = {1,2,3};
  35. f(v.begin(), v.end());
  36. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:31:25: error: no matching function for call to ‘f(std::__cxx11::list<int>::iterator, std::__cxx11::list<int>::iterator)’
     f(x.begin(), x.end());
                         ^
prog.cpp:8:6: note: candidate: template<class Iterator, class U, class> typename std::enable_if<(! std::is_same<Iterator, U>::value), void>::type f(Iterator, U)
 auto f(Iterator beg, U end)
      ^
prog.cpp:8:6: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class Iterator, class U, class> typename std::enable_if<(! std::is_same<Iterator, U>::value), void>::type f(Iterator, U) [with Iterator = std::_List_iterator<int>; U = std::_List_iterator<int>; <template-parameter-1-3> = void]’:
prog.cpp:31:25:   required from here
prog.cpp:8:6: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
prog.cpp:35:25: error: no matching function for call to ‘f(std::vector<int>::iterator, std::vector<int>::iterator)’
     f(v.begin(), v.end());
                         ^
prog.cpp:8:6: note: candidate: template<class Iterator, class U, class> typename std::enable_if<(! std::is_same<Iterator, U>::value), void>::type f(Iterator, U)
 auto f(Iterator beg, U end)
      ^
prog.cpp:8:6: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class Iterator, class U, class> typename std::enable_if<(! std::is_same<Iterator, U>::value), void>::type f(Iterator, U) [with Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; U = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; <template-parameter-1-3> = void]’:
prog.cpp:35:25:   required from here
prog.cpp:8:6: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
stdout
Standard output is empty