This question is rather theoretical, I don't have a real life example at hand, it's just somethig I had encountered a couple of times and wondered if there's a generic way to solve.
Let's say I have a program interfacing with a library that exposes an enum. Depending on the library version, some enum members may exist, and some may not.
I want to compile conditional code based on their existence. I know there are other best-practices, like #if-ing on the library version. In some cases, however, it's cleanest and easiest to just check the existence of enums.
Some code I've seen follow this convention when declaring enums:
enum foo {
foo_bar = 1
#define foo_bar foo_bar
foo_baz = 2
#define foo_baz foo_baz
...
};
this lets me use #ifdef foo_XXX to check the existence of a member of enum foo. But it's not always available, of course.
Is there any way to test for the definition of a "name" in the current lexical scope? I'm specifically targeting enums but a more general answer is accepted as well :) I assume there's no "standard C" way to do so, thus GCC-only or clang-only solutions apply as well.
EDIT: After contemplating a bit on it, I implemented a small GCC "builtin" that I called __builtin_compile_time_ifdef. It accepts a name as astring and returns 1 if the name is defined. It works fine. The problem that remains is that in all constructs I know of that provide conditional evaluation (__builtin_choose_expr, _Generic), the non-evaluated branch is still checked for undeclared names. So if I want to use the name (and not just check for existence), I can't.