質問

I've heard before that I should simply let the compiler choose which values to assign for enumerated constants if I'm not doing something clever like using the values as bitmasks. If I'm just using enumeration values for more explicit code documentation, are there any gotchas that could creep in if I don't explicitly define all the values? I believe that values are assigned in ascending order. Should I define the 1st value to ensure the same values for each successive compilation?

役に立ちましたか?

解決

I think it will depend on your use of the type.

Advantages to not providing enum initializers:

  • Easier to add and remove values as you develop.
  • You won't accidentally define two identifiers with the same value.

Advantages to providing enum initializers:

  • Safe to serialize and deserialize the data. If you need to put these values in a file, network socket, etc., I would go ahead and write the values.
  • Easier to look the values up for debugging purposes.

他のヒント

From C99, Section 6.7.2.2p3:

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.)

The only time you need to assign a value to the first enumerator is if you want its value to be different from 0. Usually, you wouldn't do that, since the enumerators are often used to index an array.

Below is a simple example of "adding 1 to the value of the previous".

enum {
  A,
  B,
  C = 0,
  D,
  E
};

int main ()
{
    printf("%d\n", A);
    printf("%d\n", B);
    printf("%d\n", C);
    printf("%d\n", D);
    printf("%d\n", E);
    return 0;
}

The output for the above program is:

0
1
0
1
2

The only "gotcha" that I can think of is that if you want multiple enum names to represent the same value, you should remember to either stick them right next to the item you alias, or at the bottom of the enum list. I prefer the latter so I can count the enum lines to predict what the enum values will be.

enum {
    A,
    B,
    E,

    /* aliases below */
    C = A,
    D = B
};

Otherwise, an enumerator is just like an int literal.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top