title: How to immediately invoke a C++ lambda?
body: <p>A constructor from a class I'm inheriting requires a non-trivial object to be passed in.  Similar to this:</p>
  
  <pre><code>MyFoo::MyFoo() : SomeBase( complexstuff )
  {
      return;
  }
  </code></pre>
  
  <p>The <code>complexstuff</code> has little to do with <code>MyFoo</code>, so I didn't want to have to pass it in.</p>
  
  <p>Instead of writing some kind of 1-off temporary function that returns <code>complexstuff</code> I used a lambda.  What took me a few minutes to figure out is I have to <em>invoke</em> the lambda.  So my code now looks like this:</p>
  
  <pre><code>MyFoo::MyFoo() : SomeBase(
      []()
      {
          /* blah blah do stuff with complexstuff */
          return complexstuff;
      } () )
  {
      return;
  }
  </code></pre>
  
  <p>If you didn't catch it, it is subtle.  But after the lambda body, I had to put <code>()</code> to tell the compiler to immediately "run" the lambda.  Which made sense after I figured out what I had done wrong.  Otherwise, without the <code>()</code> to invoke the lambda, gcc says something similar to this:</p>
  
  <pre><code>error: no matching function for call to 'SomeBase(&lt;lambda()&gt;)'
  </code></pre>
  
  <p>But now that has me thinking -- did I do this correctly?  Is there a better way in C++11 or C++14 to tell the compiler that I want it to immediately invoke a lambda I've written?  Or is appending an empty <code>()</code> like I did the usual way to do this?</p>