title: How does one manage a self-referring cross-scope lambda?
body: <p>Assume that I have a functionality which I want to call whenever a timer finishes. I have put that piece of functionality in a lambda function. Furthermore, in that function, I may wish to set another timer to call that same lambda on another, later occasion.</p>
  <pre><code>void doSetupThingsInSomeDecoupledCodeOrWhatever() {
      std::function&lt;void(float)&gt; semiRecursiveFunc;
      semiRecursiveFunc = [&amp;semiRecursiveFunc](float deltaT){
          if (whatever()) {
              // Do something...
          }
          else {
              // Do something else, then:
              float durationMS = getRNGSystem().getUniformFloat(1000.0f, 5000.0f)
              // Gives the timer a duration to wait, and a function to run at the end of it.
              getTimerSystem().setNewTimer(durationMS, semiRecursiveFunc);
          }
      };
  
      float durationMS = getRNGSystem().getUniformFloat(1000.0f, 5000.0f)
      // Gives the timer a duration to wait, and a function to run at the end of it.
      getTimerSystem().setNewTimer(durationMS, fooLambda);
  }
  </code></pre>
  <p>Now, clearly this won't work, because <code>semiRecursiveFunc</code> is tied to the scope of <code>doSetupThingsInSomeDecoupledCodeOrWhatever</code>, and when the timer system tries to run it the function will no longer exist and everything will disintegrate into a spectacular ball of flame.</p>
  <p>What's the best way to manage this? I can't store <code>semiRecursiveFunc</code> in a pointer because one can't declare lambdas like that, as far as I can tell. Is there some common tool for this sort of persistent-lambda use-case? What's the least ugly approach, with minimum surrounding infrastructure? Is there a best-practice to follow, some relevant tool I've missed? Any suggestions or recommendations would be much appreciated.</p>