title: Equivalent of enumerators in C++11?
body: <p>In C#, you can define a custom enumeration very trivially, eg:</p>
  
  <pre><code>public IEnumerable&lt;Foo&gt; GetNestedFoos()
  {
      foreach (var child in _SomeCollection)
      {
          foreach (var foo in child.FooCollection)
          {
              yield return foo;
          }
          foreach (var bar in child.BarCollection)
          {
              foreach (var foo in bar.MoreFoos)
              {
                  yield return foo;
              }
          }
      }
      foreach (var baz in _SomeOtherCollection)
      {
          foreach (var foo in baz.GetNestedFoos())
          {
              yield return foo;
          }
      }
  }
  </code></pre>
  
  <p>(This can be simplified using LINQ and better encapsulation but that's not the point of the question.)</p>
  
  <hr>
  
  <p>In C++11, you can do similar enumerations but AFAIK it requires a visitor pattern instead:</p>
  
  <pre><code>template&lt;typename Action&gt;
  void VisitAllFoos(const Action&amp; action)
  {
      for (auto&amp; child : m_SomeCollection)
      {
          for (auto&amp; foo : child.FooCollection)
          {
              action(foo);
          }
          for (auto&amp; bar : child.BarCollection)
          {
              for (auto&amp; foo : bar.MoreFoos)
              {
                  action(foo);
              }
          }
      }
      for (auto&amp; baz : m_SomeOtherCollection)
      {
          baz.VisitAllFoos(action);
      }
  }
  </code></pre>
  
  <p>Is there a way to do something more like the first, where the function returns a range that can be iterated externally rather than calling a visitor internally?</p>
  
  <p>(And I don't mean by constructing a <code>std::vector&lt;Foo&gt;</code> and returning it -- it should be an in-place enumeration.)</p>
  
  <p>I am aware of the Boost.Range library, which I suspect would be involved in the solution, but I'm not particularly familiar with it.</p>
  
  <p>I'm also aware that it's possible to define custom iterators to do this sort of thing (which I also suspect might be involved in the answer) but I'm looking for something that's easy to write, ideally no more complicated than the examples shown here, and composable (like with <code>_SomeOtherCollection</code>).</p>
  
  <p>I would prefer something that does not require the caller to use lambdas or other functors (since that just makes it a visitor again), although I don't mind using lambdas internally if needed (but would still prefer to avoid them there too).</p>