| The power function on integer type arguments - alternative implementation with an enumeration constant. | Lecture 5 - slide 13 : 39 Program 3 |
// The power function on integer type arguments - use of enumeration constants.
#include <iostream>
// The general case:
template <unsigned int N, unsigned int P> struct Power{
enum {value = N * Power<N,P-1>::value};
};
// The base case, via template specialization:
template <unsigned int N>struct Power<N,1> {
enum {value = 1};
};
int main(){
std::cout << Power<5,3>::value << std::endl; // 25
std::cout << Power<5,5>::value << std::endl; // 625
std::cout << Power<5,7>::value << std::endl; // 15625
}