fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. // List of factors
  5. template<std::intmax_t ... Misc>
  6. struct factors { };
  7.  
  8. // Declaration
  9. template<std::intmax_t ... Misc>
  10. struct factorization;
  11.  
  12. // Initial specialization
  13. template<std::intmax_t Value>
  14. struct factorization<Value>
  15. {
  16. typedef typename std::conditional<Value % 2 == 0,
  17. typename factorization<Value / 2, 2, 2>::type,
  18. typename factorization<Value / 2, 2 + 1>::type>::type type;
  19. };
  20.  
  21. // Initial specialization when the value is not divisible by 2
  22. template<std::intmax_t Value, std::intmax_t Divisor>
  23. struct factorization<Value, Divisor>
  24. {
  25. typedef typename std::conditional<Value % Divisor == 0,
  26. typename factorization<Value / Divisor, Divisor, Divisor>::type,
  27. typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
  28. };
  29.  
  30. // Specialization after the first recusion step
  31. template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t Prime>
  32. struct factorization<Value, Divisor, Prime>
  33. {
  34. typedef typename std::conditional<Value % Divisor == 0,
  35. typename factorization<Value / Divisor, Divisor, Divisor>::type,
  36. typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
  37. };
  38.  
  39. // Recursion specialization
  40. template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t Prime, std::intmax_t ... Primes>
  41. struct factorization<Value, Divisor, Prime, Primes...>
  42. {
  43. typedef typename std::conditional<Value % Divisor == 0 && Divisor != Prime,
  44. typename factorization<Value / Divisor, Divisor, Divisor, Prime,
  45. Primes...>::type,
  46. typename factorization<
  47. Value % Divisor == 0 ? Value / Divisor : Value,
  48. Divisor + (Value % Divisor != 0), Prime, Primes...>::type>::type type;
  49. };
  50.  
  51. // Last recursion step
  52. template<std::intmax_t Value, std::intmax_t ... Primes>
  53. struct factorization<Value, Value, Primes...>
  54. {
  55. typedef typename factorization<1, Value, Value, Primes...>::type type;
  56. };
  57.  
  58. // Finalize
  59. template<std::intmax_t Divisor, std::intmax_t ... Primes>
  60. struct factorization<1, Divisor, Primes...>
  61. {
  62. typedef factors<Primes...> type;
  63. };
  64.  
  65. // Main
  66. int main() {
  67. typename factorization<18>::type x;
  68. return 0;
  69. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘struct factorization<4ll, 2ll, 2ll>’:
prog.cpp:36:79:   required from ‘struct factorization<9ll, 2ll, 2ll>’
prog.cpp:18:67:   required from ‘struct factorization<18ll>’
prog.cpp:67:28:   required from here
prog.cpp:36:79: error: ambiguous class template instantiation for ‘struct factorization<2ll, 2ll, 2ll>’
             typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
                                                                               ^
prog.cpp:32:8: error: candidates are: struct factorization<Value, Divisor, Prime>
 struct factorization<Value, Divisor, Prime>
        ^
prog.cpp:41:8: error:                 struct factorization<Value, Divisor, Prime, Primes ...>
 struct factorization<Value, Divisor, Prime, Primes...>
        ^
prog.cpp:53:8: error:                 struct factorization<Value, Value, Primes ...>
 struct factorization<Value, Value, Primes...>
        ^
prog.cpp:36:79: error: invalid use of incomplete type ‘struct factorization<2ll, 2ll, 2ll>’
             typename factorization<Value / Divisor, Divisor + 1>::type>::type type;
                                                                               ^
prog.cpp:10:8: error: declaration of ‘struct factorization<2ll, 2ll, 2ll>’
 struct factorization;
        ^
prog.cpp: In function ‘int main()’:
prog.cpp:67:30: error: invalid combination of multiple type-specifiers
  typename factorization<18>::type x;
                              ^
prog.cpp:67:36: error: invalid type in declaration before ‘;’ token
  typename factorization<18>::type x;
                                    ^
prog.cpp:67:35: warning: unused variable ‘x’ [-Wunused-variable]
  typename factorization<18>::type x;
                                   ^
stdout
Standard output is empty