Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
max_element.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_MAX_ELEMENT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAX_ELEMENT_HPP_
7
8#include "arene/base/algorithm/detail/traits.hpp"
11#include "arene/base/functional/detail/operator_less.hpp"
17
18// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
19// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
20
21// IWYU pragma: private, include <algorithm>
22// IWYU pragma: friend "stdlib_detail/.*"
23
24namespace std {
25
26// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
27
28/// @brief returns the largest element in a range
29/// @tparam ForwardIterator iterator type
30/// @tparam Compare binary predicate type
31/// @param first beginning of the range of elements
32/// @param last end of the range of elements
33/// @param comp binary predicate which returns @c true if the first argument is ordered before the second
34///
35/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
36/// @pre <tt>[begin, end)</tt> must be a valid range.
37/// @pre The expression <tt>comp(e1, e2)</tt> must be convertible to @c bool
38/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the
39/// reference type of @c ForwardIterator, regardless of value category, and
40/// must not modify @c e1 or @c e2.
41///
42/// Finds the largest element, i.e. the element ordered after all other
43/// elements, in the range <tt>[first, last)</tt>.
44///
45/// @return iterator to the largest element of the range <tt>[first, last)</tt>.
46/// If several elements in the range are equivalent to the largest element,
47/// returns the iterator to the first such element. Returns @c last if the
48/// range is empty.
49///
50/// @note Complexity
51/// Given N as <tt>std::distance(first, last)</tt>, exactly <tt>max(N - 1,
52/// 0)</tt> applications of the comparator @c comp.
53///
54template <
55 class ForwardIterator,
56 class Compare,
61 nullptr>
62constexpr auto max_element(ForwardIterator first, ForwardIterator last, Compare comp) noexcept(
63 noexcept(arene::base::max_element(std::move(first), std::move(last), std::move(comp)))
64) -> ForwardIterator {
66 return arene::base::max_element(std::move(first), std::move(last), std::move(comp));
67}
68
69/// @brief returns the largest element in a range
70/// @tparam ForwardIterator iterator type
71/// @tparam Compare binary predicate type
72/// @param first beginning of the range of elements
73/// @param last end of the range of elements
74///
75/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
76/// @pre <tt>[begin, end)</tt> must be a valid range.
77/// @pre The expression <tt>e1 < e2</tt> must be convertible to @c bool
78/// for every argument @c e1, @c e2 of type @c RT, where @c RT is the
79/// reference type of @c ForwardIterator, regardless of value category, and
80/// must not modify @c e1 or @c e2.
81///
82/// Finds the largest element, i.e. the element ordered after all other
83/// elements, in the range <tt>[first, last)</tt>.
84///
85/// @return iterator to the largest element of the range <tt>[first, last)</tt>.
86/// If several elements in the range are equivalent to the largest element,
87/// returns the iterator to the first such element. Returns @c last if the
88/// range is empty.
89///
90/// @note Complexity
91/// Given N as <tt>std::distance(first, last)</tt>, exactly <tt>max(N - 1,
92/// 0)</tt> comparisons using @c operator<.
93///
94template <
95 class ForwardIterator,
99 decltype(arene::base::functional_detail::operator_less)&,
100 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
101constexpr auto max_element(ForwardIterator first, ForwardIterator last) noexcept(
102 noexcept(std::max_element(std::move(first), std::move(last), arene::base::functional_detail::operator_less))
103) -> ForwardIterator {
104 return std::max_element(std::move(first), std::move(last), arene::base::functional_detail::operator_less);
105}
106
107// parasoft-end-suppress AUTOSAR-M3_3_2-a
108
109} // namespace std
110
111#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAX_ELEMENT_HPP_
Public export header for the constraints subpackage.
constexpr bool is_compare_v
determines if a type satisfies the Compare named requirement
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
constexpr auto disable_constexpr_in_cpp14() noexcept -> void
function used to disable constexpr in C++14 but allow for it in C++17
Definition internal_disable_constexpr_in_cpp14.hpp:23
STL namespace.
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
constexpr auto max_element(ForwardIterator first, ForwardIterator last, Compare comp) noexcept(noexcept(arene::base::max_element(std::move(first), std::move(last), std::move(comp)))) -> ForwardIterator
returns the largest element in a range
Definition max_element.hpp:62