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