| 1 | /*! |
| 2 | @file |
| 3 | Forward declares `boost::hana::sum`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 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_FWD_SUM_HPP |
| 11 | #define BOOST_HANA_FWD_SUM_HPP |
| 12 | |
| 13 | #include <boost/hana/config.hpp> |
| 14 | #include <boost/hana/core/when.hpp> |
| 15 | #include <boost/hana/fwd/integral_constant.hpp> |
| 16 | |
| 17 | |
| 18 | namespace boost { namespace hana { |
| 19 | //! Compute the sum of the numbers of a structure. |
| 20 | //! @ingroup group-Foldable |
| 21 | //! |
| 22 | //! More generally, `sum` will take any foldable structure containing |
| 23 | //! objects forming a Monoid and reduce them using the Monoid's binary |
| 24 | //! operation. The initial state for folding is the identity of the |
| 25 | //! Monoid. It is sometimes necessary to specify the Monoid to use; |
| 26 | //! this is possible by using `sum<M>`. If no Monoid is specified, |
| 27 | //! the structure will use the Monoid formed by the elements it contains |
| 28 | //! (if it knows it), or `integral_constant_tag<int>` otherwise. Hence, |
| 29 | //! @code |
| 30 | //! sum<M>(xs) = fold_left(xs, zero<M or inferred Monoid>(), plus) |
| 31 | //! sum<> = sum<integral_constant_tag<int>> |
| 32 | //! @endcode |
| 33 | //! |
| 34 | //! For numbers, this will just compute the sum of the numbers in the |
| 35 | //! `xs` structure. |
| 36 | //! |
| 37 | //! |
| 38 | //! @note |
| 39 | //! The elements of the structure are not actually required to be in the |
| 40 | //! same Monoid, but it must be possible to perform `plus` on any two |
| 41 | //! adjacent elements of the structure, which requires each pair of |
| 42 | //! adjacent element to at least have a common Monoid embedding. The |
| 43 | //! meaning of "adjacent" as used here is that two elements of the |
| 44 | //! structure `x` and `y` are adjacent if and only if they are adjacent |
| 45 | //! in the linearization of that structure, as documented by the Iterable |
| 46 | //! concept. |
| 47 | //! |
| 48 | //! |
| 49 | //! Why must we sometimes specify the `Monoid` by using `sum<M>`? |
| 50 | //! ------------------------------------------------------------- |
| 51 | //! This is because sequence tags like `tuple_tag` are not parameterized |
| 52 | //! (by design). Hence, we do not know what kind of objects are in the |
| 53 | //! sequence, so we can't know a `0` value of which type should be |
| 54 | //! returned when the sequence is empty. Therefore, the type of the |
| 55 | //! `0` to return in the empty case must be specified explicitly. Other |
| 56 | //! foldable structures like `hana::range`s will ignore the suggested |
| 57 | //! Monoid because they know the tag of the objects they contain. This |
| 58 | //! inconsistent behavior is a limitation of the current design with |
| 59 | //! non-parameterized tags, but we have no good solution for now. |
| 60 | //! |
| 61 | //! |
| 62 | //! Example |
| 63 | //! ------- |
| 64 | //! @include example/sum.cpp |
| 65 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 66 | constexpr auto sum = see documentation; |
| 67 | #else |
| 68 | template <typename T, typename = void> |
| 69 | struct sum_impl : sum_impl<T, when<true>> { }; |
| 70 | |
| 71 | template <typename M> |
| 72 | struct sum_t { |
| 73 | template <typename Xs> |
| 74 | constexpr decltype(auto) operator()(Xs&& xs) const; |
| 75 | }; |
| 76 | |
| 77 | template <typename M = integral_constant_tag<int>> |
| 78 | BOOST_HANA_INLINE_VARIABLE constexpr sum_t<M> sum{}; |
| 79 | #endif |
| 80 | }} // end namespace boost::hana |
| 81 | |
| 82 | #endif // !BOOST_HANA_FWD_SUM_HPP |
| 83 | |