fork(4) download
  1. #include <iostream>
  2.  
  3. template <int ORIGIN = 0, int INCREMENT = 1>
  4. struct $CTCOrigin : std::integral_constant<int, ORIGIN>
  5. {
  6. static constexpr int increment = INCREMENT;
  7. };
  8.  
  9. #define CREATE_CTC(name, ...) \
  10.   template <size_t N> struct name : name<N - 1> { }; \
  11.   template <> struct name<0> : $CTCOrigin<__VA_ARGS__> \
  12.   { \
  13.   static constexpr int counter_base = __COUNTER__; \
  14.   };
  15.  
  16. #define UPDATE_CTC_(name, counter) \
  17.   template <> struct name<counter - name<0>::counter_base> \
  18.   : name<counter - name<0>::counter_base - 1> \
  19.   { \
  20.   using base = name<counter - name<0>::counter_base - 1>; \
  21.   static constexpr int value = base::value + increment; \
  22.   };
  23. #define UPDATE_CTC(name) UPDATE_CTC_(name, __COUNTER__)
  24.  
  25. #define GET_CTC(name) \
  26.   name<__COUNTER__ - name<0>::counter_base>::value
  27.  
  28. CREATE_CTC(ctc1);
  29. CREATE_CTC(ctc2, 2);
  30. CREATE_CTC(ctc3, 20, -5);
  31. constexpr auto i11 = GET_CTC(ctc1);
  32. constexpr auto i12 = GET_CTC(ctc2);
  33. constexpr auto i13 = GET_CTC(ctc3);
  34.  
  35. UPDATE_CTC(ctc1);
  36. UPDATE_CTC(ctc2);
  37. UPDATE_CTC(ctc3);
  38. constexpr auto i21 = GET_CTC(ctc1);
  39. constexpr auto i22 = GET_CTC(ctc2);
  40. constexpr auto i23 = GET_CTC(ctc3);
  41.  
  42. UPDATE_CTC(ctc1);
  43. UPDATE_CTC(ctc2);
  44. UPDATE_CTC(ctc3);
  45. constexpr auto i31 = GET_CTC(ctc1);
  46. constexpr auto i32 = GET_CTC(ctc2);
  47. constexpr auto i33 = GET_CTC(ctc3);
  48.  
  49. int main()
  50. {
  51. std::cout << i11 << ", " << i12 << ", " << i13 << std::endl;
  52. std::cout << i21 << ", " << i22 << ", " << i23 << std::endl;
  53. std::cout << i31 << ", " << i32 << ", " << i33 << std::endl;
  54. }
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
0, 2, 20
1, 3, 15
2, 4, 10