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