power.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*!
  2. @file
  3. Defines `boost::hana::power`.
  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_POWER_HPP
  9. #define BOOST_HANA_POWER_HPP
  10. #include <boost/hana/fwd/power.hpp>
  11. #include <boost/hana/concept/integral_constant.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/functional/iterate.hpp>
  16. #include <boost/hana/functional/partial.hpp>
  17. #include <boost/hana/mult.hpp>
  18. #include <boost/hana/one.hpp>
  19. #include <cstddef>
  20. BOOST_HANA_NAMESPACE_BEGIN
  21. //! @cond
  22. template <typename X, typename N>
  23. constexpr decltype(auto) power_t::operator()(X&& x, N const& n) const {
  24. using R = typename hana::tag_of<X>::type;
  25. using Power = BOOST_HANA_DISPATCH_IF(power_impl<R>,
  26. hana::Ring<R>::value &&
  27. hana::IntegralConstant<N>::value
  28. );
  29. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  30. static_assert(hana::Ring<R>::value,
  31. "hana::power(x, n) requires 'x' to be in a Ring");
  32. static_assert(hana::IntegralConstant<N>::value,
  33. "hana::power(x, n) requires 'n' to be an IntegralConstant");
  34. #endif
  35. static_assert(N::value >= 0,
  36. "hana::power(x, n) requires 'n' to be non-negative");
  37. return Power::apply(static_cast<X&&>(x), n);
  38. }
  39. //! @endcond
  40. template <typename R, bool condition>
  41. struct power_impl<R, when<condition>> : default_ {
  42. template <typename X, typename N>
  43. static constexpr decltype(auto) apply(X&& x, N const&) {
  44. constexpr std::size_t n = N::value;
  45. return hana::iterate<n>(
  46. hana::partial(hana::mult, static_cast<X&&>(x)),
  47. hana::one<R>()
  48. );
  49. }
  50. };
  51. BOOST_HANA_NAMESPACE_END
  52. #endif // !BOOST_HANA_POWER_HPP