I'm writing an EnumSet
class for a project. I would like to have an EnumSet::AllOf()
method if possible, like in Java. The obvious way to go about this is to iterate over every member of the std::underlying_type
and try casting it to the enum class, then go from the first element where this is possible to the last.
You are not given what the lowest or highest element of the enum is, so you must search the entire space.
Is this even possible? If so, how?
EDIT FOR CLARITY: I'm looking for something along the lines of
try {
std::dynamic_cast<std::underlying_type<E>::type>(e);
return true; // Cast succeeded, enum is valid
} catch (SOME_KIND_OF_EXCEPTION e) {
return false; // Cast failed, enum is invalid
}