Closures in C++
<p>I've found myself in a strange place, mentally. In a C++ project, I long for closures.</p>

<p>Background. There's a Document-type class with a public Render method which spawns a deep call tree. There's some transient state that only makes sense during rendering. Right now it resides in the class like regular member variables. However, this is not satisfactory on some levels - this data only makes sense during a Render call, why store it all the time? Passing it around in arguments would be ugly - there are around 15 variables there. Passing around a structure would add a lot of "RenderState->..." in the lower-level methods.</p>

<p>So what do I want? I want the world, like we all do. Specifically, a set of variables that are:</p>

<ul>
<li>available to some methods in a class (not all of them)</li>
<li>accessible by name alone (no pState->... stuff - so that refactoring is easy)</li>
<li>not copied around on every method call</li>
<li>only live during a method call and up its call tree (assuming trees grow up)</li>
<li>live on a stack</li>
</ul>

<p>I know I can have some of those properties with C++ - but not all of them. Tell me I'm not turning weird.</p>

<p>Heck, in Pascal, of all places, nested functions give you all that...</p>

<p><strong>So what is a good workaround to emulate closures in C++, getting as many of the above benefits as possible?</strong></p>
