greater.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*!
  2. @file
  3. Defines `boost::hana::greater`.
  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_GREATER_HPP
  9. #define BOOST_HANA_GREATER_HPP
  10. #include <boost/hana/fwd/greater.hpp>
  11. #include <boost/hana/concept/orderable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/common.hpp>
  14. #include <boost/hana/core/to.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/detail/concepts.hpp>
  17. #include <boost/hana/detail/has_common_embedding.hpp>
  18. #include <boost/hana/detail/nested_than.hpp> // required by fwd decl
  19. #include <boost/hana/if.hpp>
  20. BOOST_HANA_NAMESPACE_BEGIN
  21. //! @cond
  22. template <typename X, typename Y>
  23. constexpr decltype(auto) greater_t::operator()(X&& x, Y&& y) const {
  24. using T = typename hana::tag_of<X>::type;
  25. using U = typename hana::tag_of<Y>::type;
  26. using Greater = BOOST_HANA_DISPATCH_IF(decltype(greater_impl<T, U>{}),
  27. hana::Orderable<T>::value &&
  28. hana::Orderable<U>::value
  29. );
  30. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  31. static_assert(hana::Orderable<T>::value,
  32. "hana::greater(x, y) requires 'x' to be Orderable");
  33. static_assert(hana::Orderable<U>::value,
  34. "hana::greater(x, y) requires 'y' to be Orderable");
  35. #endif
  36. return Greater::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  37. }
  38. //! @endcond
  39. template <typename T, typename U, bool condition>
  40. struct greater_impl<T, U, when<condition>> : default_ {
  41. template <typename X, typename Y>
  42. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  43. return hana::less(static_cast<Y&&>(y),
  44. static_cast<X&&>(x));
  45. }
  46. };
  47. // Cross-type overload
  48. template <typename T, typename U>
  49. struct greater_impl<T, U, when<
  50. detail::has_nontrivial_common_embedding<Orderable, T, U>::value
  51. >> {
  52. using C = typename hana::common<T, U>::type;
  53. template <typename X, typename Y>
  54. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  55. return hana::greater(hana::to<C>(static_cast<X&&>(x)),
  56. hana::to<C>(static_cast<Y&&>(y)));
  57. }
  58. };
  59. BOOST_HANA_NAMESPACE_END
  60. #endif // !BOOST_HANA_GREATER_HPP