3 Defines `boost::hana::plus`.
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_PLUS_HPP
11 #define BOOST_HANA_PLUS_HPP
13 #include <boost/hana/fwd/plus.hpp>
15 #include <boost/hana/concept/constant.hpp>
16 #include <boost/hana/concept/monoid.hpp>
17 #include <boost/hana/config.hpp>
18 #include <boost/hana/core/common.hpp>
19 #include <boost/hana/core/to.hpp>
20 #include <boost/hana/core/dispatch.hpp>
21 #include <boost/hana/detail/canonical_constant.hpp>
22 #include <boost/hana/detail/has_common_embedding.hpp>
24 #include <type_traits>
27 BOOST_HANA_NAMESPACE_BEGIN
29 template <typename X, typename Y>
30 constexpr decltype(auto) plus_t::operator()(X&& x, Y&& y) const {
31 using T = typename hana::tag_of<X>::type;
32 using U = typename hana::tag_of<Y>::type;
33 using Plus = BOOST_HANA_DISPATCH_IF(decltype(plus_impl<T, U>{}),
34 hana::Monoid<T>::value &&
35 hana::Monoid<U>::value &&
36 !is_default<plus_impl<T, U>>::value
39 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
40 static_assert(hana::Monoid<T>::value,
41 "hana::plus(x, y) requires 'x' to be a Monoid");
43 static_assert(hana::Monoid<U>::value,
44 "hana::plus(x, y) requires 'y' to be a Monoid");
46 static_assert(!is_default<plus_impl<T, U>>::value,
47 "hana::plus(x, y) requires 'x' and 'y' to be embeddable "
48 "in a common Monoid");
51 return Plus::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
55 template <typename T, typename U, bool condition>
56 struct plus_impl<T, U, when<condition>> : default_ {
57 template <typename ...Args>
58 static constexpr auto apply(Args&& ...) = delete;
61 // Cross-type overload
62 template <typename T, typename U>
63 struct plus_impl<T, U, when<
64 detail::has_nontrivial_common_embedding<Monoid, T, U>::value
66 using C = typename common<T, U>::type;
67 template <typename X, typename Y>
68 static constexpr decltype(auto) apply(X&& x, Y&& y) {
69 return hana::plus(hana::to<C>(static_cast<X&&>(x)),
70 hana::to<C>(static_cast<Y&&>(y)));
74 //////////////////////////////////////////////////////////////////////////
75 // Model for non-boolean arithmetic data types
76 //////////////////////////////////////////////////////////////////////////
78 struct plus_impl<T, T, when<
79 std::is_arithmetic<T>::value &&
80 !std::is_same<T, bool>::value
82 template <typename X, typename Y>
83 static constexpr decltype(auto) apply(X&& x, Y&& y)
84 { return static_cast<X&&>(x) + static_cast<Y&&>(y); }
87 //////////////////////////////////////////////////////////////////////////
88 // Model for Constants over a Monoid
89 //////////////////////////////////////////////////////////////////////////
91 template <typename C, typename X, typename Y>
92 struct constant_from_plus {
93 static constexpr auto value = hana::plus(hana::value<X>(), hana::value<Y>());
94 using hana_tag = detail::CanonicalConstant<typename C::value_type>;
99 struct plus_impl<C, C, when<
100 hana::Constant<C>::value &&
101 Monoid<typename C::value_type>::value
103 template <typename X, typename Y>
104 static constexpr decltype(auto) apply(X const&, Y const&)
105 { return hana::to<C>(detail::constant_from_plus<C, X, Y>{}); }
107 BOOST_HANA_NAMESPACE_END
109 #endif // !BOOST_HANA_PLUS_HPP