[Issue 3999] New: Enum equality to an int

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 22 07:04:29 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3999

           Summary: Enum equality to an int
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-03-22 07:04:27 PDT ---
This D2 code compiles and doesn't assert at runtime (tested with dmd 2.042,
that's absent in the versions list in this page):


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


But enums are not integers, and a language has to discourage hard-coded
comparisons between enum instances and number literals, so I think it's better
to require a cast to compare an enum to an int:

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


Note: in C++0x Foo::V1 == 10 is a compile error, enum and int can't be
compared:


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


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>

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list