3 Defines `boost::hana::power`.
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)
10 #ifndef BOOST_HANA_POWER_HPP
11 #define BOOST_HANA_POWER_HPP
13 #include <boost/hana/fwd/power.hpp>
15 #include <boost/hana/concept/integral_constant.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/functional/iterate.hpp>
20 #include <boost/hana/functional/partial.hpp>
21 #include <boost/hana/mult.hpp>
22 #include <boost/hana/one.hpp>
27 BOOST_HANA_NAMESPACE_BEGIN
29 template <typename X, typename N>
30 constexpr decltype(auto) power_t::operator()(X&& x, N const& n) const {
31 using R = typename hana::tag_of<X>::type;
32 using Power = BOOST_HANA_DISPATCH_IF(power_impl<R>,
33 hana::Ring<R>::value &&
34 hana::IntegralConstant<N>::value
37 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
38 static_assert(hana::Ring<R>::value,
39 "hana::power(x, n) requires 'x' to be in a Ring");
41 static_assert(hana::IntegralConstant<N>::value,
42 "hana::power(x, n) requires 'n' to be an IntegralConstant");
45 static_assert(N::value >= 0,
46 "hana::power(x, n) requires 'n' to be non-negative");
48 return Power::apply(static_cast<X&&>(x), n);
52 template <typename R, bool condition>
53 struct power_impl<R, when<condition>> : default_ {
54 template <typename X, typename N>
55 static constexpr decltype(auto) apply(X&& x, N const&) {
56 constexpr std::size_t n = N::value;
57 return hana::iterate<n>(
58 hana::partial(hana::mult, static_cast<X&&>(x)),
63 BOOST_HANA_NAMESPACE_END
65 #endif // !BOOST_HANA_POWER_HPP