File indexing completed on 2026-05-03 08:13:45
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___UTILITY_IS_POINTER_IN_RANGE_H
0010 #define _LIBCPP___CXX03___UTILITY_IS_POINTER_IN_RANGE_H
0011
0012 #include <__cxx03/__algorithm/comp.h>
0013 #include <__cxx03/__assert>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__type_traits/enable_if.h>
0016 #include <__cxx03/__type_traits/integral_constant.h>
0017 #include <__cxx03/__type_traits/is_constant_evaluated.h>
0018 #include <__cxx03/__type_traits/void_t.h>
0019 #include <__cxx03/__utility/declval.h>
0020 #include <__cxx03/__utility/is_valid_range.h>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 template <class _Tp, class _Up, class = void>
0029 struct __is_less_than_comparable : false_type {};
0030
0031 template <class _Tp, class _Up>
0032 struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
0033 };
0034
0035 template <class _Tp, class _Up, __enable_if_t<__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
0036 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
0037 __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
0038 _LIBCPP_ASSERT_VALID_INPUT_RANGE(std::__is_valid_range(__begin, __end), "[__begin, __end) is not a valid range");
0039
0040 if (__libcpp_is_constant_evaluated()) {
0041
0042
0043 if (!__builtin_constant_p(__begin <= __ptr && __ptr < __end))
0044 return false;
0045 }
0046
0047 return !__less<>()(__ptr, __begin) && __less<>()(__ptr, __end);
0048 }
0049
0050 template <class _Tp, class _Up, __enable_if_t<!__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
0051 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
0052 __is_pointer_in_range(const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
0053 if (__libcpp_is_constant_evaluated())
0054 return false;
0055
0056 return reinterpret_cast<const char*>(__begin) <= reinterpret_cast<const char*>(__ptr) &&
0057 reinterpret_cast<const char*>(__ptr) < reinterpret_cast<const char*>(__end);
0058 }
0059
0060 _LIBCPP_END_NAMESPACE_STD
0061
0062 #endif