extern(C) enum
Timothy Foster via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Sep 15 19:21:02 UTC 2017
On Friday, 15 September 2017 at 15:35:48 UTC, bitwise wrote:
> On Friday, 15 September 2017 at 07:24:34 UTC, Jonathan M Davis
> wrote:
>> On Friday, September 15, 2017 04:15:57 bitwise via
>> Digitalmars-d-learn wrote:
>>> I translated the headers for FreeType2 to D, and in many
>>> cases, enums are used as struct members.
>>>
>>> If I declare an extern(C) enum in D, is it guaranteed to have
>>> the same underlying type and size as it would for a C
>>> compiler on the same platform?
>>
>> extern(C) should have no effect on enums. It's for function
>> linkage, and enums don't even have an address, so they don't
>> actually end up in the program as a symbol. And since C's int
>> and D's int are the same on all platforms that D supports
>> (we'd have c_int otherwise, like we have c_long), any enum
>> with a base type of int (which is the default) will match
>> what's in C.
>>
>> - Jonathan M Davis
>
> I'm confused...is it only C++ that has implementation defined
> enum size? I thought that was C as well.
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.
A D int is always the same size as a C int because C ints are 4
bytes on 32bit and above architectures and D doesn't support
architectures below 32bit so you never run into a case where a C
int is 2 bytes.
D can't guarantee that the size of an extern(C) enum will match
an arbitrary C compiler's choice, so I'm pretty sure it'll just
default to a D int.
It's further likely that padding in a struct will differ between
C compilers so if you need a D struct to be the same size as a C
struct in every case... welp that's not exactly going to be fun.
More information about the Digitalmars-d-learn
mailing list