title: For range (c++11) using virtual interface object
body: <p>I'm programming to interfaces and I want to interate over its contents in a generalized way. So, in general, my interfaces have prototypes like that:</p>
  
  <pre><code>class Interface
  {
  public:
    class Iterator;
    virtual Interface::Iterator* begin() = 0;
    virtual Interface::Iterator* end() = 0;  
  
    class Iterator
    {
    public:
      virtual const Iterator* operator++() = 0;
      virtual bool operator!=(const Iterator&amp; i) = 0;
    };
  };
  </code></pre>
  
  <p>and bellow, is an example of a simple specialization:</p>
  
  <pre><code>class Derived : public Interface
  {
  public:
    Derived() : array {2, 0, 1, 5, 4, 3} {};
    Iterator* begin() { return new Derived::IterDerived(0);};
    Iterator* end() { return new Derived::IterDerived(6);};
  
    int array[6];
  
  public:
    class IterDerived : public Interface::Iterator
    {
    public:
      IterDerived(int i) {pos = i;};
      IterDerived(IterDerived&amp;&amp; i) {pos = i.pos;};
  
      const Interface::Iterator* operator++() override { ++pos; return this;};
      bool operator!=(const Interface::Iterator&amp; i) { return pos != dynamic_cast&lt;const     Derived::IterDerived&amp;&gt;(i).pos;};
      int position() { return pos;};
  
    private:
      int pos;
    };
  };
  </code></pre>
  
  <p>So far, so good. Now, I want to write a piece of code that allow me to iterate over its contents using the new for range, as specified in new standard (c++11). Note that in real interface I will provide getter methods so, using dynamic_cast will be unnecessary. i.e:</p>
  
  <pre><code>int main()
  {
    Interface *a = new Derived();
  
    for(auto i : a) 
      std::cout &lt;&lt; dynamic_cast&lt;Derived*&gt;(a)-&gt;array[dynamic_cast&lt;Derived::IterDerived*&gt;(i.get())-&gt;position()] &lt;&lt; " ";
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>But this code does not compile. In order to compile, I need to use macro in the form of:</p>
  
  <pre><code>#define FOR_ITERATOR(iter, object)                  \
    for(std::unique_ptr&lt;Interface::Iterator&gt; iter((object)-&gt;begin()), \
      iter##end((object)-&gt;end());                 \
        *iter != *(iter##end); ++(*iter))
  </code></pre>
  
  <p>at the place of the for range. Any chances to use the new range for statements, or do I have to use this kind of macro?</p>