templates/functions/pow/pow1.cpp - The power function on integer type arguments. | Lecture 6 - slide 40 : 40 Program 2 |
// The power function on integer type arguments. #include <iostream> // The general case: template <unsigned int N, unsigned int P> struct Power{ static const unsigned int value = N * Power<N,P-1>::value; }; // The base case, via template specialization: template <unsigned int N>struct Power<N,1> { static const unsigned int 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 }