fork(3) download
  1. #include <iostream>
  2.  
  3. template <int>
  4. struct Inc
  5. {
  6. enum { value = 0 };
  7. };
  8.  
  9. template <int index>
  10. struct Id
  11. {
  12. enum { value = Id<index - 1>::value + Inc<index - 1>::value };
  13. };
  14.  
  15. template <>
  16. struct Id<0>
  17. {
  18. enum { value = 0 };
  19. };
  20.  
  21. #define CLASS_DECLARATION(Class) \
  22. template <> \
  23. struct Inc<__LINE__> \
  24. { \
  25. enum { value = 1 }; \
  26. }; \
  27.  \
  28. struct Class \
  29. { \
  30. enum { id = Id<__LINE__>::value }; \
  31. private:
  32.  
  33. CLASS_DECLARATION(A)
  34. // ...
  35. };
  36.  
  37. CLASS_DECLARATION(B)
  38. // ...
  39. };
  40.  
  41. CLASS_DECLARATION(C)
  42. // ...
  43. };
  44.  
  45. int main()
  46. {
  47. std::cout << A::id << std::endl;
  48. std::cout << B::id << std::endl;
  49. std::cout << C::id << std::endl;
  50. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
0
1
2