extern(C) enum

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 15 22:44:03 UTC 2017


On Friday, 15 September 2017 at 19:35:50 UTC, nkm1 wrote:
> On Friday, 15 September 2017 at 19:21:02 UTC, Timothy Foster 
> wrote:
>> I believe C enum size is implementation defined. A C compiler 
>> can pick the underlying type (1, 2, or 4 bytes, signed or 
>> unsigned) that fits the values in the enum.
>
> No, at least, not C99. See 6.4.4.3: "An identifier declared as 
> an enumeration constant has type int". You must be thinking 
> about C++.

Thanks - this works for me. The bindings are for an open source C 
library. So I guess I'm safe as long as I can be sure I'm using a 
C99 compiler and strongly typing as int in D.

C++ seems to be a much more complicated situation, but it appears 
that for 'enum class' or 'enum struct' the underlying type is 
int, even when it's not specified.


§ 7.2:

[1] "The enum-keys enum class and enum struct are semantically 
equivalent; an enumeration type declared with one of these is a 
scoped enumeration, and its enumerators are scoped enumerators."

[2] "For a scoped enumeration type, the underlying type is int if 
it is not explicitly specified."

[1][2] 
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

Shame that even relatively new C++ code tends to use unscoped 
enums.



More information about the Digitalmars-d-learn mailing list