incrementable_traits.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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>
  27. struct incrementable_traits {};
  28. template <class _Tp>
  29. requires is_object_v<_Tp>
  30. struct incrementable_traits<_Tp*> {
  31. using difference_type = ptrdiff_t;
  32. };
  33. template <class _Ip>
  34. struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
  35. template <class _Tp>
  36. concept __has_member_difference_type = requires { typename _Tp::difference_type; };
  37. template <__has_member_difference_type _Tp>
  38. struct incrementable_traits<_Tp> {
  39. using difference_type = typename _Tp::difference_type;
  40. };
  41. template <class _Tp>
  42. concept __has_integral_minus = 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 =
  57. typename conditional_t<__is_primary_template<iterator_traits<remove_cvref_t<_Ip> > >::value,
  58. incrementable_traits<remove_cvref_t<_Ip> >,
  59. iterator_traits<remove_cvref_t<_Ip> > >::difference_type;
  60. #endif // _LIBCPP_STD_VER >= 20
  61. _LIBCPP_END_NAMESPACE_STD
  62. #endif // _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H