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<__LINE__> : CTCOrigin<__VA_ARGS__> { };
  12.  
  13. #define UPDATE_CTC(name) \
  14.   template <> struct name<__LINE__> : name<__LINE__ - 1> \
  15.   { \
  16.   static constexpr int value = name<__LINE__ - 1>::value + increment; \
  17.   };
  18.  
  19. #define GET_CTC(name) \
  20.   name<__LINE__>::value
  21.  
  22.  
  23. CREATE_CTC(ctc1);
  24. CREATE_CTC(ctc2, 2);
  25. CREATE_CTC(ctc3, 20, -5);
  26. constexpr auto i11 = GET_CTC(ctc1);
  27. constexpr auto i12 = GET_CTC(ctc2);
  28. constexpr auto i13 = GET_CTC(ctc3);
  29.  
  30. UPDATE_CTC(ctc1);
  31. UPDATE_CTC(ctc2);
  32. UPDATE_CTC(ctc3);
  33. constexpr auto i21 = GET_CTC(ctc1);
  34. constexpr auto i22 = GET_CTC(ctc2);
  35. constexpr auto i23 = GET_CTC(ctc3);
  36.  
  37. UPDATE_CTC(ctc1);
  38. UPDATE_CTC(ctc2);
  39. UPDATE_CTC(ctc3);
  40. constexpr auto i31 = GET_CTC(ctc1);
  41. constexpr auto i32 = GET_CTC(ctc2);
  42. constexpr auto i33 = GET_CTC(ctc3);
  43.  
  44. int main()
  45. {
  46. std::cout << i11 << ", " << i12 << ", " << i13 << std::endl;
  47. std::cout << i21 << ", " << i22 << ", " << i23 << std::endl;
  48. std::cout << i31 << ", " << i32 << ", " << i33 << std::endl;
  49. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0, 2, 20
1, 3, 15
2, 4, 10