Created
January 8, 2016 01:11
-
-
Save kaidokert/05f1dd32c3eaf4cffbf5 to your computer and use it in GitHub Desktop.
Portable variadic macros
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
// excellent solution from here : http://stackoverflow.com/a/24028231/295231 | |
//macro glue to make it work on msvc/gcc | |
#define GLUE(x, y) x y | |
#define RETURN_ARG_COUNT(_1_, _2_, _3_, _4_, _5_, count, ...) count | |
#define EXPAND_ARGS(args) RETURN_ARG_COUNT args | |
#define COUNT_ARGS_MAX5(...) EXPAND_ARGS((__VA_ARGS__, 5, 4, 3, 2, 1, 0)) | |
#define OVERLOAD_MACRO2(name, count) name##count | |
#define OVERLOAD_MACRO1(name, count) OVERLOAD_MACRO2(name, count) | |
#define OVERLOAD_MACRO(name, count) OVERLOAD_MACRO1(name, count) | |
#define CALL_OVERLOAD(name, ...) GLUE(OVERLOAD_MACRO(name, COUNT_ARGS_MAX5(__VA_ARGS__)), (__VA_ARGS__)) | |
//usage | |
#define ERROR1(title) printf("Error: %s\n", title) | |
#define ERROR2(title, message)\ | |
ERROR1(title);\ | |
printf("Message: %s\n", message) | |
#define ERROR(...) CALL_OVERLOAD(ERROR, __VA_ARGS__) | |
#define ASSERT1(expr) singleArgumentExpansion(expr) | |
#define ASSERT2(expr, explain) twoArgumentExpansion(expr, explain) | |
#define ASSERT(...) CALL_OVERLOAD(ASSERT, __VA_ARGS__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment