fork(3) download
  1. #include <iostream>
  2.  
  3. template<int N>
  4. constexpr int factorial() {
  5. return N*factorial<N-1>();
  6. }
  7.  
  8. template<>
  9. constexpr int factorial<0>() {
  10. return 1;
  11. }
  12.  
  13. template<>
  14. constexpr int factorial<1>() {
  15. return 1;
  16. }
  17.  
  18. struct factorials {
  19. static constexpr int tab[9]{factorial<1>(), factorial<2>(), factorial<3>(), factorial<4>(), factorial<5>(), factorial<6>(), factorial<7>(), factorial<8>(), factorial<9>()};
  20. };
  21.  
  22. constexpr int factorials::tab[];
  23.  
  24. int main() {
  25. for(int i=0; i<9; i++) {
  26. std::cout << factorials::tab[i] << std::endl;
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1
2
6
24
120
720
5040
40320
362880