Created
March 21, 2016 03:02
-
-
Save engelmarkus/c543c148f43938f0edc0 to your computer and use it in GitHub Desktop.
Using a boost coroutine for lazily generating an endless sequence of numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
#include <boost/asio/coroutine.hpp> | |
#include <boost/asio/yield.hpp> | |
using namespace std; | |
using namespace boost::asio; | |
struct endless : coroutine { | |
int i = 0; | |
int operator()() { | |
reenter(this) while (true) { | |
yield return i++; | |
} | |
} | |
}; | |
int main() { | |
endless e; | |
for (int i = 0; i < 5; ++i) { | |
cout << e() << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment