fork download
  1. #define GENERATOR(name) \
  2. struct name \
  3. { \
  4.   template<typename F> \
  5.   void operator()(F yield) \
  6. /**/
  7. #define _ };
  8.  
  9. template<typename Gen>
  10. struct Adaptor
  11. {
  12. Gen f;
  13. template<typename C>
  14. void operator*(C cont)
  15. {
  16. f(cont);
  17. }
  18. };
  19.  
  20. template<typename Gen>
  21. Adaptor<Gen> make_adaptor(Gen gen)
  22. {
  23. return {gen};
  24. }
  25.  
  26. #define FOREACH(arg, gen) make_adaptor(gen) * [&](arg)
  27.  
  28. /******************************************************/
  29.  
  30. #include <iostream>
  31. using namespace std;
  32.  
  33. GENERATOR(integers)
  34. {
  35. yield(1);
  36. yield(2);
  37. yield(4);
  38. yield(8);
  39. yield(16777216);
  40. }_
  41.  
  42. int main()
  43. {
  44. FOREACH(int i, integers())
  45. {
  46. cout << i << endl;
  47. };
  48. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
1
2
4
8
16777216