File indexing completed on 2026-05-03 08:13:07
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___ALGORITHM_CLAMP_H
0010 #define _LIBCPP___ALGORITHM_CLAMP_H
0011
0012 #include <__algorithm/comp.h>
0013 #include <__assert>
0014 #include <__config>
0015
0016 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0017 # pragma GCC system_header
0018 #endif
0019
0020 _LIBCPP_BEGIN_NAMESPACE_STD
0021
0022 #if _LIBCPP_STD_VER >= 17
0023 template <class _Tp, class _Compare>
0024 [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
0025 clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
0026 _LIBCPP_LIFETIMEBOUND const _Tp& __lo,
0027 _LIBCPP_LIFETIMEBOUND const _Tp& __hi,
0028 _Compare __comp) {
0029 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
0030 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
0031 }
0032
0033 template <class _Tp>
0034 [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
0035 clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
0036 _LIBCPP_LIFETIMEBOUND const _Tp& __lo,
0037 _LIBCPP_LIFETIMEBOUND const _Tp& __hi) {
0038 return std::clamp(__v, __lo, __hi, __less<>());
0039 }
0040 #endif
0041
0042 _LIBCPP_END_NAMESPACE_STD
0043
0044 #endif