Advertisement
Guest User

C++/17 Generator CodeGen

a guest
May 1st, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////
  2. // Scenario 1 - std::experimental::generator
  3. auto positive() -> generator<int> {
  4.    int val = 0;
  5.    while (true)
  6.       __yield_value ++val;
  7. }
  8.  
  9. for (auto&& i : positive())  
  10.    printf("%d\n", i);
  11.  
  12.  
  13.  
  14. /////////////////////////////////////////////////////////////////////
  15. // Scenario 2 - light-weight but hard-coded version
  16. struct positive_iterator {
  17.    int& operator*() { return val; }
  18.    positive_iterator& operator++() { val++; return *this; }
  19.    bool operator!=(const positive_iterator& other) { return true; }
  20. private:
  21.    int val = 1;
  22. };
  23.  
  24. struct light_generator {
  25.    auto begin() { return positive_iterator {}; }
  26.    auto end() { return positive_iterator {}; }
  27. };
  28.  
  29. for (auto&& i : light_generator())  
  30.    printf("%d\n", i);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement