Another C99 abuse: „named formal parameters“: named formal parameters.
This outputs:
By combining compound literals and __VA_ARGS__ (again!) it is possible to explicitly name function arguments, specify them in arbitrary order, and omit some of them (omitted arguments are initialized by corresponding default initializers).
int foo(int a, char b) { printf("foo: a: %i, b: %c (%i)\n", a, b, b); } #define foo(...) ({ \ struct { \ int a; \ char b; \ } __fa = { __VA_ARGS__ }; \ foo(__fa.a, __fa.b); \ }) int main(int argc, char **argv) { foo(.b = 'b', .a = 42); foo(); }
foo: a: 42, b: b (98) foo: a: 0, b: (0)
By combining compound literals and __VA_ARGS__ (again!) it is possible to explicitly name function arguments, specify them in arbitrary order, and omit some of them (omitted arguments are initialized by corresponding default initializers).
--Tags:-[C]-[Programming]-----------
This would be more impressive without the GCC extension. Simply change your function to take a struct argument (or pointer to struct) and then you can simplify the macro body not to need ({}) hacks.
ReplyDelete