is_set()

From vegard.wiki
Revision as of 11:56, 1 March 2020 by Vegard (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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