Guest User

Untitled

a guest
Nov 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <type_traits>
  2. #include <vector>
  3.  
  4. template <typename T, typename = void>
  5. struct is_iterable : std::false_type {};
  6.  
  7. template <typename T>
  8. struct is_iterable<T, std::void_t<decltype(std::declval<T&>().begin() == std::declval<T&>().end())>> : std::true_type {};
  9.  
  10. template <class T>
  11. constexpr bool is_iterable_v = is_iterable<T>::value;
  12.  
  13.  
  14. template <typename T, typename = void>
  15. struct iterable_value_type
  16. {
  17. using type = std::false_type;
  18. };
  19.  
  20. template <typename T>
  21. struct iterable_value_type<T, std::void_t<decltype(T::value_type)>>
  22. {
  23. using type = typename T::value_type;
  24. };
  25.  
  26. template <class T>
  27. using iterable_value_type_t = typename iterable_value_type<T>::type;
  28.  
  29.  
  30. // Transforms a concept "does type T have property P" into the concept
  31. // "is type T1 an iterable with value_type T2 where T2 has property P"
  32. template <typename T, template <typename...> typename BaseConcept>
  33. struct CollectionConcept
  34. {
  35. static_assert(BaseConcept<typename T::value_type>::value);
  36. static constexpr bool value = is_iterable_v<T> && BaseConcept<iterable_value_type_t<T>>::value;
  37. };
  38.  
  39. int main()
  40. {
  41. static_assert(std::is_arithmetic<int>::value);
  42. static_assert(CollectionConcept<std::vector<int>, std::is_arithmetic>::value);
  43. }
Add Comment
Please, Sign In to add comment