A
AngleWyrm
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T,
section 4.8 (pp 77):
"A value of integral type may be explicitly converted to an enumeration
type. The result of such a conversion is undefined unless the value is
within the range of the enumeration. For example:
enum flag { x=1, y=2, z=4, e=8 }; // range 0:15
flag f1 = 5; // type error: 5 is not of type flag
flag f2 = flag(5); // ok: flag(5) is of type flag and withing the range of
flag"
C++ Draft 2, December 1996, Section 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."
In both of these old references, an explicit conversion from integer (or
another enumeration) to an enumeration is allowed as long as the value falls
within the range of the enumeration. The first text even offers up the
obvious bug, and states that this is ok!
Consider what we are representing with an enumeration: At first glance, it
appears to be a set, useful for categorizing/subdividing some domain, with
the implicit purpose of later performing a switch/case or (less optimally)
if/then against the categories.
However,
enum TEST_T {FIRST = 0, SECOND = 0, THIRD = 100};
TEST_T eTest = FIRST, eSecondTest = SECOND;
if ( eTest == SECOND ){
cout << "faulty logic!" << endl;
}
if( eTest == eSecondTest ){
cout << "more confusion" << endl;
}
int iTest = 5;
eTest = (TEST_T) iTest; // which comes to what?
switch (eTest){
case FIRST:
case SECOND: // compiler catches what?: Duplicate case value
case THIRD:
cout << "success" << endl;
break;
default:
cout << "switch failure" << endl;
}
As you can see, several disasterous thinking processes are opened up.
Firstly, it doesn't seem to be in line with the mathematical concept of an
unordered set, nor does it seem to be a strictly ordered set, due to
duplicate values--which cause serious problems. Furthermore, explicit
conversion to an enumeration is allowed without having anything to do with
members of that enumeration! That's just absurd.
A tighter definition of what we are talking about when we refer to an
enumeration is in order, and thus I ask you, the programming world: What is
an enumeration?
section 4.8 (pp 77):
"A value of integral type may be explicitly converted to an enumeration
type. The result of such a conversion is undefined unless the value is
within the range of the enumeration. For example:
enum flag { x=1, y=2, z=4, e=8 }; // range 0:15
flag f1 = 5; // type error: 5 is not of type flag
flag f2 = flag(5); // ok: flag(5) is of type flag and withing the range of
flag"
C++ Draft 2, December 1996, Section 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."
In both of these old references, an explicit conversion from integer (or
another enumeration) to an enumeration is allowed as long as the value falls
within the range of the enumeration. The first text even offers up the
obvious bug, and states that this is ok!
Consider what we are representing with an enumeration: At first glance, it
appears to be a set, useful for categorizing/subdividing some domain, with
the implicit purpose of later performing a switch/case or (less optimally)
if/then against the categories.
However,
enum TEST_T {FIRST = 0, SECOND = 0, THIRD = 100};
TEST_T eTest = FIRST, eSecondTest = SECOND;
if ( eTest == SECOND ){
cout << "faulty logic!" << endl;
}
if( eTest == eSecondTest ){
cout << "more confusion" << endl;
}
int iTest = 5;
eTest = (TEST_T) iTest; // which comes to what?
switch (eTest){
case FIRST:
case SECOND: // compiler catches what?: Duplicate case value
case THIRD:
cout << "success" << endl;
break;
default:
cout << "switch failure" << endl;
}
As you can see, several disasterous thinking processes are opened up.
Firstly, it doesn't seem to be in line with the mathematical concept of an
unordered set, nor does it seem to be a strictly ordered set, due to
duplicate values--which cause serious problems. Furthermore, explicit
conversion to an enumeration is allowed without having anything to do with
members of that enumeration! That's just absurd.
A tighter definition of what we are talking about when we refer to an
enumeration is in order, and thus I ask you, the programming world: What is
an enumeration?