Structs as reference types?
evilrat
evilrat666 at gmail.com
Sun May 30 05:37:02 UTC 2021
On Sunday, 30 May 2021 at 00:39:59 UTC, tsbockman wrote:
> 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.
extern(C++) class doesn't have monitor so it's just single
pointer size for typeinfo.
```d
extern(C++)
class test
{
}
pragma(msg, __traits(classInstanceSize, test)); // 8
pragma(msg, (void*).sizeof); // 8
pragma(msg, __traits(allMembers, test)); // tuple() no members
at all
```
about allmembers, D class usually have these `"tuple("toString",
"toHash", "opCmp", "opEquals", "Monitor", "factory")"` and size
of 16 (one extra pointer) for monitor.
More information about the Digitalmars-d
mailing list