product.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*!
  2. @file
  3. Defines `boost::hana::product`.
  4. @copyright Louis Dionne 2013-2017
  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_PRODUCT_HPP
  9. #define BOOST_HANA_PRODUCT_HPP
  10. #include <boost/hana/fwd/product.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/concept/ring.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/mult.hpp>
  18. #include <boost/hana/one.hpp>
  19. BOOST_HANA_NAMESPACE_BEGIN
  20. //! @cond
  21. template <typename R>
  22. template <typename Xs>
  23. constexpr decltype(auto) product_t<R>::operator()(Xs&& xs) const {
  24. using S = typename hana::tag_of<Xs>::type;
  25. using Product = BOOST_HANA_DISPATCH_IF(product_impl<S>,
  26. hana::Foldable<S>::value
  27. );
  28. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  29. static_assert(hana::Ring<R>::value,
  30. "hana::product<R> requires 'R' to be a Ring");
  31. static_assert(hana::Foldable<S>::value,
  32. "hana::product<R>(xs) requires 'xs' to be Foldable");
  33. #endif
  34. return Product::template apply<R>(static_cast<Xs&&>(xs));
  35. }
  36. //! @endcond
  37. template <typename T, bool condition>
  38. struct product_impl<T, when<condition>> : default_ {
  39. template <typename R, typename Xs>
  40. static constexpr decltype(auto) apply(Xs&& xs) {
  41. return hana::fold_left(static_cast<Xs&&>(xs), hana::one<R>(), hana::mult);
  42. }
  43. };
  44. BOOST_HANA_NAMESPACE_END
  45. #endif // !BOOST_HANA_PRODUCT_HPP