quicknir/wise_enum

Partial C compatibility

Closed this issue · 2 comments

Alright, I realize this sounds like a stupid question, but give me a chance. I have a project that is written in C++ but also has to have C compatibility. I love the functionality of wise_enum and would like to use it for the enums in that project. The problem: the enums need to be used in C and C++ contexts.

One option it to do:

enum Foo {
  BAR,
  BAT
}

#ifdef __cplusplus
WISE_ENUM_ADAPT(Foo, BAR, BAT)
#endif //__cplusplus

The result -- in C contexts I just have the plain Foo enum. In C++ contexts I get all the niceties of Wise Enum!

However, there is a downside to this, which is I have to keep two lists of my enums up to date, which I think can easily go wrong and would like to avoid. So, do you think it would be possible to have the WISE_ENUM macro work in C contexts?

I think it is possible if all you want in C is the enum declaration. All you need to do is the same #ifdef __cplusplus trick.

Thoughts? Am I wrong? How hard would this be to implement (I'm willing to do it myself)?

My guess is that, while possible, it would be a real pain to implement.

I think you can do it by defining another macro yourself:

#ifdef __cplusplus
#define MAKE_ENUM(name, ...) WISE_ENUM_ADAPT(name, __VA_ARGS__)
#else
#define MAKE_ENUM(name, ...) enum name { __VA_ARGS__ }
#endif //__cplusplus

Or something along those lines. I'm not sure how easy it will be to get to work but it shouldn't be that bad.