File indexing completed on 2025-01-18 09:38:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_PLUS_HPP
0011 #define BOOST_HANA_PLUS_HPP
0012
0013 #include <boost/hana/fwd/plus.hpp>
0014
0015 #include <boost/hana/concept/constant.hpp>
0016 #include <boost/hana/concept/monoid.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/common.hpp>
0019 #include <boost/hana/core/to.hpp>
0020 #include <boost/hana/core/dispatch.hpp>
0021 #include <boost/hana/detail/canonical_constant.hpp>
0022 #include <boost/hana/detail/has_common_embedding.hpp>
0023
0024 #include <type_traits>
0025
0026
0027 namespace boost { namespace hana {
0028
0029 template <typename X, typename Y>
0030 constexpr decltype(auto) plus_t::operator()(X&& x, Y&& y) const {
0031 using T = typename hana::tag_of<X>::type;
0032 using U = typename hana::tag_of<Y>::type;
0033 using Plus = BOOST_HANA_DISPATCH_IF(decltype(plus_impl<T, U>{}),
0034 hana::Monoid<T>::value &&
0035 hana::Monoid<U>::value &&
0036 !is_default<plus_impl<T, U>>::value
0037 );
0038
0039 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0040 static_assert(hana::Monoid<T>::value,
0041 "hana::plus(x, y) requires 'x' to be a Monoid");
0042
0043 static_assert(hana::Monoid<U>::value,
0044 "hana::plus(x, y) requires 'y' to be a Monoid");
0045
0046 static_assert(!is_default<plus_impl<T, U>>::value,
0047 "hana::plus(x, y) requires 'x' and 'y' to be embeddable "
0048 "in a common Monoid");
0049 #endif
0050
0051 return Plus::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
0052 }
0053
0054
0055 template <typename T, typename U, bool condition>
0056 struct plus_impl<T, U, when<condition>> : default_ {
0057 template <typename ...Args>
0058 static constexpr auto apply(Args&& ...) = delete;
0059 };
0060
0061
0062 template <typename T, typename U>
0063 struct plus_impl<T, U, when<
0064 detail::has_nontrivial_common_embedding<Monoid, T, U>::value
0065 >> {
0066 using C = typename common<T, U>::type;
0067 template <typename X, typename Y>
0068 static constexpr decltype(auto) apply(X&& x, Y&& y) {
0069 return hana::plus(hana::to<C>(static_cast<X&&>(x)),
0070 hana::to<C>(static_cast<Y&&>(y)));
0071 }
0072 };
0073
0074
0075
0076
0077 template <typename T>
0078 struct plus_impl<T, T, when<
0079 std::is_arithmetic<T>::value &&
0080 !std::is_same<T, bool>::value
0081 >> {
0082 template <typename X, typename Y>
0083 static constexpr decltype(auto) apply(X&& x, Y&& y)
0084 { return static_cast<X&&>(x) + static_cast<Y&&>(y); }
0085 };
0086
0087
0088
0089
0090 namespace detail {
0091 template <typename C, typename X, typename Y>
0092 struct constant_from_plus {
0093 static constexpr auto value = hana::plus(hana::value<X>(), hana::value<Y>());
0094 using hana_tag = detail::CanonicalConstant<typename C::value_type>;
0095 };
0096 }
0097
0098 template <typename C>
0099 struct plus_impl<C, C, when<
0100 hana::Constant<C>::value &&
0101 Monoid<typename C::value_type>::value
0102 >> {
0103 template <typename X, typename Y>
0104 static constexpr decltype(auto) apply(X const&, Y const&)
0105 { return hana::to<C>(detail::constant_from_plus<C, X, Y>{}); }
0106 };
0107 }}
0108
0109 #endif