]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::sum`. | |
4 | ||
b32b8144 | 5 | @copyright Louis Dionne 2013-2017 |
7c673cae FG |
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) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_SUM_HPP | |
11 | #define BOOST_HANA_SUM_HPP | |
12 | ||
13 | #include <boost/hana/fwd/sum.hpp> | |
14 | ||
15 | #include <boost/hana/concept/foldable.hpp> | |
16 | #include <boost/hana/concept/monoid.hpp> | |
17 | #include <boost/hana/config.hpp> | |
18 | #include <boost/hana/core/dispatch.hpp> | |
19 | #include <boost/hana/fold_left.hpp> | |
20 | #include <boost/hana/integral_constant.hpp> // required by fwd decl | |
21 | #include <boost/hana/plus.hpp> | |
22 | #include <boost/hana/zero.hpp> | |
23 | ||
24 | ||
1e59de90 | 25 | namespace boost { namespace hana { |
92f5a8d4 | 26 | //! @cond |
7c673cae | 27 | template <typename M> |
92f5a8d4 TL |
28 | template <typename Xs> |
29 | constexpr decltype(auto) sum_t<M>::operator()(Xs&& xs) const { | |
30 | using S = typename hana::tag_of<Xs>::type; | |
31 | using Sum = BOOST_HANA_DISPATCH_IF(sum_impl<S>, | |
32 | hana::Foldable<S>::value | |
33 | ); | |
34 | ||
7c673cae FG |
35 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
36 | static_assert(hana::Monoid<M>::value, | |
37 | "hana::sum<M> requires 'M' to be a Monoid"); | |
7c673cae | 38 | |
92f5a8d4 TL |
39 | static_assert(hana::Foldable<S>::value, |
40 | "hana::sum<M>(xs) requires 'xs' to be Foldable"); | |
41 | #endif | |
7c673cae | 42 | |
92f5a8d4 TL |
43 | return Sum::template apply<M>(static_cast<Xs&&>(xs)); |
44 | } | |
45 | //! @endcond | |
7c673cae FG |
46 | |
47 | template <typename T, bool condition> | |
48 | struct sum_impl<T, when<condition>> : default_ { | |
49 | template <typename M, typename Xs> | |
50 | static constexpr decltype(auto) apply(Xs&& xs) { | |
51 | return hana::fold_left(static_cast<Xs&&>(xs), hana::zero<M>(), hana::plus); | |
52 | } | |
53 | }; | |
1e59de90 | 54 | }} // end namespace boost::hana |
7c673cae FG |
55 | |
56 | #endif // !BOOST_HANA_SUM_HPP |