Skip to content

Instantly share code, notes, and snippets.

@ArGxento
Created October 28, 2012 13:05
Show Gist options
  • Save ArGxento/3968536 to your computer and use it in GitHub Desktop.
Save ArGxento/3968536 to your computer and use it in GitHub Desktop.
note about pseudo-coroutine in C++
#include <iostream>
#include <boost/optional.hpp>
boost::optional<int> coro (int const a)
{
#define yield(arg) ___lino = __LINE__;return boost::optional<int>(arg);case __LINE__:
#define yield_return ___lino = 0;return boost::optional<int>();case __LINE__:
static int ___lino = 0;
//decl of vars
//--------------------
static int i;
//--------------------
switch(___lino){
case 0:
//----init of vars
i = 0;
//------------------
for(i=0; i < 42; i++){
if(i & 1){
yield(i);
} else {
yield(i*2);
}
}
yield_return;
}
return boost::optional<int>();
#undef yield
#undef yield_return
}
int main()
{
while(boost::optional<int> i = coro(0)){
std::cout << *i << std::endl;
}
while(boost::optional<int> i = coro(0)){
std::cout << *i * 2 << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment