fork(27) download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <vector>
  4.  
  5. namespace detail
  6. {
  7. // To allow ADL with custom begin/end
  8. using std::begin;
  9. using std::end;
  10.  
  11. template <typename T>
  12. auto is_iterable_impl(int)
  13. -> decltype (
  14. begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator !=
  15. ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++
  16. *begin(std::declval<T&>()), // operator*
  17. std::true_type{});
  18.  
  19. template <typename T>
  20. std::false_type is_iterable_impl(...);
  21.  
  22. }
  23.  
  24. template <typename T>
  25. using is_iterable = decltype(detail::is_iterable_impl<T>(0));
  26.  
  27.  
  28. static_assert(is_iterable<std::vector<int>>::value, "");
  29. static_assert(is_iterable<int[42]>::value, "");
  30. static_assert(!is_iterable<int>::value, "");
  31.  
  32. int main()
  33. {
  34. std::cout << is_iterable<std::vector<int>>::value << std::endl;
  35. std::cout << is_iterable<int[42]>::value << std::endl;
  36. std::cout << is_iterable<int>::value << std::endl;
  37. }
  38.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
1
1
0