Examine the C++ code below. The output is really counterintuitive, at least on Visual Studio 8.0.
#include <iostream>
enum AnEnum
{
E0,
E1,
E2
};
struct S
{
AnEnum e0:2;
AnEnum e1:2;
AnEnum e2:2;
} s;
int main(int argc, char* argv[])
{
using namespace std;
s.e0 = E0;
s.e1 = E1;
s.e2 = E2;
cout << E0 << ", " << E1 << ", " << E2 << endl;
cout << s.e0 << ", " << s.e1 << ", " << s.e2 << endl;
return 0;
}
prints:
0, 1, 2
0, 1, -2
It turns out that Visual Studio's 8.0 compiler represents bit fields of enum types, such
as AnEnum e2:2
with a signed type, but E2
as unsigned. So in this case s.e2 == E2 will result in false!
Both the Visual C++ Team and the folks working with the C++ standard are aware of the issue.