How should I check whether an underlying-type value is an enumerated value?

  • A+
Category:Languages

Let I be some integral type. Now suppose I have a enum class my_enum_class : I, with values which may not be consecutive. And now I get some I value. How do I check whether it's a value enumerated in my_enum_class?

An answer to a similar question (for the C language) makes the assumption that values are contiguous, and that one can add a "dummy" upper-bound value, and check the range between 0 and that value; that's not relevant in my case. Is there another way to do it?

 


There is currently no way to do this.

There are reflection proposals that may make it into and/or that let you iterate (at compile, and hence run, time) over the enumerated values in an enum. Using that the check would be relatively easy.

Sometimes people do manual enum reflection, often using macros.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: