File indexing completed on 2025-01-30 09:43:38
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_MULT_HPP
0011 #define BOOST_HANA_MULT_HPP
0012
0013 #include <boost/hana/fwd/mult.hpp>
0014
0015 #include <boost/hana/concept/constant.hpp>
0016 #include <boost/hana/concept/ring.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/to.hpp>
0019 #include <boost/hana/core/dispatch.hpp>
0020 #include <boost/hana/detail/canonical_constant.hpp>
0021 #include <boost/hana/detail/has_common_embedding.hpp>
0022 #include <boost/hana/value.hpp>
0023
0024 #include <type_traits>
0025
0026
0027 namespace boost { namespace hana {
0028
0029 template <typename X, typename Y>
0030 constexpr decltype(auto) mult_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 Mult = BOOST_HANA_DISPATCH_IF(decltype(mult_impl<T, U>{}),
0034 hana::Ring<T>::value &&
0035 hana::Ring<U>::value &&
0036 !is_default<mult_impl<T, U>>::value
0037 );
0038
0039 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0040 static_assert(hana::Ring<T>::value,
0041 "hana::mult(x, y) requires 'x' to be in a Ring");
0042
0043 static_assert(hana::Ring<U>::value,
0044 "hana::mult(x, y) requires 'y' to be in a Ring");
0045
0046 static_assert(!is_default<mult_impl<T, U>>::value,
0047 "hana::mult(x, y) requires 'x' and 'y' to be embeddable "
0048 "in a common Ring");
0049 #endif
0050
0051 return Mult::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
0052 }
0053
0054
0055 template <typename T, typename U, bool condition>
0056 struct mult_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 mult_impl<T, U, when<
0064 detail::has_nontrivial_common_embedding<Ring, 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::mult(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 mult_impl<T, T, when<std::is_arithmetic<T>::value &&
0079 !std::is_same<bool, T>::value>> {
0080 template <typename X, typename Y>
0081 static constexpr decltype(auto) apply(X&& x, Y&& y)
0082 { return static_cast<X&&>(x) * static_cast<Y&&>(y); }
0083 };
0084
0085
0086
0087
0088 namespace detail {
0089 template <typename C, typename X, typename Y>
0090 struct constant_from_mult {
0091 static constexpr auto value = hana::mult(hana::value<X>(), hana::value<Y>());
0092 using hana_tag = detail::CanonicalConstant<typename C::value_type>;
0093 };
0094 }
0095
0096 template <typename C>
0097 struct mult_impl<C, C, when<
0098 hana::Constant<C>::value &&
0099 Ring<typename C::value_type>::value
0100 >> {
0101 template <typename X, typename Y>
0102 static constexpr decltype(auto) apply(X const&, Y const&)
0103 { return hana::to<C>(detail::constant_from_mult<C, X, Y>{}); }
0104 };
0105 }}
0106
0107 #endif