fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define DEF_BASE(name) namespace { constexpr int name##localbase = __COUNTER__; }
  4. #define GET_BASE(name) (name##localbase)
  5. #define GET_OFFSET(name) GET_OFFSET_C(name,__COUNTER__)
  6. #define GET_OFFSET_C(name,C) ((C)-GET_BASE(name))
  7.  
  8.  
  9. #define BODY(V) { static constexpr int value = (V); }
  10.  
  11. #define START(name) \
  12.   DEF_BASE(name) \
  13.   template<int offset> struct name BODY(name<offset-1>::value); \
  14.   template<> struct name<0> BODY(0);
  15.  
  16. #define GET(name) (name<GET_OFFSET(name)>::value)
  17.  
  18. #define NEXT(name) NEXT_C(name, __COUNTER__) // нам понадобится одно значение дважды, поэтому опосредуем
  19. #define NEXT_C(name, C) \
  20.   template<> struct name<GET_OFFSET_C(name,C)> BODY(name<GET_OFFSET_C(name,C)-1>::value + 1);
  21.  
  22. const int c0 = (__COUNTER__, __COUNTER__);
  23. START(foo)
  24. const int x = GET(foo);
  25. const int c1 = (__COUNTER__, __COUNTER__);
  26. const int y = GET(foo);
  27. const int c2 = (__COUNTER__, __COUNTER__);
  28. NEXT(foo)
  29. const int z = GET(foo);
  30. const int c3 = (__COUNTER__, __COUNTER__);
  31. const int t = GET(foo);
  32.  
  33. int main() {
  34. cout << c0 << endl;
  35. cout << x << endl;
  36. cout << c1 << endl;
  37. cout << y << endl;
  38. cout << c2 << endl;
  39. cout << z << endl;
  40. cout << c3 << endl;
  41. cout << t << endl;
  42. }
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
1
0
5
0
8
1
12
1