title: A C++ iterator adapter which wraps and hides an inner iterator and converts the iterated type
body: <p>Having toyed with this I suspect it isn't remotely possible, but I thought I'd ask the experts. I have the following C++ code:</p>
  
  <pre>
  class IInterface
  {
      virtual void SomeMethod() = 0;
  };
  
  class Object
  {
      IInterface* GetInterface() { ... }
  };
  
  class Container
  {
  private:
      struct Item
      {
          Object* pObject;
          [... other members ...]
      };
      std::list&lt;Item&gt; m_items;
  };
  </pre>
  
  <p>I want to add these methods to Container:</p>
  
  <pre>
      MagicIterator&lt;IInterface*&gt; Begin();
      MagicIterator&lt;IInterface*&gt; End();
  </pre>
  
  <p>In order that callers can write:</p>
  
  <pre>
  Container c = [...]
  for (MagicIterator&lt;IInterface*&gt; i = c.Begin(); i != c.End(); i++)
  {
      IInterface* pItf = *i;
      [...]
  }
  </pre>
  
  <p>So essentially I want to provide a class which appears to be iterating over some collection (which the caller of Begin() and End() is not allowed to see) of IInterface pointers, but which is actually iterating over a collection of pointers to other objects (private to the Container class) which can be converted into IInterface pointers.</p>
  
  <p>A few key points:</p>
  
  <ul>
  <li><code>MagicIterator</code> is to be defined outside <code>Container</code>.
  <li><code>Container::Item</code> must remain private.
  </ul>
  
  <ul>
  <li><code>MagicIterator</code> has to iterate over <code>IInterface</code> pointers, despite the fact that <code>Container</code> holds a <code>std::list&lt;Container::Item&gt;</code>. <code>Container::Item</code> contains an <code>Object*</code>, and <code>Object</code> can be used to fetch <code>IInterface*</code>.
  </ul>
  
  <ul>
  <li><code>MagicIterator</code> has to be reusable with several classes which resemble Container, but might internally have different list implementations holding different objects (<code>std::vector&lt;SomeOtherItem&gt;</code>, <code>mylist&lt;YetAnotherItem&gt;</code>) and with <code>IInterface*</code> obtained in a different manner each time.
  </ul>
  
  <ul>
  <li><code>MagicIterator</code> should not contain container-specific code, though it may delegate to classes which do, provided such delegation is not hard coded to to particular containers inside <code>MagicIterator</code> (so is somehow resolved automatically by the compiler, for example).
  </ul>
  
  <ul>
  <li>The solution must compile under Visual C++ without use of other libraries (such as boost) which would require a license agreement from their authors.
  </ul>
  
  <ul><li>Also, iteration may not allocate any heap memory (so no <code>new()</code> or <code>malloc()</code> at any stage), and no <code>memcpy()</code>.
  </ul>
  
  <p>Thanks for your time, even if you're just reading; this one's really been bugging me!</p>
  
  <p><b>Update: </b>Whilst I've had some very interesting answers, none have met all the above requirements yet. Notably the tricky areas are i) decoupling MagicIterator from Container somehow (default template arguments don't cut it), and ii) avoiding heap allocation; but I'm really after a solution which covers all of the above bullets.</p>