Created
October 6, 2016 23:31
-
-
Save Enelar/f17479cab879a04739c4feb794907741 to your computer and use it in GitHub Desktop.
Preprocessor foreach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define EMPTY() | |
#define DEFER(func) func EMPTY() | |
#define EVAL(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__))) | |
#define EVAL1(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__))) | |
#define EVAL2(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__))) | |
#define EVAL3(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__))) | |
#define EVAL4(...) EVAL5(EVAL5(EVAL5(__VA_ARGS__))) | |
#define EVAL5(...) __VA_ARGS__ | |
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__ | |
#define IIF(c) PRIMITIVE_CAT(IIF_, c) | |
#define IIF_0(t, ...) __VA_ARGS__ | |
#define IIF_1(t, ...) t | |
#define SECOND(a, b, ...) b | |
#define IS_PROBE(...) DEFER(SECOND)(__VA_ARGS__, 0) | |
#define NOT(x) IS_PROBE(_NOT_ ## x) | |
#define _NOT_0 ~, 1 | |
#define _NOT_ ~, 1 | |
#define FOREACH(METHOD, ...) EVAL(DEFER(FOREACH_CONTINUE)(METHOD, __VA_ARGS__)) | |
#define IF_NOT_ZERO(A) DEFER(IIF)(NOT(A)) | |
#define FOREACH_CONTINUE(METHOD, A, ...) IF_NOT_ZERO(A) (, POP_FIRST(METHOD, A, __VA_ARGS__)) | |
#define POP_FIRST(METHOD, A, ...) METHOD(A) DEFER(POP_FIRST_RESHEDULE)()(METHOD, __VA_ARGS__) | |
#define POP_FIRST_RESHEDULE() FOREACH_CONTINUE | |
#define ECHO(a) a + | |
int f() | |
{ | |
int a = FOREACH(ECHO, 1, 2, 3, 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment