incrementable_traits.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <__concepts/arithmetic.h>
  12. #include <__config>
  13. #include <__type_traits/conditional.h>
  14. #include <__type_traits/is_object.h>
  15. #include <__type_traits/is_primary_template.h>
  16. #include <__type_traits/make_signed.h>
  17. #include <__type_traits/remove_cvref.h>
  18. #include <__utility/declval.h>
  19. #include <cstddef>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. #if _LIBCPP_STD_VER >= 20
  25. // [incrementable.traits]
  26. template<class> struct incrementable_traits {};
  27. template<class _Tp>
  28. requires is_object_v<_Tp>
  29. struct incrementable_traits<_Tp*> {
  30. using difference_type = ptrdiff_t;
  31. };
  32. template<class _Ip>
  33. struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
  34. template<class _Tp>
  35. concept __has_member_difference_type = requires { typename _Tp::difference_type; };
  36. template<__has_member_difference_type _Tp>
  37. struct incrementable_traits<_Tp> {
  38. using difference_type = typename _Tp::difference_type;
  39. };
  40. template<class _Tp>
  41. concept __has_integral_minus =
  42. requires(const _Tp& __x, const _Tp& __y) {
  43. { __x - __y } -> integral;
  44. };
  45. template<__has_integral_minus _Tp>
  46. requires (!__has_member_difference_type<_Tp>)
  47. struct incrementable_traits<_Tp> {
  48. using difference_type = make_signed_t<decltype(std::declval<_Tp>() - std::declval<_Tp>())>;
  49. };
  50. template <class>
  51. struct iterator_traits;
  52. // Let `RI` be `remove_cvref_t<I>`. The type `iter_difference_t<I>` denotes
  53. // `incrementable_traits<RI>::difference_type` if `iterator_traits<RI>` names a specialization
  54. // generated from the primary template, and `iterator_traits<RI>::difference_type` otherwise.
  55. template <class _Ip>
  56. using iter_difference_t = typename conditional_t<__is_primary_template<iterator_traits<remove_cvref_t<_Ip> > >::value,
  57. incrementable_traits<remove_cvref_t<_Ip> >,
  58. iterator_traits<remove_cvref_t<_Ip> > >::difference_type;
  59. #endif // _LIBCPP_STD_VER >= 20
  60. _LIBCPP_END_NAMESPACE_STD
  61. #endif // _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H