Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

enums

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?

like image 651
einpoklum Avatar asked Nov 07 '18 13:11

einpoklum


1 Answers

There is currently no way to do this.

There are reflection proposals that may make it into c++20 and/or c++23 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.

like image 109
Yakk - Adam Nevraumont Avatar answered Oct 26 '22 16:10

Yakk - Adam Nevraumont