Polymorphic STL foreach without passing the container type I was trying to figure out how to make a breakable foreach macro for STL containers and I came up with this method that uses templates to recognize the container type automatically. Are there any performance / code-wise improvements you can think of?

// polymorphic break-able foreach test that works with nested calls on the same type of container

#include &lt;vector&gt;
#include &lt;list&gt;
#include &lt;iostream&gt;
#include &lt;unordered_map&gt;

std::unordered_map&lt;void *, bool&gt; _advance_iterator__instances_done;

template &lt;class Tc, class Ti&gt;
bool advance_iterator(Tc &amp;container, Ti &amp;iterator)
{
   static typename Tc::iterator it;

   if (_advance_iterator__instances_done[&amp;container])
   { 
      it = container.begin();
      _advance_iterator__instances_done[&amp;container] = false;
   }   

   if (it == container.end())
   { 
      _advance_iterator__instances_done[&amp;container] = true;   
      return false;
   }  

   iterator = *it;
   it++; 

   return true;   
}

#define foreach(value_type, iterator, container) \
_advance_iterator__instances_done[&amp;container] = true; \
for (value_type iterator; advance_iterator(container, iterator); )

int main(int argc, char *argv[])
{
   std::vector&lt;int&gt; vec;
   std::list&lt;int&gt; lis;
   std::list&lt;std::list&lt;int&gt;&gt; matrix; 

   // populate containers 
   for (int i = 0; i &lt; 10; i++)
   {
      static std::list&lt;int&gt; row; 

      vec.push_back(i);
      lis.push_back(i + 10);

      for (int j = 1; j &lt;= 10; j++)
         row.push_back(j * i);

      matrix.push_back(row);
      row.clear();    
   }  

   // regular foreach test  
   std::cout &lt;&lt; "vec:" &lt;&lt; std::endl;      

   foreach(int, value, vec)
      std::cout &lt;&lt; value &lt;&lt; std::endl;  

   std::cout &lt;&lt; std::endl &lt;&lt; "half of lis:" &lt;&lt; std::endl; 

   // breaking test 
   foreach(int, value, lis)
   { 
      std::cout &lt;&lt; value &lt;&lt; std::endl;

      if (value == 14)
         break;    
   }  

   // regular again 
   std::cout &lt;&lt; std::endl &lt;&lt; "full lis:" &lt;&lt; std::endl;

   foreach(int, value, lis)
      std::cout &lt;&lt; value &lt;&lt; std::endl; 

   // nested test
   std::cout &lt;&lt; std::endl &lt;&lt; "matrix:" &lt;&lt; std::endl; 

   foreach(std::list&lt;int&gt;, row, matrix)
   {
      foreach(int, value, row)
         std::cout &lt;&lt; value &lt;&lt; " "; 

      std::cout &lt;&lt; std::endl;
   }      

   return 0;
}
This line gives my nightmares:

std::unordered_map&lt;void *, bool&gt; _advance_iterator__instances_done;


First up the name is reserved. Prefer not to use identifiers without a leading underscore (the rules are not trivial and everybody gets them wrong so just avoid them).

Secondly the size of the map is unbound. If you have a really large application (runs a long time) this is going to continuously grow.

This construct

bool advance_iterator(Tc &amp;container, Ti &amp;iterator)
{
   static typename Tc::iterator it;


makes the whole thing not thread safe.

Your naming convention makes it confusing to read:

#define foreach(value_type, iterator, container) \
                           ^^^^^^^^^^^
                     This is never the iterator it is the value.


Overall I think the boost FOREACH is a better choice for C++03 and C++11 now has a built in version