Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
clamp.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5///
6/// @file clamp.hpp
7/// @brief Provides implementation of a backport of C++20's version of std::clamp
8///
9#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_CLAMP_HPP_
10#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_CLAMP_HPP_
11
12// IWYU pragma: private, include "arene/base/algorithm.hpp"
13// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/constraints/constraints.hpp"
17#include "arene/base/contracts/contract.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/less.hpp"
20#include "arene/base/type_traits/is_compare.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23namespace arene {
24namespace base {
25
26// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
27// parasoft-begin-suppress AUTOSAR-A7_5_1-a "Returning a reference is required to match std::clamp."
28/// @brief Clamp the supplied value between two bounds using the supplied comparator for ordering
29/// @tparam T The type of the value and both bounds
30/// @tparam Compare The type of a comparator which models a strict weak ordering, similar to @c std::less
31/// @param val The value to clamp between the two bounds
32/// @param low The lower bound
33/// @param high The upper bound
34/// @param comp The comparator instance to use
35/// @pre @c low must be less than or equal to @c high ; that is, @c comp(high,low) must be @c false
36/// @return A reference to @c val if it's between the bounds, or to the nearest bound if @c val is outside the bounds
37template <typename T, class Compare, constraints<std::enable_if_t<is_compare_v<Compare&, T const&>>> = nullptr>
38constexpr auto clamp(T const& val, T const& low, T const& high, Compare comp) noexcept(noexcept(comp(val, low)))
39 -> T const& {
41
42 if (comp(val, low)) {
43 return low;
44 }
45
46 if (comp(high, val)) {
47 return high;
48 }
49
50 return val;
51}
52
53/// @brief Clamp the supplied value between two bounds using @c std::less for ordering
54/// @tparam T The type of the value and both bounds
55/// @param val The value to clamp between the two bounds
56/// @param low The lower bound
57/// @param high The upper bound
58/// @pre @c low must be less than or equal to @c high
59/// @return A reference to @c val if it's between the bounds, or to the nearest bound if @c val is outside the bounds
60template <typename T>
61constexpr auto clamp(T const& val, T const& low, T const& high) noexcept(noexcept(std::less<T>{}(val, low)))
62 -> T const& {
63 return ::arene::base::clamp(val, low, high, std::less<T>{});
64}
65// parasoft-end-suppress AUTOSAR-A7_5_1-a "Returning a reference is required to match std::clamp."
66// parasoft-end-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
67
68} // namespace base
69} // namespace arene
70#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_CLAMP_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr auto clamp(T const &val, T const &low, T const &high) noexcept(noexcept(std::less< T >{}(val, low))) -> T const &
Clamp the supplied value between two bounds using std::less for ordering.
Definition clamp.hpp:61
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10