fork download
  1. #include <iostream>
  2. #include <ostream>
  3.  
  4. template<int Size>
  5. struct CrapParam
  6. {
  7. bool is_relevant()
  8. {
  9. return Size%2 == 0;
  10. }
  11. unsigned calc_size()
  12. {
  13. return Size;
  14. }
  15. void drop()
  16. {
  17. std::cout << "Dropped item with size="<< calc_size() << std::endl;
  18. }
  19. };
  20.  
  21. struct Dropper
  22. {
  23. template<class Item>
  24. void operator&(Item &item)
  25. {
  26. std::cout << "\t";
  27. item.drop();
  28. }
  29. };
  30.  
  31. struct Updater
  32. {
  33. unsigned size;
  34. Updater(): size(0) {}
  35. template<class Item>
  36. void operator&(Item &item)
  37. {
  38. std::cout << "\t";
  39. if(item.is_relevant())
  40. {
  41. std::cout << "Relevant item";
  42. size+=item.calc_size();
  43. }
  44. else
  45. {
  46. std::cout << "Irrelevant item";
  47. }
  48. std::cout << " with size=" << item.calc_size() << std::endl;
  49. }
  50. };
  51.  
  52. struct ParamAggregator
  53. {
  54. unsigned counter;
  55. ParamAggregator(): counter(0) {}
  56.  
  57. CrapParam<1> p1;
  58. CrapParam<2> someShit2;
  59. CrapParam<5> superCrap3;
  60. CrapParam<4> crappyShit4;
  61.  
  62. template<class Archive>
  63. void serialize(Archive & ar)
  64. {
  65. ar & p1;
  66. ar & someShit2;
  67. ar & superCrap3;
  68. ar & crappyShit4;
  69. }
  70.  
  71. int update()
  72. {
  73. ++counter;
  74. std::cout << "Updating, counter=" << counter << std::endl;
  75. Updater updater;
  76. serialize(updater);
  77. std::cout << "Updated, size=" << updater.size << std::endl << std::endl;
  78. return updater.size;
  79. }
  80.  
  81. void drop()
  82. {
  83. std::cout << "Dropping items" << std::endl;
  84. Dropper d;
  85. serialize(d);
  86. std::cout << "Items are droped" << std::endl << std::endl;
  87. }
  88. };
  89.  
  90.  
  91.  
  92. int main(int argc,char *argv[])
  93. {
  94. ParamAggregator pa;
  95. pa.update();
  96. pa.drop();
  97. pa.update();
  98. return 0;
  99. }
  100.  
Success #stdin #stdout 0.01s 2684KB
stdin
Standard input is empty
stdout
Updating, counter=1
	Irrelevant item with size=1
	Relevant item with size=2
	Irrelevant item with size=5
	Relevant item with size=4
Updated, size=6

Dropping items
	Dropped item with size=1
	Dropped item with size=2
	Dropped item with size=5
	Dropped item with size=4
Items are droped

Updating, counter=2
	Irrelevant item with size=1
	Relevant item with size=2
	Irrelevant item with size=5
	Relevant item with size=4
Updated, size=6