3 Defines `boost::hana::maximum`.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_MAXIMUM_HPP
11 #define BOOST_HANA_MAXIMUM_HPP
13 #include <boost/hana/fwd/maximum.hpp>
15 #include <boost/hana/concept/foldable.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/dispatch.hpp>
18 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
19 #include <boost/hana/fold_left.hpp>
20 #include <boost/hana/if.hpp>
21 #include <boost/hana/less.hpp>
24 namespace boost { namespace hana {
26 template <typename Xs>
27 constexpr decltype(auto) maximum_t::operator()(Xs&& xs) const {
28 using S = typename hana::tag_of<Xs>::type;
29 using Maximum = BOOST_HANA_DISPATCH_IF(maximum_impl<S>,
30 hana::Foldable<S>::value
33 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
34 static_assert(hana::Foldable<S>::value,
35 "hana::maximum(xs) requires 'xs' to be Foldable");
38 return Maximum::apply(static_cast<Xs&&>(xs));
41 template <typename Xs, typename Predicate>
42 constexpr decltype(auto) maximum_t::operator()(Xs&& xs, Predicate&& pred) const {
43 using S = typename hana::tag_of<Xs>::type;
44 using Maximum = BOOST_HANA_DISPATCH_IF(maximum_pred_impl<S>,
45 hana::Foldable<S>::value
48 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
49 static_assert(hana::Foldable<S>::value,
50 "hana::maximum(xs, predicate) requires 'xs' to be Foldable");
53 return Maximum::apply(static_cast<Xs&&>(xs),
54 static_cast<Predicate&&>(pred));
58 //////////////////////////////////////////////////////////////////////////
59 // maximum (with a custom predicate)
60 //////////////////////////////////////////////////////////////////////////
62 template <typename Pred>
66 template <typename X, typename Y>
67 constexpr decltype(auto) operator()(X&& x, Y&& y) const {
68 auto result = (*pred)(x, y);
69 return hana::if_(result, static_cast<Y&&>(y),
75 template <typename T, bool condition>
76 struct maximum_pred_impl<T, when<condition>> : default_ {
77 template <typename Xs, typename Pred>
78 static constexpr decltype(auto) apply(Xs&& xs, Pred&& pred) {
79 // We use a pointer instead of a reference to avoid a Clang ICE.
80 return hana::fold_left(static_cast<Xs&&>(xs),
81 detail::max_by<decltype(&pred)>{&pred}
86 //////////////////////////////////////////////////////////////////////////
87 // maximum (without a custom predicate)
88 //////////////////////////////////////////////////////////////////////////
89 template <typename T, bool condition>
90 struct maximum_impl<T, when<condition>> : default_ {
91 template <typename Xs>
92 static constexpr decltype(auto) apply(Xs&& xs)
93 { return hana::maximum(static_cast<Xs&&>(xs), hana::less); }
95 }} // end namespace boost::hana
97 #endif // !BOOST_HANA_MAXIMUM_HPP