Structs as reference types?

tsbockman thomas.bockman at gmail.com
Sun May 30 00:39:59 UTC 2021


On Saturday, 29 May 2021 at 18:40:40 UTC, Alexandru Ermicioi 
wrote:
> On Saturday, 29 May 2021 at 18:34:44 UTC, sighoya wrote:
>> What about:
>>
>> ```D
>> ptr struct S
>> {
>>      int i;
>> }
>> // ...
>> ```
>
> That is literally a final class:
> ```D
> final class S {
> int s;
> }
> ```

No, a final class in D still has a `size_t.sizeof * 2` header 
containing a virtual function table pointer and a monitor, 
because all `extern(D)` classes are implicitly derived from 
`Object`:

```D
struct SS
{
	int i;
}
pragma(msg, SS.sizeof); // 4
pragma(msg, is(SS : Object)); // false

final class CS {
     int i;
}
pragma(msg, __traits(classInstanceSize, CS)); // 20 (with 64-bit 
architecture)
pragma(msg, is(CS : Object)); // true
```

Aside from the wasted space, the `final class` version isn't 
compatible with `extern(C)` interfaces like the `struct` version 
is.


More information about the Digitalmars-d mailing list