title: How to Emulate Statement Exprs GNU extension on Standard C++
body: <p>I need to automatically translate from certain language to C++. The source language has the equivalent of <a href="http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs" rel="nofollow">Statement Exprs</a> and I'm having a lot of difficulty replicating that on standard C++ (C++11, actually).</p>
  
  <p>At first I considered lambdas, but the result is very ugly (with lambdas within lambdas within lambdas...) and possibly would bring the compiler to its knees when applied to large sources.</p>
  
  <p>How could I replicate that GNU extension? It is imperative that semantics about construction/destruction/copying/etc are maintained, so translating this:</p>
  
  <pre><code>Foo foo( { ... declarations and statements ... } );
  </code></pre>
  
  <p>into this:</p>
  
  <pre><code>Foo foo;
  ... declarations and statements ...
  foo = last result;
  </code></pre>
  
  <p>is not correct (as <code>Foo</code> is being constructed with default constructor and then assigned, instead of being constructed with the last value of the block of statements. Also, objects created inside the block of statements have a different lifetime on each case.)</p>
  
  <p>Please note that this question also applies for the case where a program that uses that extension needs to be translated to Standard C++.</p>