plus.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*!
  2. @file
  3. Defines `boost::hana::plus`.
  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_PLUS_HPP
  9. #define BOOST_HANA_PLUS_HPP
  10. #include <boost/hana/fwd/plus.hpp>
  11. #include <boost/hana/concept/constant.hpp>
  12. #include <boost/hana/concept/monoid.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/common.hpp>
  15. #include <boost/hana/core/to.hpp>
  16. #include <boost/hana/core/dispatch.hpp>
  17. #include <boost/hana/detail/canonical_constant.hpp>
  18. #include <boost/hana/detail/has_common_embedding.hpp>
  19. #include <type_traits>
  20. BOOST_HANA_NAMESPACE_BEGIN
  21. //! @cond
  22. template <typename X, typename Y>
  23. constexpr decltype(auto) plus_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 Plus = BOOST_HANA_DISPATCH_IF(decltype(plus_impl<T, U>{}),
  27. hana::Monoid<T>::value &&
  28. hana::Monoid<U>::value &&
  29. !is_default<plus_impl<T, U>>::value
  30. );
  31. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  32. static_assert(hana::Monoid<T>::value,
  33. "hana::plus(x, y) requires 'x' to be a Monoid");
  34. static_assert(hana::Monoid<U>::value,
  35. "hana::plus(x, y) requires 'y' to be a Monoid");
  36. static_assert(!is_default<plus_impl<T, U>>::value,
  37. "hana::plus(x, y) requires 'x' and 'y' to be embeddable "
  38. "in a common Monoid");
  39. #endif
  40. return Plus::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  41. }
  42. //! @endcond
  43. template <typename T, typename U, bool condition>
  44. struct plus_impl<T, U, when<condition>> : default_ {
  45. template <typename ...Args>
  46. static constexpr auto apply(Args&& ...) = delete;
  47. };
  48. // Cross-type overload
  49. template <typename T, typename U>
  50. struct plus_impl<T, U, when<
  51. detail::has_nontrivial_common_embedding<Monoid, T, U>::value
  52. >> {
  53. using C = typename common<T, U>::type;
  54. template <typename X, typename Y>
  55. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  56. return hana::plus(hana::to<C>(static_cast<X&&>(x)),
  57. hana::to<C>(static_cast<Y&&>(y)));
  58. }
  59. };
  60. //////////////////////////////////////////////////////////////////////////
  61. // Model for non-boolean arithmetic data types
  62. //////////////////////////////////////////////////////////////////////////
  63. template <typename T>
  64. struct plus_impl<T, T, when<
  65. std::is_arithmetic<T>::value &&
  66. !std::is_same<T, bool>::value
  67. >> {
  68. template <typename X, typename Y>
  69. static constexpr decltype(auto) apply(X&& x, Y&& y)
  70. { return static_cast<X&&>(x) + static_cast<Y&&>(y); }
  71. };
  72. //////////////////////////////////////////////////////////////////////////
  73. // Model for Constants over a Monoid
  74. //////////////////////////////////////////////////////////////////////////
  75. namespace detail {
  76. template <typename C, typename X, typename Y>
  77. struct constant_from_plus {
  78. static constexpr auto value = hana::plus(hana::value<X>(), hana::value<Y>());
  79. using hana_tag = detail::CanonicalConstant<typename C::value_type>;
  80. };
  81. }
  82. template <typename C>
  83. struct plus_impl<C, C, when<
  84. hana::Constant<C>::value &&
  85. Monoid<typename C::value_type>::value
  86. >> {
  87. template <typename X, typename Y>
  88. static constexpr decltype(auto) apply(X const&, Y const&)
  89. { return hana::to<C>(detail::constant_from_plus<C, X, Y>{}); }
  90. };
  91. BOOST_HANA_NAMESPACE_END
  92. #endif // !BOOST_HANA_PLUS_HPP