How to Emulate Statement Exprs GNU extension on Standard C++
I need to automatically translate from certain language to C++. The source
language has the equivalent of Statement Exprs and I'm having a lot of
difficulty replicating that on standard C++ (C++11, actually).
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.
How could I replicate that GNU extension? It is imperative that semantics
about construction/destruction/copying/etc are maintained, so translating
this:
Foo foo( { ... declarations and statements ... } );
into this:
Foo foo;
... declarations and statements ...
foo = last result;
is not correct (as Foo 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.)
Please note that this question also applies for the case where a program
that uses that extension needs to be translated to Standard C++.
No comments:
Post a Comment