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