title: "yield" keyword for C++, How to Return an Iterator from my Function?
body: <p>Consider the following code.</p>
  <pre><code>std::vector&lt;result_data&gt; do_processing() 
  {
      pqxx::result input_data = get_data_from_database();
      return process_data(input_data);
  }
  
  std::vector&lt;result_data&gt; process_data(pqxx::result const &amp; input_data)
  {
      std::vector&lt;result_data&gt; ret;
      pqxx::result::const_iterator row;
      for (row = input_data.begin(); row != inpupt_data.end(); ++row) 
      {
          // somehow populate output vector
      }
      return ret;
  }
  </code></pre>
  <p>While I was thinking about whether or not I could expect Return Value Optimization (RVO) to happen, I found <a href="https://stackoverflow.com/a/3134989/1025391">this answer by Jerry Coffin</a> [emphasis mine]:</p>
  <blockquote>
  <p>At least IMO, it's usually a poor idea, but not for efficiency reasons. It's a poor idea because the function in question should usually be written as a generic algorithm <strong>that produces its output via an iterator.</strong> Almost any code that accepts or returns a container instead of operating on iterators should be considered suspect.</p>
  <p>Don't get me wrong: there are times it makes sense to pass around collection-like objects (e.g., strings) but for the example cited, I'd consider passing or returning the vector a poor idea.</p>
  </blockquote>
  <p>Having some Python background, I like Generators very much. Actually, if it were Python, I would have written above function as a Generator, i.e. to avoid the necessity of processing the entire data before anything else could happen. For example like this:</p>
  <pre class="lang-py prettyprint-override"><code>def process_data(input_data):
      for item in input_data:
          # somehow process items
          yield result_data
  </code></pre>
  <p>If I correctly interpreted Jerry Coffins note, this is what he suggested, isn't it? If so, how can I implement this in C++?</p>