Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <type_traits>
- #include <vector>
- template <typename T, typename = void>
- struct is_iterable : std::false_type {};
- template <typename T>
- struct is_iterable<T, std::void_t<decltype(std::declval<T&>().begin() == std::declval<T&>().end())>> : std::true_type {};
- template <class T>
- constexpr bool is_iterable_v = is_iterable<T>::value;
- template <typename T, typename = void>
- struct iterable_value_type
- {
- using type = std::false_type;
- };
- template <typename T>
- struct iterable_value_type<T, std::void_t<decltype(T::value_type)>>
- {
- using type = typename T::value_type;
- };
- template <class T>
- using iterable_value_type_t = typename iterable_value_type<T>::type;
- // Transforms a concept "does type T have property P" into the concept
- // "is type T1 an iterable with value_type T2 where T2 has property P"
- template <typename T, template <typename...> typename BaseConcept>
- struct CollectionConcept
- {
- static_assert(BaseConcept<typename T::value_type>::value);
- static constexpr bool value = is_iterable_v<T> && BaseConcept<iterable_value_type_t<T>>::value;
- };
- int main()
- {
- static_assert(std::is_arithmetic<int>::value);
- static_assert(CollectionConcept<std::vector<int>, std::is_arithmetic>::value);
- }
Add Comment
Please, Sign In to add comment