We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello everyone,
is it possible to test an enum for each value just appearing once. Example:
enum foo { BAR = 0, BAZ = 0 };
Is it possible to create an error or warning with this?
Thanks for your answers Alexander
But there is nothing erroneous about it as far as the language is concerned - so nothing to report.
I just wondered if there was any compiler specific flag.
Just as there should be compiler flags to warn if any integer gets the value 123456?
If you want non-overlapping enumeration values, the natural way is to write:
enum { Ben, John, Charlie, }
or with an initial start value:
enum { Red = 1, Green, Blue };
If you on the other hand decides to explicitly assign a numeric value to multiple entries, then it's 100% your task to decide what values to assign. And consider the implications of the selected numerical values.
The language do not care about overlapping numerical values, just as preprocessor-based code:
#define VOLTAGE 1 #define CURRENT 2 #define TEMPERATURE 3
wouldn't care if two #define expands to the same numerical value.
If you have a problem with overlapping numerical values, then the obvious choice is to stop assign explicit values and let the compiler auto-enumerate.