Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 125: Arithmitic Metaprogramming

These are example of using C++ template metaprogramming in processing arithmitic operations in compile time.

Section 125.1: Calculating power in O(log n)

This example shows an e cient way of calculating power using template metaprogramming.

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;

};

Example Usage:

std::cout << power<2, 9>::value;

Version ≥ C++14

This one also handles negative exponents:

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>

GoalKicker.com – C++ Notes for Professionals

607

struct powerDouble<base, 1>

{

static const int intermediateValue = base; constexpr static double value = base;

};

int main()

{

std::cout << powerDouble<2,-3>::value;

}

GoalKicker.com – C++ Notes for Professionals

608