sum.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*!
  2. @file
  3. Defines `boost::hana::sum`.
  4. Copyright Louis Dionne 2013-2022
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_SUM_HPP
  9. #define BOOST_HANA_SUM_HPP
  10. #include <boost/hana/fwd/sum.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/concept/monoid.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/fold_left.hpp>
  16. #include <boost/hana/integral_constant.hpp> // required by fwd decl
  17. #include <boost/hana/plus.hpp>
  18. #include <boost/hana/zero.hpp>
  19. namespace boost { namespace hana {
  20. //! @cond
  21. template <typename M>
  22. template <typename Xs>
  23. constexpr decltype(auto) sum_t<M>::operator()(Xs&& xs) const {
  24. using S = typename hana::tag_of<Xs>::type;
  25. using Sum = BOOST_HANA_DISPATCH_IF(sum_impl<S>,
  26. hana::Foldable<S>::value
  27. );
  28. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  29. static_assert(hana::Monoid<M>::value,
  30. "hana::sum<M> requires 'M' to be a Monoid");
  31. static_assert(hana::Foldable<S>::value,
  32. "hana::sum<M>(xs) requires 'xs' to be Foldable");
  33. #endif
  34. return Sum::template apply<M>(static_cast<Xs&&>(xs));
  35. }
  36. //! @endcond
  37. template <typename T, bool condition>
  38. struct sum_impl<T, when<condition>> : default_ {
  39. template <typename M, typename Xs>
  40. static constexpr decltype(auto) apply(Xs&& xs) {
  41. return hana::fold_left(static_cast<Xs&&>(xs), hana::zero<M>(), hana::plus);
  42. }
  43. };
  44. }} // end namespace boost::hana
  45. #endif // !BOOST_HANA_SUM_HPP