max.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*!
  2. @file
  3. Defines `boost::hana::max`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_MAX_HPP
  9. #define BOOST_HANA_MAX_HPP
  10. #include <boost/hana/fwd/max.hpp>
  11. #include <boost/hana/concept/orderable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/if.hpp>
  15. #include <boost/hana/less.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename X, typename Y>
  19. constexpr decltype(auto) max_t::operator()(X&& x, Y&& y) const {
  20. using T = typename hana::tag_of<X>::type;
  21. using U = typename hana::tag_of<Y>::type;
  22. using Max = BOOST_HANA_DISPATCH_IF(decltype(max_impl<T, U>{}),
  23. hana::Orderable<T>::value &&
  24. hana::Orderable<U>::value
  25. );
  26. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  27. static_assert(hana::Orderable<T>::value,
  28. "hana::max(x, y) requires 'x' to be Orderable");
  29. static_assert(hana::Orderable<U>::value,
  30. "hana::max(x, y) requires 'y' to be Orderable");
  31. #endif
  32. return Max::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  33. }
  34. //! @endcond
  35. template <typename T, typename U, bool condition>
  36. struct max_impl<T, U, when<condition>> : default_ {
  37. template <typename X, typename Y>
  38. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  39. decltype(auto) cond = hana::less(x, y);
  40. return hana::if_(static_cast<decltype(cond)&&>(cond),
  41. static_cast<Y&&>(y),
  42. static_cast<X&&>(x)
  43. );
  44. }
  45. };
  46. BOOST_HANA_NAMESPACE_END
  47. #endif // !BOOST_HANA_MAX_HPP