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