Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
min_value_overload.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#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MIN_VALUE_OVERLOAD_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MIN_VALUE_OVERLOAD_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
9#include "stdlib/include/stdlib_detail/declval.hpp"
10// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
11
12// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
13// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
14
15// IWYU pragma: private, include <algorithm>
16// IWYU pragma: friend "stdlib_detail/.*"
17
18namespace std {
19
20// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
21// parasoft-begin-suppress AUTOSAR-M17_0_3-a "This is an implementation of the standard library function"
22// parasoft-begin-suppress AUTOSAR-M4_5_1-a "False positive: Arguments are only used with the conditional operator"
23// parasoft-begin-suppress CERT_C-EXP46-b "False positive: Arguments are only used with the conditional operator"
24/// @brief Returns the smaller of the given values.
25/// @tparam T The type of the values
26/// @param lhs The first value to compare
27/// @param rhs The second value to compare
28/// @return Returns the smaller of @c lhs and @c rhs
29///
30/// @note If T is not @c LessThanComparable, the behavior is undefined
31template <class T>
32constexpr auto min(T const& lhs, T const& rhs) noexcept(noexcept(rhs < lhs)) -> T const& {
33 return rhs < lhs ? rhs : lhs;
34}
35// parasoft-end-suppress CERT_C-EXP46-b
36// parasoft-end-suppress AUTOSAR-M4_5_1-a
37
38/// @brief Returns the smaller of the given values.
39/// @tparam T The type of the values
40/// @tparam Compare The type of the comparison function
41/// @param lhs The first value to compare
42/// @param rhs The second value to compare
43/// @param comp The comparison function which returns true if the first parameter is less than the second
44/// @return Returns the smaller of @c lhs and @c rhs using the comparison function @c comp to compare the values
45template <class T, class Compare>
46constexpr auto min(T const& lhs, T const& rhs, Compare comp) noexcept(
47 noexcept(std::declval<Compare&>()(std::declval<T&>(), std::declval<T&>()))
48) -> T const& {
49 // The callable comp checks if the first argument is less than the second. As lhs should be returned if the two
50 // inputs are equal, the comparison of comp(rhs, lhs) is used to check if rhs should be returned.
51 // NOLINTNEXTLINE(readability-suspicious-call-argument)
52 return comp(rhs, lhs) ? rhs : lhs;
53}
54
55// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
56// parasoft-end-suppress AUTOSAR-M17_0_3-a
57
58} // namespace std
59
60#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MIN_VALUE_OVERLOAD_HPP_
constexpr auto min(T const &lhs, T const &rhs, Compare comp) noexcept(noexcept(std::declval< Compare & >()(std::declval< T & >(), std::declval< T & >()))) -> T const &
Returns the smaller of the given values.
Definition min_value_overload.hpp:46
constexpr auto min(T const &lhs, T const &rhs) noexcept(noexcept(rhs< lhs)) -> T const &
Returns the smaller of the given values.
Definition min_value_overload.hpp:32
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827