xref: /freebsd-src/contrib/llvm-project/libcxx/include/__utility/is_valid_range.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
2*0fca6ea1SDimitry Andric //
3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0fca6ea1SDimitry Andric //
7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
8*0fca6ea1SDimitry Andric 
9*0fca6ea1SDimitry Andric #ifndef _LIBCPP___UTILITY_IS_VALID_RANGE_H
10*0fca6ea1SDimitry Andric #define _LIBCPP___UTILITY_IS_VALID_RANGE_H
11*0fca6ea1SDimitry Andric 
12*0fca6ea1SDimitry Andric #include <__algorithm/comp.h>
13*0fca6ea1SDimitry Andric #include <__config>
14*0fca6ea1SDimitry Andric #include <__type_traits/is_constant_evaluated.h>
15*0fca6ea1SDimitry Andric 
16*0fca6ea1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17*0fca6ea1SDimitry Andric #  pragma GCC system_header
18*0fca6ea1SDimitry Andric #endif
19*0fca6ea1SDimitry Andric 
20*0fca6ea1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
21*0fca6ea1SDimitry Andric 
22*0fca6ea1SDimitry Andric template <class _Tp>
23*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool
24*0fca6ea1SDimitry Andric __is_valid_range(const _Tp* __first, const _Tp* __last) {
25*0fca6ea1SDimitry Andric   if (__libcpp_is_constant_evaluated()) {
26*0fca6ea1SDimitry Andric     // If this is not a constant during constant evaluation, that is because __first and __last are not
27*0fca6ea1SDimitry Andric     // part of the same allocation. If they are part of the same allocation, we must still make sure they
28*0fca6ea1SDimitry Andric     // are ordered properly.
29*0fca6ea1SDimitry Andric     return __builtin_constant_p(__first <= __last) && __first <= __last;
30*0fca6ea1SDimitry Andric   }
31*0fca6ea1SDimitry Andric 
32*0fca6ea1SDimitry Andric   return !__less<>()(__last, __first);
33*0fca6ea1SDimitry Andric }
34*0fca6ea1SDimitry Andric 
35*0fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
36*0fca6ea1SDimitry Andric 
37*0fca6ea1SDimitry Andric #endif // _LIBCPP___UTILITY_IS_VALID_RANGE_H
38