Skip to content

Instantly share code, notes, and snippets.

@glejeune
Created November 22, 2012 16:18
Show Gist options
  • Save glejeune/4131931 to your computer and use it in GitHub Desktop.
Save glejeune/4131931 to your computer and use it in GitHub Desktop.
C/C++ useful macros
#define CONCATENATE(arg1, arg2) arg1##arg2
#define IN_NARG(...) IN_NARG_(__VA_ARGS__, IN_RSEQ_N())
#define IN_NARG_(...) IN_ARG_N(__VA_ARGS__)
#define IN_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
#define IN_RSEQ_N() 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define IN_1(x, v, ...) ((v) == (x))
#define IN_2(x, v, ...) ((v) == (x)) || IN_1(x, __VA_ARGS__)
#define IN_3(x, v, ...) ((v) == (x)) || IN_2(x, __VA_ARGS__)
#define IN_4(x, v, ...) ((v) == (x)) || IN_3(x, __VA_ARGS__)
#define IN_5(x, v, ...) ((v) == (x)) || IN_4(x, __VA_ARGS__)
#define IN_6(x, v, ...) ((v) == (x)) || IN_5(x, __VA_ARGS__)
#define IN_7(x, v, ...) ((v) == (x)) || IN_6(x, __VA_ARGS__)
#define IN_8(x, v, ...) ((v) == (x)) || IN_7(x, __VA_ARGS__)
#define IN_9(x, v, ...) ((v) == (x)) || IN_8(x, __VA_ARGS__)
#define IN_10(x, v, ...) ((v) == (x)) || IN_9(x, __VA_ARGS__)
#define IN_(N, x, v, ...) CONCATENATE(IN_, N)(x, v, __VA_ARGS__)
#define IN(x, n, ...) IN_(IN_NARG(n, __VA_ARGS__), x, n, __VA_ARGS__)
#define FOREACH(item, array) \
for(int keep=1, \
count=0,\
size=sizeof (array)/sizeof *(array); \
count != size; \
keep=1, count++) \
for(item = (array)+count; keep; keep = !keep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment