|
|
|
|
|
by nkurz
3786 days ago
|
|
If you don't need the return value, the do-while approach works as you suggested: #define bar(...) do { \
const char *_args[] = {NULL, __VA_ARGS__}; \
bar(sizeof(_args)/sizeof(*_args) - 1, _args + 1);\
} while (0)
If for some absurd reason you really do need to emulate a "compound statement expression" in MSVC (because you need to declare some temp variables in a macro that can be used for assignment), it turns out that it can be done with a "lambda expression": #include <stdio.h>
int bar(int n, const char *p[])
{
int total = 0;
for (int i = 0; i < n; i++) {
total += printf("%s\t", p[i]);
}
putchar('\n');
return total;
}
#define bar(...) [](){ \
const char *_args[] = {NULL, __VA_ARGS__}; \
return bar(sizeof(_args)/sizeof(*_args) - 1, _args + 1); \
}()
int main(/* int argc, char **argv */) {
int total = 0;
total += bar();
total += bar("a");
total += bar("a", "b");
total += bar("a", "b", "c");
printf("total: %d\n", total);
return 0;
}
|
|