title: Equivalent C++ to Python generator pattern
body: <p>I've got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some manner.</p>
  
  <h2>Python</h2>
  
  <p>This is a basic sequence generator, clearly too large to store a materialized version.</p>
  
  <pre><code>def pair_sequence():
      for i in range(2**32):
          for j in range(2**32):
              yield (i, j)
  </code></pre>
  
  <p>The goal is to maintain two instances of the sequence above, and iterate over them in semi-lockstep, but in chunks. In the example below the <code>first_pass</code> uses the sequence of pairs to initialize the buffer, and the <code>second_pass</code> regenerates the <em>same exact sequence</em> and processes the buffer again.</p>
  
  <pre><code>def run():
      seq1 = pair_sequence()
      seq2 = pair_sequence()
  
      buffer = [0] * 1000
      first_pass(seq1, buffer)
      second_pass(seq2, buffer)
      ... repeat ...
  </code></pre>
  
  <h2>C++</h2>
  
  <p>The only thing I can find for a solution in C++ is to mimic <code>yield</code> with C++ coroutines, but I haven't found any good reference on how to do this. I'm also interested in alternative (non general) solutions for this problem. I do not have enough memory budget to keep a copy of the sequence between passes.</p>