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 <vector>#include <list>#include <iostream>#include <unordered_map>std::unordered_map<void *, bool> _advance_iterator__instances_done;template <class Tc, class Ti>bool advance_iterator(Tc &container, Ti &iterator){   static typename Tc::iterator it;   if (_advance_iterator__instances_done[&container])   {       it = container.begin();      _advance_iterator__instances_done[&container] = false;   }      if (it == container.end())   {       _advance_iterator__instances_done[&container] = true;         return false;   }     iterator = *it;   it++;    return true;   }#define foreach(value_type, iterator, container) \_advance_iterator__instances_done[&container] = true; \for (value_type iterator; advance_iterator(container, iterator); )int main(int argc, char *argv[]){   std::vector<int> vec;   std::list<int> lis;   std::list<std::list<int>> matrix;    // populate containers    for (int i = 0; i < 10; i++)   {      static std::list<int> row;       vec.push_back(i);      lis.push_back(i + 10);      for (int j = 1; j <= 10; j++)         row.push_back(j * i);      matrix.push_back(row);      row.clear();       }     // regular foreach test     std::cout << vec: << std::endl;         foreach(int, value, vec)      std::cout << value << std::endl;     std::cout << std::endl << half of lis: << std::endl;    // breaking test    foreach(int, value, lis)   {       std::cout << value << std::endl;      if (value == 14)         break;       }     // regular again    std::cout << std::endl << full lis: << std::endl;   foreach(int, value, lis)      std::cout << value << std::endl;    // nested test   std::cout << std::endl << matrix: << std::endl;    foreach(std::list<int>, row, matrix)   {      foreach(int, value, row)         std::cout << value <<  ;       std::cout << std::endl;   }         return 0;}
Polymorphic STL foreach without passing the container type
c++;template;iterator