Suppose i have an enum class
enum class Sectors {
A = 1, B, C, D, E, F, G, H
};
I need to read data from file to assign numeric data to variable of Sectors type. I use this template function to convert from integer to enumeration value.
template <typename Enumeration, typename NumType>
Enumeration NumericToEnum(NumType const value) {
return static_cast<Enumeration>(value);
};
Is there a way to check whether given numeric value is actually in given enumeration range?
Sectors sec1 = NumericToEnum<Sectors>(intValueFromFile); //ok when 2
Sectors sec2 = NumericToEnum<Sectors>(999); //error, out of range
I know how to to check before passing to function (value >= Sectors::A && value <= Sectors::H ), but i would preferably do it inside template function and maybe throw exception. Is it possible?