Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////
- // Scenario 1 - std::experimental::generator
- auto positive() -> generator<int> {
- int val = 0;
- while (true)
- __yield_value ++val;
- }
- for (auto&& i : positive())
- printf("%d\n", i);
- /////////////////////////////////////////////////////////////////////
- // Scenario 2 - light-weight but hard-coded version
- struct positive_iterator {
- int& operator*() { return val; }
- positive_iterator& operator++() { val++; return *this; }
- bool operator!=(const positive_iterator& other) { return true; }
- private:
- int val = 1;
- };
- struct light_generator {
- auto begin() { return positive_iterator {}; }
- auto end() { return positive_iterator {}; }
- };
- for (auto&& i : light_generator())
- printf("%d\n", i);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement