Mircea Ispas
Mircea Ispas

Reputation: 20780

Question about enums in C++

I tried to use an enum in a for loop like this:

enum foo
{
    foo_0, 
    foo_1,
    foo_2,
    foo_3,
    ...
    foo_n,
    foo_count
};

for(foo f = foo_0; f < foo_count; ++f) 

and I had a compilation error. I understand that this is not valid because ++f might not be a valid foo enum - not in this case but in general case so I switched the for loop to this:

for(foo f = foo_0; f < foo_count; f = foo(f+1)) 

which compiles fine. But this rises the fallowing question. What happens if I have the fallowing statement?

foo f = foo(k); //k is not a valid foo value

Is this undefined behavior?

EDIT: k is an int and it hasn't any corresponding value in foo

EDIT2:

enum foo
{
    foo_0, 
    foo_1,
    foo_2,
    foo_3
};

foo f = foo(100); //what value will have f after this according to the standard

Thanks for help!

Upvotes: 4

Views: 264

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361322

I had a compilation error. I understand that this is not valid because ++f might not be a valid foo enum.

No. That is wrong interpretation of the error. It doesn't compile because there is no operator++ defined for foo type.

If you define operator++ as:

foo & operator++(foo & f) 
{  
   return f = foo(f+1); 
}

Then ++f would compile and work : http://ideone.com/1GG09

And as for foo(f+1) (or foo(k)), then that is okay. Internally,foo is an integral type. And it's value can be anything which can be represented by the underlying integral type.

§7.2/6 says,

For an enumeration where emin is the smallest enumerator and emax is the largest, the values of the enumeration are the values of the underlying type in the range bmin to bmax, where bmin and bmax are, respectively, the smallest and largest values of the smallest bit-field that can store emin and emax) It is possible to define an enumeration that has values not defined by any of its enumerators.


EDIT:

 foo f = foo(100); //what value will have f after this according to the standard

I think the behavior is unspecified here, as the Standard says in §7.2/9,

An expression of arithmetic or enumeration type can be converted to an enumeration type explicitly. The value is unchanged if it is in the range of enumeration values of the enumeration type; otherwise the resulting enumeration value is unspecified.

For enumeration range, see the previous quotation.

Upvotes: 7

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506897

It's defined behavior. The value that ends up in f is unspecified if k is outside the range of values of foo and is the same as k if it is inside of the range of values of foo.

The range of foo is 0 .. 2^(ld(n+2)+1) - 1. That is, the values of a bitfield that can store all the values of your enum constants (enumerators).

Upvotes: 4

DrYap
DrYap

Reputation: 6647

In that case f would have the value k but this doesn't correspond to a label in the enum. Think of the enum as an unsigned int as the data type (unless set otherwise) and the identifiers inside the enum as constants.

Upvotes: 0

Related Questions