fork download
  1. #include <iostream>
  2.  
  3. namespace x {
  4.  
  5. template <typename... Ts>
  6. struct make_void {
  7. typedef void type;
  8. };
  9.  
  10. template <typename... Ts> using void_t = typename make_void<Ts...>::type;
  11.  
  12. template <class T>
  13. using dereference_t = decltype(*std::declval<T>());
  14.  
  15. namespace detail {
  16.  
  17. template <class T, class = void>
  18. struct is_iterator_impl : std::false_type {};
  19.  
  20. template <class T>
  21. struct is_iterator_impl<T, void_t<dereference_t<T>>> : std::true_type {};
  22.  
  23. template <class T, bool = false>
  24. struct is_const_iterator_impl : std::false_type {};
  25.  
  26. template <class T>
  27. struct is_const_iterator_impl<T, true> : std::is_const<std::remove_reference_t<dereference_t<T>>> {};
  28.  
  29. } // namespace detail
  30.  
  31. template <class T>
  32. struct is_iterator : detail::is_iterator_impl<T> {};
  33.  
  34. template <class T>
  35. struct is_const_iterator : detail::is_const_iterator_impl<T, is_iterator<T>::value> {};
  36.  
  37. } // namespace x
  38.  
  39. int main() {
  40. std::cout << x::is_const_iterator<int>::value << std::endl;
  41. std::cout << x::is_const_iterator<int*>::value << std::endl;
  42. std::cout << x::is_const_iterator<const int*>::value << std::endl;
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
0
0
1