dimanche 26 août 2018

Check at compile time that multiple enums have unique values

I want to list error codes using multiple enums, so that I can define those enums in different files. How do I check at compile time that all values in these enums are unique?

I am currently defining enums like this:

constexpr int ERROR_CODE_MAX = 1000000;

#define ERRORS1_LIST(f) \
    f(IRRADIANCE_MUST_BE_BETWEEN, 103, L"message1") \
    f(MODULE_MUST_BE_SELECTED, 104, L"message2")

#define GENERATE_ENUM(key, value, name) key = value,
#define GENERATE_LIST(key, value, name) { key, name },

enum Errors1 {
    ERRORS1_LIST(GENERATE_ENUM)
    UndefinedError1 = ERROR_CODE_MAX - 1
};

// Error code 103 is defined twice; should trigger compile error
#define ERRORS2_LIST(f) \
    f(OPERATOR_MUST_BE_SELECTED, 105, L"message3")  \
    f(IRRADIANCE_MUST_BE_BETWEEN2, 103, L"message4")

enum Errors2 {
    ERRORS2_LIST(GENERATE_ENUM)
    UndefinedError2 = ERROR_CODE_MAX - 2
};

// List of all error messages
// I want to check error code uniqueness in the same place where I define this
static const std::map<int, std::wstring> ErrorMessageList = {
    ERRORS1_LIST(GENERATE_LIST)
    ERRORS2_LIST(GENERATE_LIST)
    {UndefinedError1, L"Undefined"}
};

Aucun commentaire:

Enregistrer un commentaire