taming-cpp

Experiments with C++ and comparisons with C
git clone https://git.tronto.net/taming-cpp
Download | Log | Files | Refs | README

factorial.cpp (393B)


      1 // Taken from https://en.wikipedia.org/wiki/Template_metaprogramming
      2 
      3 #include <iostream>
      4 
      5 template<long long N>
      6 class factorial {
      7 public:
      8 	static constexpr long long value = N * factorial<N-1>::value;
      9 };
     10 
     11 template<>
     12 class factorial<0> {
     13 public:
     14 	static constexpr long long value = 1;
     15 };
     16 
     17 int main() {
     18 	constexpr long long X = 12;
     19 	std::cout << factorial<X>::value << std::endl;
     20 
     21 	return 0;
     22 }