title: Range-based loop from a temporary returned by a member function
body: <p>I'm a little unsure how the new (as of c++11) ranged-based loop works internally.</p>
  
  <p>If I were to do something like...</p>
  
  <pre><code>for( auto i : someclass.Elements() )
  {
   ...
  }
  </code></pre>
  
  <p>...where "Elements()" returns a a temporary, are there any side effects I might not be aware of?</p>
  
  <p>I will add a working prototype at the bottom of this question.</p>
  
  <p>I've verified that the type returned by "Elements()" is created once, which is my biggest concern. To be honest, this feels a bit <em>too</em> easy, and I haven't seen this implementation else where.</p>
  
  <p>For context, I have an XML library I created and want to be able to iterate over an element's children ("for( auto child: element->Children() ) {...}"), it's attributes ("for( auto attribute : element->Attributes() ( {...}"), and perhaps more specific children ("for( auto cats : element->Children( "cats" ) ) {...}".</p>
  
  <p>Here is a working prototype:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  template&lt; typename T &gt;
  class Array
  {
  public:
      Array()
      {       
      }
  
      Array( T firstValue )
      {
          m_elements.push_back( firstValue );
      }
  
      Array( const std::list&lt; T &gt; &amp; values )
      {
          m_elements.insert( m_elements.begin(), values.begin(), values.end() );
      }   
  
      virtual ~Array() 
      {
      }
  
      void Add( T value )
      {
          m_elements.push_back( value );
      }
  
      const T &amp; operator[]( int index ) const
      {
          return m_elements[ index ];
      }   
  
      size_t Count() const
      {
          return m_elements.size();
      }
  
      class ElementsIterable
      {
          Array &amp; m_array;
      public:
          ElementsIterable( Array &amp; array ) : m_array( array )
          {
              std::cout &lt;&lt; "ElementsIterable Constructed" &lt;&lt; std::endl;
          }
  
          class iterator
          {
          public:
              iterator( Array &amp; array, size_t index )
                  : m_array( array ), m_index( index )
              {
              }
  
              iterator &amp; operator++()
              {
                  m_index++;
                  return *this;
              }
  
              bool operator!=( const iterator &amp; itr ) const
              {
                  return m_index != itr.m_index;
              }
  
              const T &amp; operator*() const
              {
                  return m_array[ m_index ];
              }
  
          private:
              Array &amp; m_array;
              size_t m_index;
          };
  
          iterator begin() {
              return iterator( m_array, 0 );
          }
          iterator end() {
              return iterator( m_array, m_array.Count() );
          }
      };
  
      ElementsIterable Elements()
      {
          return ElementsIterable( *this );
      }
  
  private:
      std::vector&lt; T &gt; m_elements;
  };
  
  int main( int argc, char ** argv )
  {
      Array&lt; int &gt; array( { 1, 2, 3 } );
      array.Add( 4 );
  
      for( auto a : array.Elements() )
      {
          std::cout &lt;&lt; a &lt;&lt; std::endl;
      }
  
      return 0;
  } 
  </code></pre>