Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declearing enum and setting values manually not in increasing order

Tags:

c++

Is the definition of enums in C++ and setting values in random order a valid? Is it used in any well-known code? e.g.:

enum ESampleType{ STPositiveControl='P', STNegativeControl='N', STSample='S' };

VS2008 compiles without warnings. I wonder whether gcc would. In my opinion, it a least harms "least surprise rule" as iterating over all values using

for(int i=STPositiveControl; i<=STSample; ++i)

would fail.

P.S: The rationale for this approach: In my DB application I'm defining wrapper methods. Some columns contain "constants" encoded as single char. I'm trying to make it a little more type safe.

like image 366
Valentin H Avatar asked Oct 23 '25 18:10

Valentin H


1 Answers

It's a standard and widely used feature. Most of the time (but not always) it will be used to create bitmaps, but it can also be used as you show, to give "printable" values to the enum contants.

It shouldn't surprise anyone who knows C++, as it is widely used.

like image 60
James Kanze Avatar answered Oct 26 '25 07:10

James Kanze