fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define TYPE_INIT_(_Number, _Line, _Type,...) struct ln_##_Line##_number_##_Number : public _Type\
  5. {\
  6.   ln_##_Line##_number_##_Number()\
  7.   :_Type(__VA_ARGS__){}\
  8. \
  9.   ln_##_Line##_number_##_Number(const _Type& _val)\
  10.   :_Type(_val){}\
  11. \
  12.   const _Type& operator=(const _Type& _val)\
  13.   {\
  14.   ((_Type)(*this)) = _val;\
  15.   return (*this);\
  16.   }\
  17. }
  18. #define TYPE_INIT(_Number, _Line, _Type,...) TYPE_INIT_(_Number, _Line, _Type, __VA_ARGS__)
  19. #define INIT(_Type, ...) TYPE_INIT(__COUNTER__, __LINE__, _Type, __VA_ARGS__)
  20.  
  21. // использование
  22.  
  23. struct ABC
  24. {
  25. int A,B,C;
  26.  
  27. ABC(int _A,int _B,int _C)
  28. :A(_A),
  29. B(_B),
  30. C(_C)
  31. {}
  32. };
  33.  
  34.  
  35. struct TestClass
  36. {
  37. INIT( std::string, "Ololo" ) lv_String;
  38. INIT( ABC, 1, 2, 3 ) lv_ABC;
  39. };
  40.  
  41. std::ostream & operator << (std::ostream &out, const TestClass &t) {
  42. std::cout << t.lv_String << " " << t.lv_ABC.A << " " <<
  43. t.lv_ABC.B << " " << t.lv_ABC.C;
  44. return out;
  45. }
  46.  
  47. int main() {
  48.  
  49. TestClass t;
  50. std::cout << t << std::endl;
  51. t.lv_String = "Hui";
  52. t.lv_ABC = ABC(4,5,6);
  53. std::cout << t << std::endl;
  54. }
  55.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Ololo 1 2 3
Ololo 1 2 3