1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_MAX_ELEMENT_H
10 #define _LIBCPP___ALGORITHM_MAX_ELEMENT_H
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__type_traits/is_callable.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
22 _LIBCPP_BEGIN_NAMESPACE_STD
24 template <class _Compare
, class _ForwardIterator
>
25 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
26 __max_element(_ForwardIterator __first
, _ForwardIterator __last
, _Compare __comp
) {
28 __has_forward_iterator_category
<_ForwardIterator
>::value
, "std::max_element requires a ForwardIterator");
29 if (__first
!= __last
) {
30 _ForwardIterator __i
= __first
;
31 while (++__i
!= __last
)
32 if (__comp(*__first
, *__i
))
38 template <class _ForwardIterator
, class _Compare
>
39 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
40 max_element(_ForwardIterator __first
, _ForwardIterator __last
, _Compare __comp
) {
42 __is_callable
<_Compare
&, decltype(*__first
), decltype(*__first
)>::value
, "The comparator has to be callable");
43 return std::__max_element
<__comp_ref_type
<_Compare
> >(__first
, __last
, __comp
);
46 template <class _ForwardIterator
>
47 [[__nodiscard__
]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
48 max_element(_ForwardIterator __first
, _ForwardIterator __last
) {
49 return std::max_element(__first
, __last
, __less
<>());
52 _LIBCPP_END_NAMESPACE_STD
54 #endif // _LIBCPP___ALGORITHM_MAX_ELEMENT_H