title: Ruby blocks, java closures in C++
body: <p>I am developing a program where I find myself doing this like this a lot:</p>
  
  <pre><code>void Model::SetCollideMode( const std::string &amp;m )
  {
    Body *body;
  
    std::map&lt;std::string, Body* &gt;::iterator iter;
  
    for (iter=this-&gt;bodies.begin(); iter!=this-&gt;bodies.end(); iter++)
    {
      body = iter-&gt;second;
  
      body-&gt;SetCollideMode( m );
    }
  }
  </code></pre>
  
  <p>I have several methods like that in several object that basically apply a property to all its children. Coming from Ruby world I am dying to do something like:</p>
  
  <pre><code>for_all_bodies{ body-&gt;SetCollideMode(m) }
  </code></pre>
  
  <p>There is anyway to make this code more closures like or in other way improve it?</p>
  
  <p>I am conscious of how C++ works, that it is stack based and there is no context information to create a perfect closure like functionality (this need a VM?) but at least improve over the current repeat this code 100 times kind of programming.</p>