porting GCC macro to D
Marc Schütz" <schuetzm at gmx.net>
Marc Schütz" <schuetzm at gmx.net>
Fri Apr 4 08:30:33 PDT 2014
On Friday, 4 April 2014 at 13:22:39 UTC, ketmar wrote:
> 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;
>
> <snip>
>
> 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).
You can hack around that limitation if it's acceptable to use a
temporary variable:
enum MYMACRO(alias output) = `func0(); ` ~ output.stringof ~ ` =
func1();`;
...
int a;
mixin(MYMACRO!a);
v = a - 16;
This uses the new enum template syntax available in current DMD,
alternatively use:
template MYMACRO(alias output) {
enum MYMACRO = ...;
}
Unfortunately, you cannot use template mixins for this, because
they can only contain declarations, not statements.
More information about the Digitalmars-d-learn
mailing list