std::ranges::max

Header: <algorithm>

Returns the greater of the given projected values.

# Declarations

Call signature
template< class T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = ranges::less >
constexpr const T&
max( const T& a, const T& b, Comp comp = {}, Proj proj = {} );

(since C++20)

template< std::copyable T, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<const T*, Proj>> Comp = ranges::less >
constexpr T
max( std::initializer_list<T> r, Comp comp = {}, Proj proj = {} );

(since C++20)

template< ranges::input_range R, class Proj = std::identity,
std::indirect_strict_weak_order<
std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less >
requires std::indirectly_copyable_storable<ranges::iterator_t<R>,
ranges::range_value_t<R>*>
constexpr ranges::range_value_t<R>
max( R&& r, Comp comp = {}, Proj proj = {} );

(since C++20)

# Parameters

# Notes

Capturing the result of std::ranges::max by reference produces a dangling reference if one of the parameters is a temporary and that parameter is returned:

# Example

#include <algorithm>
#include <iostream>
#include <string>
 
static_assert(std::ranges::max({0B10, 0X10, 010, 10}) == 16); // overload (2)
 
int main()
{
    namespace ranges = std::ranges;
    using namespace std::string_view_literals;
 
    std::cout << "larger of 1 and 9999: " << ranges::max(1, 9999) << '\n'
              << "larger of 'a', and 'b': '" << ranges::max('a', 'b') << "'\n"
              << "longest of \"foo\", \"bar\", and \"hello\": \""
              << ranges::max({"foo"sv, "bar"sv, "hello"sv}, {},
                             &std::string_view::size) << "\"\n";
}

# See also