Generators in C++ revisited.

June 25, 2008

Filed under: Philosophy,Source code — Andrew @ 6:09 pm

Previous version of generators has a design problem – it required some special stop value. That is the same kind of problem as with iterators in C++ – they require special end() value. But in some cases it is not even possible to choose such a value.

So is this new version:

// generator/continuation for C++
// author: Andrew Fedoniouk @
// idea borrowed from: "coroutines in C" Simon Tatham,

  //++ coroutine, generator, continuation for C++

  struct _generator
    int _line;
    _generator():_line(-1) {}

  #define $generator(NAME) struct NAME : public _generator

  #define $emit(T) bool operator()(T& _rv) { \
                      if(_line < 0) { _line=0;}\
                      switch(_line) { case 0:;

  #define $stop  } _line = 0; return false; }

  #define $yield(V)     \
          do {\
              _rv = (V); return true; case __LINE__:;\
          } while (0)

  //-- coroutine, generator, continuation for C++

Implementation of typical iterator practically is the same an in previous version except of $stop is being used without any parameter now:

#include "generator.h"

   int i;
   $emit(int) // will emit int values. Start of body of the generator.
      for (i = 10; i > 0; i–)
         $yield(i); // a.k.a. yield in Python, 
                      // returns next number in [1..10], reversed.
   $stop; // stop, end of sequence. End of body of the generator.

But its usage is a bit different and is close to JavaScript for(var in sequence) statement:

int main(int argc, char* argv[])
  descent gen;
  for(int n; gen(n);) // "get next" generator invocation
    printf("next number is %d\n", n);
  return 0;

And here is version of the generator that supports restart (recursive call) - needed for walking through tree alike (recursive) data structures. It uses allocations on the heap that I think is overkill for such constructions.


  1. Cool!

    What’s the license on this? ;D

    Comment by LKRaider — September 19, 2008 @ 10:06 am

  2. “What’s the license on this?”

    OpenContentLicense ( )
    or BSD ( ).

    Whatever you like.

    Comment by Andrew — September 19, 2008 @ 12:32 pm

  3. I’ve published article on CodeProject about this stuff.

    Comment by Andrew — September 26, 2008 @ 8:06 pm

  4. Hello. I’m software engineer and these days seek to use ‘continuation’ in c++ for single-threaded net-message process that needs to be waited for response from another machine and I tried that in ugly MACRO.

    But yesterday I found your article and applied your generator code. It works greatly for me.
    Thank you and I like that code very much :)

    I uploaded my test continuation-applied message processing frameworks(with your generator code) in my blog. (sorry because it was published in KOREAN)

    Thanks again.

    Comment by icedac — March 26, 2009 @ 12:53 am

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.