www.digitalmars.com         C & C++   DMDScript  

D.gnu - enum is int or uint?

reply "shinichiro.h" <s31552 mail.ecc.u-tokyo.ac.jp> writes:
Hi,

When I compile the following code with DMD, it does not fail. But with
GDC, it fails:

int main() {
    enum E { A, B };
    E e = -E.B;
    assert(e < 0);
    return 0;
}

That's because DMD treats enum as int but GDC may treat as uint.
The language specification of D says the behavior of DMD is correct.

  The EnumBaseType is the underlying type of the enum. It must be an integral
  type. If omitted, it defaults to int.

http://digitalmars.com/d/enum.html

------------------
 shinichiro.h
Nov 03 2004
next sibling parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
Seems strange to me that you're allowed to do "operator -" on an enum at 
all.
The result is not a valid value for enum E.

Lio. 
Nov 11 2004
parent Thomas =?UTF-8?B?S8O8aG5l?= <thomas-dloop kuehne.cn> writes:
added to DStress as:

http://svn.kuehne.cn/dstress/run/enum_09.d

shinichiro.h schrieb am Freitag, 12. November 2004 15:32:
 So I refined my code.
 
 int main() {
     enum E { A = -1 };
     E e = E.A;
     assert(e < 0);
     return 0;
 }

Nov 14 2004
prev sibling parent "shinichiro.h" <s31552 mail.ecc.u-tokyo.ac.jp> writes:
 Seems strange to me that you're allowed to do "operator -" on an enum at 
 all.
 The result is not a valid value for enum E.

I think enum of C and D is just one of the integral type and IMHO my code is legal. But there is not the essence of the problem. So I refined my code. int main() { enum E { A = -1 }; E e = E.A; assert(e < 0); return 0; }
Nov 12 2004