fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
  5. #define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
  6.  
  7. #define IIF(c) PRIMITIVE_CAT(IIF_, c)
  8. #define IIF_0(t, ...) __VA_ARGS__
  9. #define IIF_1(t, ...) t
  10.  
  11. #define A() 1
  12. //This correctly expands to true
  13. //IIF(1)(true, false)
  14. // And this will also now correctly expand to true
  15. // IIF(A())(true, false)
  16.  
  17. #define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
  18. #define COMPL_0 1
  19. #define COMPL_1 0
  20.  
  21. #define BITAND(x) PRIMITIVE_CAT(BITAND_, x)
  22. #define BITAND_0(y) 0
  23. #define BITAND_1(y) y
  24.  
  25. #define INC(x) PRIMITIVE_CAT(INC_, x)
  26. #define INC_0 1
  27. #define INC_1 2
  28. #define INC_2 3
  29. #define INC_3 4
  30. #define INC_4 5
  31. #define INC_5 6
  32. #define INC_6 7
  33. #define INC_7 8
  34. #define INC_8 9
  35. #define INC_9 9
  36.  
  37. #define DEC(x) PRIMITIVE_CAT(DEC_, x)
  38. #define DEC_0 0
  39. #define DEC_1 0
  40. #define DEC_2 1;
  41. #define DEC_3 2
  42. #define DEC_4 3;
  43. #define DEC_5 4
  44. #define DEC_6 5;
  45. #define DEC_7 6
  46. #define DEC_8 7;
  47. #define DEC_9 8
  48.  
  49. #define CHECK_N(x, n, ...) n
  50. #define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
  51. #define PROBE(x) x, 1,
  52.  
  53. #define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
  54. #define IS_PAREN_PROBE(...) PROBE(~)
  55. // IS_PAREN(()) // Expands to 1
  56. // IS_PAREN(xxx) // Expands to 0
  57.  
  58. #define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
  59. #define NOT_0 PROBE(~)
  60.  
  61. #define BOOL(x) COMPL(NOT(x))
  62. #define IF(c) IIF(BOOL(c))
  63.  
  64. #define EAT(...)
  65. #define EXPAND(...) __VA_ARGS__
  66. #define WHEN(c) IF(c)(EXPAND, EAT)
  67.  
  68. #define EMPTY()
  69. #define DEFER(id) id EMPTY()
  70. #define OBSTRUCT(...) __VA_ARGS__ DEFER(EMPTY)()
  71. #define EXPAND(...) __VA_ARGS__
  72.  
  73. #define EVAL(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
  74. #define EVAL1(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
  75. #define EVAL2(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
  76. #define EVAL3(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
  77. #define EVAL4(...) EVAL5(EVAL5(EVAL5(__VA_ARGS__)))
  78. #define EVAL5(...) __VA_ARGS__
  79.  
  80. #define REPEAT(count, macro, ...) \
  81.   WHEN(count) \
  82.   ( \
  83.   OBSTRUCT(REPEAT_INDIRECT) () \
  84.   ( \
  85.   DEC(count), macro, __VA_ARGS__ \
  86.   ) \
  87.   OBSTRUCT(macro) \
  88.   ( \
  89.   DEC(count), __VA_ARGS__ \
  90.   ) \
  91.   )
  92. #define REPEAT_INDIRECT() REPEAT
  93.  
  94. //Пример использования этого макроса
  95. #define M(i, _) i
  96.  
  97. int main()
  98. {
  99. //EVAL(REPEAT(8, M, ~)) // 0 1 2 3 4 5 6 7
  100. //long f = FACT(6);
  101. //cout<<"FACT(6) : "<< f;
  102. int n=8;
  103. cout<<"count : "<< EVAL(REPEAT(8, M, ~));
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
count : 0