Last active
February 15, 2018 18:27
-
-
Save isaac-ped/3c9065338fa63449c95279e65073117c to your computer and use it in GitHub Desktop.
Macro fanciness for detecting disabled feature
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
#include <stdio.h> | |
#define STRINGIFY(X) "" #X | |
#define STRINGIFY2(X) STRINGIFY(X) | |
#define IF_NOT_DISABLED(ID, FN, ...) \ | |
(!strcmp("DISABLE_" #ID, STRINGIFY2(DISABLE_##ID))) ? FN(ID, ##__VA_ARGS__) : 0 | |
enum my_enum { | |
FOO, | |
BAR, | |
QUX | |
}; | |
#define DISABLE_BAR | |
int func(enum my_enum id, char *name) { | |
printf("Enum %s has value %d\n", name, id); | |
} | |
#define func_wrapper(id, name) IF_NOT_DISABLED(id, func, name) | |
int main(int argc, char **argv) { | |
func_wrapper(FOO, "FOO"); | |
func_wrapper(BAR, "BAR"); // This line will be compiled out if -O3 enabled | |
func_wrapper(QUX, "QUX"); | |
} | |
//// OUTPUTS: | |
// > Enum FOO has value 0 | |
// > Enum QUX has value 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment