I searched a bit here on SO and was surprise that I didn't find any similar question. Happy for any hints in case this has already been answered.
I have a codebase with a lot of enum classes defined. Some of them specify a totalNum constant like
enum class Foo : int
{
a,
b,
c,
totalNum
}
Others don't have this like
enum class Bar : bool
{
oneOption,
otherOption
}
Then I have a function basically like this
template <class EnumClassType>
EnumClassType typeToEnum (typename std::underlying_type<EnumClassType>::type value)
{
// If you hit this assertion, the value is outside of the valid enum range
assert (isPositiveAndBelow (value, decltype (value) (EnumClassType::totalNum)));
return EnumClassType (value);
}
While this works and makes sense for the an enum with totalNum
specified, I'd like to skip this assert in case there is no such identifier in the enum. Is there any way to do that? The codebase is currently using C++ 14 but C++ 17 solutions are also welcome due to an upcoming compiler change.