以 O(log n)计算功率
此示例显示了使用模板元编程计算功率的有效方法。
template <int base, unsigned int exponent>
struct power
{
static const int halfvalue = power<base, exponent / 2>::value;
static const int value = halfvalue * halfvalue * power<base, exponent % 2>::value;
};
template <int base>
struct power<base, 0>
{
static const int value = 1;
static_assert(base != 0, "power<0, 0> is not allowed");
};
template <int base>
struct power<base, 1>
{
static const int value = base;
};
用法示例:
std::cout << power<2, 9>::value;
Version >= C++ 14
这个也处理负指数:
template <int base, int exponent>
struct powerDouble
{
static const int exponentAbs = exponent < 0 ? (-exponent) : exponent;
static const int halfvalue = powerDouble<base, exponentAbs / 2>::intermediateValue;
static const int intermediateValue = halfvalue * halfvalue * powerDouble<base, exponentAbs % 2>::intermediateValue;
constexpr static double value = exponent < 0 ? (1.0 / intermediateValue) : intermediateValue;
};
template <int base>
struct powerDouble<base, 0>
{
static const int intermediateValue = 1;
constexpr static double value = 1;
static_assert(base != 0, "powerDouble<0, 0> is not allowed");
};
template <int base>
struct powerDouble<base, 1>
{
static const int intermediateValue = base;
constexpr static double value = base;
};
int main()
{
std::cout << powerDouble<2,-3>::value;
}