Defining a function to return members of a C++ enum definition, as part of a preprocessor macro?

171 views Asked by At

Given an enumeration defined as:

DEFINE_ENUM(name,enumerators)  \
enum name                                                              \
{                                                                      \
    BOOST_PP_SEQ_ENUM(enumerators)                                     \
}; 

How do I define a function which will be defined as part of the DEFINE_ENUM() macro and return a vector containing all members of the enumeration?

The following, added to the end of the macro definition above, is as close as I've come so far:

std::vector<name> MembersOf(name AnyItem) {     \
    return {BOOST_PP_SEQ_ENUM(enumerators)}; \
};

This is reporting invalidly-terminated macro definitions, though, and claiming that every DEFINE_ENUM()'s MembersOf() is multiply defined if the file containing it is included in multiple files.

0

There are 0 answers