Skip to content

Instantly share code, notes, and snippets.

@xsnpdngv
Last active February 19, 2017 21:19
Show Gist options
  • Save xsnpdngv/5dbac4d757c2b080ec8c53d8107b0fa7 to your computer and use it in GitHub Desktop.
Save xsnpdngv/5dbac4d757c2b080ec8c53d8107b0fa7 to your computer and use it in GitHub Desktop.
C function-like macro overload
/* overload for function-like macros with various number of named arguments */
#define OVERLOAD(Func, .../* Arg1[, Arg2[, Arg3]] */) \
FUNC_SEL(__VA_ARGS__, Func##3, Func##2, Func##1, x)(__VA_ARGS__)
#define FUNC_SEL(Arg1, Arg2, Arg3, Func, ...) Func
#define FUNC(/* arg1[, arg2[, arg3]] */ ...) OVERLOAD(FUNC, __VA_ARGS__)
#define FUNC2(arg1, arg2) arg1#arg2 /* whatever to do with 2 args */
#define FUNC3(arg1, arg2, arg3) arg1#arg2#arg3 /* whatever to do with 3 args */
@xsnpdngv
Copy link
Author

This way FUNC() can be called with 2 or 3 arguments and actually FUNC2() or FUNC3() will be expanded accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment