[GSoC] Header Generation for C/C++

Eduard Staniloiu edi33416 at gmail.com
Wed Jul 17 13:38:44 UTC 2019


I want to give a heads-up to everyone for the following issue 
(this had me scratching my head for a couple of days unit I 
caught it):

The current code doesn't take into account `enum BaseType`s, ex:
```
enum TOK : ubyte { /* ... */ }

class Expression
{
   TOK op;
   /* ... */
}
```

The `enum TOK` above gets generated as
```
enum TOK { /* ... */ }
```

According to the C standard, the compilers are free to pick the 
type that can fit the enum and most of them will pick `int` as a 
base type; thus `sizeof(TOK) -> 4UL`.
As you can see, this is a problem as the D object files will 
consider `TOK` to be one byte and the C object files will 
consider it to be four bytes.

The manual header implementation did this clever trick to solve 
the problem
```
#typedef unsigned char TOK;
enum
{
   TOKmem1,
   /* ... */
};

class Expression
{
   TOK op;
   /* ... */
}
```

The above takes advantage of the fact that enum member fields are 
in the global namespace, so the values will exist, and since they 
can fit in an unsigned char, the code will work through the 
typedef.

All of the above is required because C++98/03 doesn't have 
support for enum base types.

I have the following question: Can we use C++11 or would that 
break GDC / LDC? With C++11 we could use `enum class` which would 
solve this nicely. The issue with `enum class` is that it will 
break code since not the fields need to be prefixed with the enum 
name.

But still, what do you think? What are the pros and cons of 
supporting/using C++11?



More information about the Digitalmars-d mailing list