incrementable_traits.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
  10. #define _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
  11. #include <__config>
  12. #include <concepts>
  13. #include <type_traits>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  19. // [incrementable.traits]
  20. template<class> struct incrementable_traits {};
  21. template<class _Tp>
  22. requires is_object_v<_Tp>
  23. struct incrementable_traits<_Tp*> {
  24. using difference_type = ptrdiff_t;
  25. };
  26. template<class _Ip>
  27. struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
  28. template<class _Tp>
  29. concept __has_member_difference_type = requires { typename _Tp::difference_type; };
  30. template<__has_member_difference_type _Tp>
  31. struct incrementable_traits<_Tp> {
  32. using difference_type = typename _Tp::difference_type;
  33. };
  34. template<class _Tp>
  35. concept __has_integral_minus =
  36. requires(const _Tp& __x, const _Tp& __y) {
  37. { __x - __y } -> integral;
  38. };
  39. template<__has_integral_minus _Tp>
  40. requires (!__has_member_difference_type<_Tp>)
  41. struct incrementable_traits<_Tp> {
  42. using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
  43. };
  44. template <class>
  45. struct iterator_traits;
  46. // Let `RI` be `remove_cvref_t<I>`. The type `iter_difference_t<I>` denotes
  47. // `incrementable_traits<RI>::difference_type` if `iterator_traits<RI>` names a specialization
  48. // generated from the primary template, and `iterator_traits<RI>::difference_type` otherwise.
  49. template <class _Ip>
  50. using iter_difference_t = typename conditional_t<__is_primary_template<iterator_traits<remove_cvref_t<_Ip> > >::value,
  51. incrementable_traits<remove_cvref_t<_Ip> >,
  52. iterator_traits<remove_cvref_t<_Ip> > >::difference_type;
  53. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  54. _LIBCPP_END_NAMESPACE_STD
  55. #endif // _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H