Skip to content

Instantly share code, notes, and snippets.

@engelmarkus
Created March 21, 2016 03:02
Show Gist options
  • Save engelmarkus/c543c148f43938f0edc0 to your computer and use it in GitHub Desktop.
Save engelmarkus/c543c148f43938f0edc0 to your computer and use it in GitHub Desktop.
Using a boost coroutine for lazily generating an endless sequence of numbers.
#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