Skip to content

Instantly share code, notes, and snippets.

@dhh1128
dhh1128 / paired_sliding_arg_list_macro_trick_1
Last active October 5, 2019 11:46
the "paired, sliding arg list" macro trick
// Accept any number of args >= N, but expand to just the Nth one. In this case,
// we have settled on 5 as N. We could pick a different number by adjusting
// the count of throwaway args before N. Note that this macro is preceded by
// an underscore--it's an implementation detail, not something we expect people
// to call directly.
#define _GET_NTH_ARG(_1, _2, _3, _4, N, ...) N
// Count how many args are in a variadic macro. Only works for up to N-1 args.
#define COUNT_VARARGS(...) _GET_NTH_ARG(__VA_ARGS__, 4, 3, 2, 1)
@dhh1128
dhh1128 / variadic_macro_1
Created November 21, 2014 20:38
simple variadic macro
#define eprintf(fmt, ...) \
fprintf(stderr, fmt, __VA_ARGS__)
@dhh1128
dhh1128 / fancier-eprintf
Created August 4, 2020 14:41
fancier version of eprintf
#define eprintf(fmt, ...) \
fprintf(stderr, fmt, ##__VA_ARGS__)

Keybase proof

I hereby claim:

To claim this, I am signing this object: