1 /*!
2 @file
3 Defines `boost::hana::product`.
4 
5 @copyright Louis Dionne 2013-2016
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_PRODUCT_HPP
11 #define BOOST_HANA_PRODUCT_HPP
12 
13 #include <boost/hana/fwd/product.hpp>
14 
15 #include <boost/hana/concept/foldable.hpp>
16 #include <boost/hana/concept/ring.hpp>
17 #include <boost/hana/config.hpp>
18 #include <boost/hana/core/dispatch.hpp>
19 #include <boost/hana/fold_left.hpp>
20 #include <boost/hana/integral_constant.hpp> // required by fwd decl
21 #include <boost/hana/mult.hpp>
22 #include <boost/hana/one.hpp>
23 
24 
25 BOOST_HANA_NAMESPACE_BEGIN
26     template <typename R>
27     struct product_t {
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     #endif
32 
33         template <typename Xs>
operator ()product_t34         constexpr decltype(auto) operator()(Xs&& xs) const {
35             using S = typename hana::tag_of<Xs>::type;
36             using Product = BOOST_HANA_DISPATCH_IF(product_impl<S>,
37                 hana::Foldable<S>::value
38             );
39 
40         #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
41             static_assert(hana::Foldable<S>::value,
42             "hana::product<R>(xs) requires 'xs' to be Foldable");
43         #endif
44 
45             return Product::template apply<R>(static_cast<Xs&&>(xs));
46         }
47     };
48 
49     template <typename T, bool condition>
50     struct product_impl<T, when<condition>> : default_ {
51         template <typename R, typename Xs>
applyproduct_impl52         static constexpr decltype(auto) apply(Xs&& xs) {
53             return hana::fold_left(static_cast<Xs&&>(xs), hana::one<R>(), hana::mult);
54         }
55     };
56 BOOST_HANA_NAMESPACE_END
57 
58 #endif // !BOOST_HANA_PRODUCT_HPP
59