product.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*!
  2. @file
  3. Forward declares `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_FWD_PRODUCT_HPP
  9. #define BOOST_HANA_FWD_PRODUCT_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. #include <boost/hana/fwd/integral_constant.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Compute the product of the numbers of a structure.
  15. //! @ingroup group-Foldable
  16. //!
  17. //! More generally, `product` will take any foldable structure containing
  18. //! objects forming a Ring and reduce them using the Ring's binary
  19. //! operation. The initial state for folding is the identity of the
  20. //! Ring's operation. It is sometimes necessary to specify the Ring to
  21. //! use; this is possible by using `product<R>`. If no Ring is specified,
  22. //! the structure will use the Ring formed by the elements it contains
  23. //! (if it knows it), or `integral_constant_tag<int>` otherwise.
  24. //! Hence,
  25. //! @code
  26. //! product<R>(xs) = fold_left(xs, one<R or inferred Ring>(), mult)
  27. //! product<> = product<integral_constant_tag<int>>
  28. //! @endcode
  29. //!
  30. //! For numbers, this will just compute the product of the numbers in the
  31. //! `xs` structure.
  32. //!
  33. //! @note
  34. //! The elements of the structure are not actually required to be in the
  35. //! same Ring, but it must be possible to perform `mult` on any two
  36. //! adjacent elements of the structure, which requires each pair of
  37. //! adjacent element to at least have a common Ring embedding. The
  38. //! meaning of "adjacent" as used here is that two elements of the
  39. //! structure `x` and `y` are adjacent if and only if they are adjacent
  40. //! in the linearization of that structure, as documented by the Iterable
  41. //! concept.
  42. //!
  43. //! @note
  44. //! See the documentation for `sum` to understand why the Ring must
  45. //! sometimes be specified explicitly.
  46. //!
  47. //!
  48. //! Example
  49. //! -------
  50. //! @include example/product.cpp
  51. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  52. constexpr auto product = see documentation;
  53. #else
  54. template <typename T, typename = void>
  55. struct product_impl : product_impl<T, when<true>> { };
  56. template <typename R>
  57. struct product_t {
  58. template <typename Xs>
  59. constexpr decltype(auto) operator()(Xs&& xs) const;
  60. };
  61. template <typename R = integral_constant_tag<int>>
  62. constexpr product_t<R> product{};
  63. #endif
  64. BOOST_HANA_NAMESPACE_END
  65. #endif // !BOOST_HANA_FWD_PRODUCT_HPP