Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:53:05

0001 /*!
0002 @file
0003 Forward declares `boost::hana::sum`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_FWD_SUM_HPP
0011 #define BOOST_HANA_FWD_SUM_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/core/when.hpp>
0015 #include <boost/hana/fwd/integral_constant.hpp>
0016 
0017 
0018 namespace boost { namespace hana {
0019     //! Compute the sum of the numbers of a structure.
0020     //! @ingroup group-Foldable
0021     //!
0022     //! More generally, `sum` will take any foldable structure containing
0023     //! objects forming a Monoid and reduce them using the Monoid's binary
0024     //! operation. The initial state for folding is the identity of the
0025     //! Monoid. It is sometimes necessary to specify the Monoid to use;
0026     //! this is possible by using `sum<M>`. If no Monoid is specified,
0027     //! the structure will use the Monoid formed by the elements it contains
0028     //! (if it knows it), or `integral_constant_tag<int>` otherwise. Hence,
0029     //! @code
0030     //!     sum<M>(xs) = fold_left(xs, zero<M or inferred Monoid>(), plus)
0031     //!     sum<> = sum<integral_constant_tag<int>>
0032     //! @endcode
0033     //!
0034     //! For numbers, this will just compute the sum of the numbers in the
0035     //! `xs` structure.
0036     //!
0037     //!
0038     //! @note
0039     //! The elements of the structure are not actually required to be in the
0040     //! same Monoid, but it must be possible to perform `plus` on any two
0041     //! adjacent elements of the structure, which requires each pair of
0042     //! adjacent element to at least have a common Monoid embedding. The
0043     //! meaning of "adjacent" as used here is that two elements of the
0044     //! structure `x` and `y` are adjacent if and only if they are adjacent
0045     //! in the linearization of that structure, as documented by the Iterable
0046     //! concept.
0047     //!
0048     //!
0049     //! Why must we sometimes specify the `Monoid` by using `sum<M>`?
0050     //! -------------------------------------------------------------
0051     //! This is because sequence tags like `tuple_tag` are not parameterized
0052     //! (by design). Hence, we do not know what kind of objects are in the
0053     //! sequence, so we can't know a `0` value of which type should be
0054     //! returned when the sequence is empty. Therefore, the type of the
0055     //! `0` to return in the empty case must be specified explicitly. Other
0056     //! foldable structures like `hana::range`s will ignore the suggested
0057     //! Monoid because they know the tag of the objects they contain. This
0058     //! inconsistent behavior is a limitation of the current design with
0059     //! non-parameterized tags, but we have no good solution for now.
0060     //!
0061     //!
0062     //! Example
0063     //! -------
0064     //! @include example/sum.cpp
0065 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0066     constexpr auto sum = see documentation;
0067 #else
0068     template <typename T, typename = void>
0069     struct sum_impl : sum_impl<T, when<true>> { };
0070 
0071     template <typename M>
0072     struct sum_t {
0073         template <typename Xs>
0074         constexpr decltype(auto) operator()(Xs&& xs) const;
0075     };
0076 
0077     template <typename M = integral_constant_tag<int>>
0078     BOOST_HANA_INLINE_VARIABLE constexpr sum_t<M> sum{};
0079 #endif
0080 }} // end namespace boost::hana
0081 
0082 #endif // !BOOST_HANA_FWD_SUM_HPP