I currently have code with an enum where one value is set and the rest are left to be set by the compiler using the previous value +1, or so I hope.
Is this functionality within an enumerated type compiler dependant, an example is below to clarify.
enum FUNC_ERROR_CODE
{
FUNC_SUCCESS,
FUNC_ERROR_1 = 24,
FUNC_ERROR_2,
FUNC_ERROR_3
}
Is it safe to assume that FUNC_ERROR_2 will have the value 25 and FUNC_ERROR_3 will have the value 26, regardless of compliler used.
I'm coding this so as a function can return an integer value, 0 is always success and any other value can signify failure.
It is safe to assume that:
The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required. An enumerator-definition with = gives the associated enumerator the value indicated by the constant-expression. The constant-expression shall be of integral or enumeration type. 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.
But if you have a typesafe enum: Use it! Don't fall back on converting to/from integers.
IMHO, if your enums must have fixed values, then fix explicitely those values, you'll be sure of the values contained in your enums.
But if you only need to compare your enums each other, let the compiler do it for you.
It should be safe to write some comparisons like this if FUNC_ERROR_2 > FUNC_ERROR_1 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With