porting GCC macro to D

ketmar ketmar at ketmar.no-ip.org
Fri Apr 4 06:22:37 PDT 2014


i have some C code like this:

void func0 (void) { … }
int func1 (void) { … return smth+42; }

#define MYMACRO()  ({ func0(); func1(); })

int v;
…
v = MYMACRO()-16;


what it does? it uses GCC extension "statement expression" which 
allows to place almost arbitrary C operator block inside any 
expression. result of evaluating this "operator block" will be 
value of the last operator in it (return value of func1() in my 
case). this block can contain ifs, switches, local vars, local 
function declarations and so on.

in D we have no "statement expressions" in this form, afaik, and 
comma operator is very limited (and will be deprecated anyway). 
how can i reproduce such thing in D? i.e. i want to write 
something like:

v = mixin(MYMACRO())-16;

and have the same effect as in C code: all MYMACRO content (this 
can be almost anything) *always* inlined. the only thing i was 
able to think out is to generate nested functions with immediate 
call in MYMACRO, but it will not guarantee inlining (i.e. i must 
trust compiler intellgence here).


More information about the Digitalmars-d-learn mailing list