3

Suppose that I code this:

enum State{
    State0,                //=0
    State1,                //=1
    State2,                //=2
    State5 = State2+3,     //=5
    State6 = State5+1,     //=6
    StateSize = State6+1   //=7
};

How much can I rely on the fact (e.g. StateSize==7)?

More specifically, what is the C++ law/rule to determine what are the values of them?

I feel that there might be a question about this in SO, but I can't find it.

SU3
  • 5,064
  • 3
  • 35
  • 66
javaLover
  • 6,347
  • 2
  • 22
  • 67
  • 1
    Just in case, usually you will want to use enums when you don't want to keep track of the integers they represent. – synchronizer Feb 09 '17 at 04:45
  • @synchronizer I also want to manipulate and compare them. :) – javaLover Feb 09 '17 at 04:46
  • There have to be several questions that cover this in some form or another, for example [this one](http://stackoverflow.com/q/21027298/1708801) – Shafik Yaghmour Feb 09 '17 at 04:49
  • 1
    Note that there's nothing to stop you (apart from common sense) from using `enum State { State0, State1 = -1, State2 = 222, State3, State4 =-1 };` — the values need not be in increasing order, need not be positive, need not be unique. – Jonathan Leffler Feb 09 '17 at 04:49
  • @Shafik Yaghmour Thank! Now, I am sure this question is somehow duplicate. Also, thank Jonathan Leffler, it is new knowledge for me. – javaLover Feb 09 '17 at 04:50
  • Hmmm, [this is an older C++ dup](http://stackoverflow.com/q/34811486/1708801) I am surprised I can't easily find older one but many much older C ones. – Shafik Yaghmour Feb 09 '17 at 04:58

3 Answers3

4

Quoting from cppreference.com

If the first enumerator does not have an initializer, the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one.

SU3
  • 5,064
  • 3
  • 35
  • 66
2

7.2 Enumeration declarations
If the first enumerator has no initializer, the value of the corresponding constant is zero. An enumerator-definition without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one.

You can rely on values incrementing off the previously assigned enum value.

enum State{
    State0,     //=0
    State1,     //=1
    State2,     //=2
    State5=5,   //=5
    State6,     //=6
    StateSize   //=7
};
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
0

pseudo-code:

initial: cnt = 0
for each `enumor` in `enum specifier`
begin
    if `enumor` is in the form of `ID = ConstExp`
        let ID = ConstExp
        cnt = ConstExp
    else
        let ID = cnt
        cnt = cnt + 1
    endif
end
wt.cc
  • 313
  • 1
  • 9
  • set each ID's value – wt.cc Feb 09 '17 at 04:56
  • @MegaStupidMonkeys I am not sure, if anything it may be that this answer is slower than the other two, and it doesn't quote specification as I asked. I can see some effort in this answer, perhaps the effort is even greater than the others, but it takes too much time to chew its knowledge. – javaLover Feb 09 '17 at 09:13
  • @javaLover,I didn't quote anything, it's part of my project "naive-compiler", and I wrote in the form of pseudo code :). – wt.cc Feb 10 '17 at 01:06
  • Don't let the down-votes discourage you from further answering, keep up good work. :) – javaLover Feb 11 '17 at 13:30