|
|
|
|
|
by jstimpfle
1619 days ago
|
|
That's a little bit like saying, it's impossible to write functioning code because you could make a mistake. But an abstraction that can be misused might still be an abstraction worth using. Here is a macro that can be used to emulate a defer #define CONCAT_(a, b) a ## b
#define CONCAT(a, b) CONCAT_(a, b)
#define UNIQUENAME() CONCAT(i_, __LINE__)
#define SCOPE_(counter, init_stmt, exit_stmt) for (int counter = ((init_stmt), 1); counter--; (exit_stmt))
#define SCOPE(init_stmt, exit_stmt) SCOPE_(UNIQUENAME(), (init_stmt), (exit_stmt))
Granted it's a hack, but it can be useful at times. I've used something like it to define a large data hierarchy in code for example, as having to close all the nodes manually is tedious.You could wrap a second for-loop around the definition of the macro to at least be able to catch misplaced "break" statements. |
|