Last active
October 5, 2019 11:45
-
-
Save dhh1128/36bc220b10f6dafefa33 to your computer and use it in GitHub Desktop.
macros overridden by arg count
This file contains hidden or 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 two overrides that can be used by the expansion of | |
// our main macro. | |
#define _MY_CONCAT3(a, b, c) a b c | |
#define _MY_CONCAT2(a, b) a b | |
// Define a macro that uses the "paired, sliding arg list" | |
// technique to select the appropriate override. You should | |
// recognize this as similar to the GET_NTH_ARG() macro in | |
// previous examples. | |
#define _GET_OVERRIDE(_1, _2, _3, NAME, ...) NAME | |
// Define a macro that concats either 3 or 2 strings together. | |
#define MY_CONCAT(...) _GET_OVERRIDE(__VA_ARGS__, \ | |
_MY_CONCAT3, _MY_CONCAT2)(__VA_ARGS__) | |
int main() { | |
printf("3 args: %s\n", MY_CONCAT("a", "b", "c")); | |
printf("2 args: %s", MY_CONCAT("a", "b")); | |
} | |
// ------ output -------- | |
3 args: abc | |
2 args: ab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment