extern(C) enum

bitwise bitwise.pvt at gmail.com
Sun Sep 17 19:16:06 UTC 2017


On Sunday, 17 September 2017 at 18:44:47 UTC, nkm1 wrote:
> On Sunday, 17 September 2017 at 17:06:10 UTC, bitwise wrote:
>> [...]
>
> Just put the burden on the users then. It's implementation 
> defined, so they are in position to figure it out...

This isn't something that can really be done with bindings, which 
are important for D to start really picking up speed.

If someone goes to code.dlang.org and decides to download some 
FreeType2 bindings, they should just work. The memory corruption 
bugs that could occur due to binary incompatibility with some 
random copy of the original C library would be extremely hard to 
diagnose. They would also undermine the memory safety that a lot 
of people depend on when using D.

> for example, gcc: "Normally, the type is unsigned int if there 
> are no negative values in the enumeration, otherwise int. If 
> -fshort-enums is specified, then if there are negative values 
> it is the first of signed char, short and int that can 
> represent all the values, otherwise it is the first of unsigned 
> char, unsigned short and unsigned int that can represent all 
> the values. On some targets, -fshort-enums is the default; this 
> is determined by the ABI."
> https://gcc.gnu.org/onlinedocs/gcc-6.4.0/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html#Structures-unions-enumerations-and-bit-fields-implementation
>
> msvc++: "A variable declared as enum is an int."
> https://docs.microsoft.com/en-us/cpp/c-language/enum-type

I was starting to think along these lines as well. With respect 
to the above, I'm wondering if something like this could be done:

`
template NativeEnumBase(long minValue, long maxValue)
{
     static if(platform A)
     {
         static if(minValue < 0) // need signed?
         {
             static if(maxValue > int.max) // need long?
                 alias NativeEnumBase = long;
             else
                 alias NativeEnumBase = int;
         }
         else
         {
             static if(maxValue > uint.max) // need long?
                 alias NativeEnumBase = ulong;
             else
                 alias NativeEnumBase = uint;
         }
     }
     else static if(platform B)
     {
         // etc...
         alias NativeEnumBase = long;
     }
     else {
         static assert("unsupported compiler");
     }
}

enum Some_C_Enum_ : NativeEnumBase!(-1, 2)
{
     SCE_INVALID = -1,
     SCE_ZERO = 0,
     SCE_ONE = 1,
     SCE_TWO = 2,
}
`

So the question is, is there a way from inside D code to 
determine what the native enum size would be for a given set of 
min and max enum values? While C and C++ do not specify enum 
size, are there platform or compiler level specifications we 
could rely on?

> It's probably pretty safe to assume it's an int; people who 
> play tricks with "-fshort-enums" deserve what's coming to them 
> :)

Agreed ;)




More information about the Digitalmars-d-learn mailing list