mercredi 3 juin 2015

How can I ensure that each case defined in an enum class is treated, e.g. using static_assert?

Assume an enum class

enum class MY_ENUM {FOO, BAR};

and a std::map which holds some information about each element in the enum

map<MY_ENUM, MyInfoType> enumInfos;

How can I ensure at compile time that for each value of the enum there is an entry in the map?

I would like to be able to write something like (of course this code is not valid):

for (auto& elem : MY_ENUM) {
    static_assert(enumInfos.find(elem) != enumInfos.end(),
        "Error: Information for an element in MY_ENUM is missing.")
}

Is something like this possible?

Aucun commentaire:

Enregistrer un commentaire