Enum equality test

bearophile bearophileHUGS at lycos.com
Sun Mar 21 19:26:33 PDT 2010

I'm looking for D2 rough edges. I've found that this D2 code compiles and doesn't assert at runtime:

enum Foo { V1 = 10 }
void main() {
    assert(Foo.V1 == 10);

But I think enums and integers are not the same type, and I don't want to see D code that hard-codes comparisons between enum instances and number literals, so I think an equal between an enum and an int has to require a cast:

assert(cast(int)(Foo.V1) == 10); // OK

That has made me curious, so I've tried C++0x, and it seems in C++0x Foo::V1 == 10 gives a compile error ("enum class" is strongly typed enum of C++0x):

enum class Foo { V1 = 10 };
int main() {
    int b = Foo::V1 == 10;

...>g++ -std=c++0x test.cpp -o test
test.cpp: In function 'int main()':
test.cpp:3: error: no match for 'operator==' in '(Foo)10 == 10'
test.cpp:3: note: candidates are: operator==(int, int) <built-in>

Do you think this is worth a bug report (with the "accepts-invalid" keyword)?


More information about the Digitalmars-d-learn mailing list