fork download
  1. #include <type_traits>
  2. #include <iterator>
  3. #include <vector>
  4.  
  5. template<typename T, typename = void>
  6. struct is_const_iterator : std::false_type { };
  7.  
  8. template<typename T>
  9. struct is_const_iterator<T,
  10. typename std::enable_if<
  11. std::is_const<
  12. typename std::remove_pointer<
  13. typename std::iterator_traits<T>::pointer
  14. >::type
  15. >::value
  16. >::type> : std::true_type { };
  17.  
  18. template<typename It,
  19. typename std::enable_if<!is_const_iterator<It>::value>::type* = nullptr>
  20. void foo(It i)
  21. {
  22. // Does something with i...
  23. }
  24.  
  25. int main()
  26. {
  27. std::vector<int> v;
  28. foo(v.begin()); // OK
  29. foo(v.cbegin()); // ERROR!
  30. }
  31.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:29:23: error: no matching function for call to ‘foo(std::vector<int>::const_iterator)’
prog.cpp:29:23: note: candidate is:
prog.cpp:20:10: note: template<class It, typename std::enable_if<(! is_const_iterator<It, void>::value), void>::type* <anonymous> > void foo(It)
prog.cpp:20:10: note:   template argument deduction/substitution failed:
prog.cpp:19:73: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
prog.cpp:19:73: note: invalid template non-type parameter
stdout
Standard output is empty