is_set()

From vegard.wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

The following macro checks if another macro is set to 1 and works both in #if preprocessor expressions and in actual C code:

// https://stackoverflow.com/a/10119699/1697183
// https://news.ycombinator.com/item?id=3830609
// https://github.com/torvalds/linux/commit/69349c2dc01c489eccaa4c472542c08e370c6d7e
#define is_set(macro) is_set_(macro)
#define macrotest_1 ,
#define is_set_(value) is_set__(macrotest_##value)
#define is_set__(comma) is_set___(comma 1, 0)
#define is_set___(_, v, ...) v