Let's say I have the following code:
// exposition-only, I can't use this macro in my solution
#if defined(HAS_MY_ENUM)
enum my_enum {
zero,
one,
};
#endif
enum empty {
placeholder, // an enum must always have a value
};
I'd love to have a conditional type alias my_enum_or_empty
that is set to my_enum
if it is defined, otherwise it's set to empty
, e.g.:
using my_enum_or_empty = std::conditional<my_enum_exists, my_enum, empty>;
I want a SFINAE-based solution that gives me my_enum_exists
.
I was thinking of this approach, but it requires a forward-declaration of my_enum
, and it cannot be forward declared, because it is sizeless. And I cannot add a size, because this enum
comes from an external library. Do you have any ideas?
Restrictions
My use case is that this my_enum
is defined in an external C library that I don't have control over. I need to compile my code against two versions of this library (with my_enum
and without). The header sources, where my_enum
is defined cannot be changed i.e. it must be an old C-style enum
.
The library version is not known at compile time. Sadly, there is no LIBRARY_VERSION
macro. I need to rely on this enum
alone.
I cannot rely on the build system. In fact, I am doing a C++ header-only wrapper around this C code, hence there is no build system. Neither can I move responsibility to my customer. There are more than one type missing and there are many versions of this library, i.e. it'd be too complex for my wrapper user.