4

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.

Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • 1
    Yes it is valid. It is used for example by Bjarne Stroustrup in his book _The C++ Programming Language_ for a simple arithmetic expression parser example code. You just can't iterate on it (the parser used a `switch`). – gx_ Jul 19 '13 at 14:42
  • 1
    In C++11, you could even specify the storage type of the `enum` to be `char`... – Yakk - Adam Nevraumont Jul 19 '13 at 14:48
  • @Yakk But you normally wouldn't want to. Most of the time, there's no real reason to do so (except maybe that it is necessary if you want to forward declare an enum). – James Kanze Jul 19 '13 at 14:50
  • @JamesKanze: The reason could be to spare memory or align data, I suppose. – Valentin H Jul 19 '13 at 15:17
  • @gx_: Indeed! Section 6.1.1. Thanks for referring to Stroustrup! If you make it as Answer, I'll select it. – Valentin H Jul 19 '13 at 15:27

2 Answers2

5

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • yea but using this kind of enum in for cycle, i wouldn't recommend it atleast when it's taken out of scope like that – Opsenas Jul 19 '13 at 14:35
  • 1
    @Opsenas In general, you shouldn't count on being able to iterate through an enum's values. If it's truly an enumerated type, then the have no order (from a logical standpoint), and if the enum is used for bit fields or such, iterating makes no sense either. – James Kanze Jul 19 '13 at 14:49
  • 1
    @JamesKanze: Thank you. 'you shouldn't count on being able to iterate through an enum's values' - I very-very often relied on that and failed first time (in C++ since 1998) as I declared that enum like above myself :-( – Valentin H Jul 19 '13 at 15:13
4

Yes it is valid.

There is an example use of it in the book The C++ Programming Language (3rd Edition) by Bjarne Stroustrup, section "6.1 A Desk Calculator [expr.calculator]" (and more precisely "6.1.1 The Parser [expr.parser]"), for the parser code of a simple arithmetic calculator. Here's an excerpt:

The parser uses a function get_token() to get input. The value of the most recent call of get_token() can be found in the global variable curr_tok. The type of curr_tok is the enumeration Token_value:

enum Token_value {
    NAME,       NUMBER,     END,
    PLUS='+',   MINUS='-',  MUL='*',    DIV='/',
    PRINT=';',  ASSIGN='=', LP='(',     RP=')'
};
Token_value curr_tok = PRINT;

Representing each token by the integer value of its character is convenient and efficient and can be a help to people using debuggers. This works as long as no character used as input has a value used as an enumerator – and no character set I know of has a printing character with a single-digit integer value. (...)

(That last sentence is specific to this example, because the enumeration mixes "default-value" and "explicit-value" enumerators and wants each one to be unique.)

However it's only an educative example (and notably it uses a global variable, and CAPS names for enumerators when you should reserve them for macros (but Stroustrup doesn't like macros :p)).

Now, indeed you can't iterate over it (at least with a plain for loop; but see this question). (And as James Kanze pointed, an enum's values are not always ordered, contiguous, and unique.)

Community
  • 1
  • 1
gx_
  • 4,690
  • 24
  • 31