title: c++ foreach loop with pointers instead of references
body: <p>I have this <code>Container</code> class with begin() and end() functions for use with c++11 foreach loops:</p>
  
  <pre><code>class Element
  {
      //content doesn't matter
  };
  
  class Container
  {
      Element* elements;
      int size;
  
      /* constructor, destructor, operators, methods, etc.. */
  
      Element* begin() { return elements; };
      Element* end()   { return elements + size; };
  };
  </code></pre>
  
  <p>This is now a valid c++11 foreach loop:</p>
  
  <pre><code>Container container;
  for (Element&amp; e : container)
  {
      //do something
  }
  </code></pre>
  
  <p>But now consider this foreach loop:</p>
  
  <pre><code>Container container;
  for (Element* e : container)
  {
      //do something
  }
  </code></pre>
  
  <p>Is it possible to have a foreach loop with <code>Element*</code> instead of <code>Element&amp;</code> like this?<br>
  This would also have the great advantage of preventing one from typing <code>for (Element e : container)</code> which would copy the element each time.</p>
  
  <p>In that case <code>begin()</code> and <code>end()</code> would have to return <code>Element**</code> as far as I know.  </p>
  
  <p>But <code>sizeof(Element)</code> is not guaranteed to be <code>sizeof(Element*)</code> and in most cases they don't match. Incrementing a pointer increments by the base type size which is <code>sizeof(Element)</code> for incrementing <code>Element*</code> and <code>sizeof(Element*)</code> for incrementing <code>Element**</code>.  </p>
  
  <p>So the prefix <code>operator++()</code> will offset the pointer by a false value and things get crappy. Any ideas how to get this to work?</p>