// John Bartholomew, stack overflow // http://stackoverflow.com/questions/4608763/compile-time-templated-c-calculations-on-unsigned-long-long-on-doubles #include template struct Power; template struct Power { // 0: P is even (square N and halve the power) static const unsigned long long val = Power::val; }; template struct Power { // 1: P is odd (multiply by N and decrement the power) static const unsigned long long val = N * Power::val; }; template struct Power { // zero (x**0 is 1 for all x != 0) static const unsigned long long val = 1; }; int main() { std::cout << "2**0 = " << Power<2,0>::val << "\n"; std::cout << "2**1 = " << Power<2,1>::val << "\n"; std::cout << "2**2 = " << Power<2,2>::val << "\n"; std::cout << "2**3 = " << Power<2,3>::val << "\n"; std::cout << "2**4 = " << Power<2,4>::val << "\n"; std::cout << "2**5 = " << Power<2,5>::val << "\n"; std::cout << "2**6 = " << Power<2,6>::val << "\n"; std::cout << "2**7 = " << Power<2,7>::val << "\n"; std::cout << "2**8 = " << Power<2,8>::val << "\n"; std::cout << "2**9 = " << Power<2,9>::val << "\n"; std::cout << "2**10 = " << Power<2,10>::val << "\n"; std::cout << "2**11 = " << Power<2,11>::val << "\n"; std::cout << "2**12 = " << Power<2,12>::val << "\n"; std::cout << "2**30 = " << Power<2,30>::val << "\n"; std::cout << "2**40 = " << Power<2,40>::val << "\n"; std::cout << "2**50 = " << Power<2,50>::val << "\n"; std::cout << "2**60 = " << Power<2,60>::val << "\n"; return 0; }