I need a syntax that allows me to launch a block of code with a previous initialization of some variables, and after the block of code is executed I need to execute some code to deallocate some other variables.
So the code written inside the block should be executed in the middle of some other operations.
Here is what I got:
#define application(block) new Application(&argc, argv); auto __code= []() block ; __code(); mainApplication->launch()
The code that you see just initializes an application (a C++ object), so I use it this way:
application({
< write the code >
});
This way I am sure that the application is always initialized, and launched after the block.
But I wonder if there is a way to keep away the round parenthesis, so that I can write it this way:
application{
< write the code >
};
Also this way works but it should make the syntax lighter.
You can hide for-loops in a macro to achieve this.
A call to this macro, followed by a block (or single statement), will insert the declaration
declwith that block as its scope, and evaluatepreexprbefore andpostexprafter the block:This is C but should work in C++ too; of course, using macros is usually considered to be bad C++ style. The
_loopvariable and the loops should be optimized away by a good compiler.