]>
Commit | Line | Data |
---|---|---|
1 | /*! | |
2 | @file | |
3 | Defines `boost::hana::power`. | |
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_POWER_HPP | |
11 | #define BOOST_HANA_POWER_HPP | |
12 | ||
13 | #include <boost/hana/fwd/power.hpp> | |
14 | ||
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> | |
23 | ||
24 | #include <cstddef> | |
25 | ||
26 | ||
27 | BOOST_HANA_NAMESPACE_BEGIN | |
28 | //! @cond | |
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 | |
35 | ); | |
36 | ||
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"); | |
40 | ||
41 | static_assert(hana::IntegralConstant<N>::value, | |
42 | "hana::power(x, n) requires 'n' to be an IntegralConstant"); | |
43 | #endif | |
44 | ||
45 | static_assert(N::value >= 0, | |
46 | "hana::power(x, n) requires 'n' to be non-negative"); | |
47 | ||
48 | return Power::apply(static_cast<X&&>(x), n); | |
49 | } | |
50 | //! @endcond | |
51 | ||
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)), | |
59 | hana::one<R>() | |
60 | ); | |
61 | } | |
62 | }; | |
63 | BOOST_HANA_NAMESPACE_END | |
64 | ||
65 | #endif // !BOOST_HANA_POWER_HPP |