File indexing completed on 2025-01-30 09:43:40
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_SUM_HPP
0011 #define BOOST_HANA_SUM_HPP
0012
0013 #include <boost/hana/fwd/sum.hpp>
0014
0015 #include <boost/hana/concept/foldable.hpp>
0016 #include <boost/hana/concept/monoid.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/dispatch.hpp>
0019 #include <boost/hana/fold_left.hpp>
0020 #include <boost/hana/integral_constant.hpp> // required by fwd decl
0021 #include <boost/hana/plus.hpp>
0022 #include <boost/hana/zero.hpp>
0023
0024
0025 namespace boost { namespace hana {
0026
0027 template <typename M>
0028 template <typename Xs>
0029 constexpr decltype(auto) sum_t<M>::operator()(Xs&& xs) const {
0030 using S = typename hana::tag_of<Xs>::type;
0031 using Sum = BOOST_HANA_DISPATCH_IF(sum_impl<S>,
0032 hana::Foldable<S>::value
0033 );
0034
0035 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0036 static_assert(hana::Monoid<M>::value,
0037 "hana::sum<M> requires 'M' to be a Monoid");
0038
0039 static_assert(hana::Foldable<S>::value,
0040 "hana::sum<M>(xs) requires 'xs' to be Foldable");
0041 #endif
0042
0043 return Sum::template apply<M>(static_cast<Xs&&>(xs));
0044 }
0045
0046
0047 template <typename T, bool condition>
0048 struct sum_impl<T, when<condition>> : default_ {
0049 template <typename M, typename Xs>
0050 static constexpr decltype(auto) apply(Xs&& xs) {
0051 return hana::fold_left(static_cast<Xs&&>(xs), hana::zero<M>(), hana::plus);
0052 }
0053 };
0054 }}
0055
0056 #endif