product.hpp 1.8 KB

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