xref: /freebsd/contrib/llvm-project/libcxx/include/__iterator/incrementable_traits.h (revision a4e5e0106ac7145f56eb39a691e302cabb4635be)
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> struct incrementable_traits {};
33 
34 template<class _Tp>
35 requires is_object_v<_Tp>
36 struct incrementable_traits<_Tp*> {
37   using difference_type = ptrdiff_t;
38 };
39 
40 template<class _Ip>
41 struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
42 
43 template<class _Tp>
44 concept __has_member_difference_type = requires { typename _Tp::difference_type; };
45 
46 template<__has_member_difference_type _Tp>
47 struct incrementable_traits<_Tp> {
48   using difference_type = typename _Tp::difference_type;
49 };
50 
51 template<class _Tp>
52 concept __has_integral_minus =
53   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 = typename conditional_t<__is_primary_template<iterator_traits<remove_cvref_t<_Ip> > >::value,
71                                                  incrementable_traits<remove_cvref_t<_Ip> >,
72                                                  iterator_traits<remove_cvref_t<_Ip> > >::difference_type;
73 
74 #endif // _LIBCPP_STD_VER >= 20
75 
76 _LIBCPP_END_NAMESPACE_STD
77 
78 #endif // _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
79