Are D classes proper reference types?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Thu Jun 24 06:50:44 UTC 2021


D classes are distinct from structs because they are intended to 
be bound to a reference (pointer) and not addressed as a value 
(inline/copying).

But for efficiency reasons, scoped classes can stack-allocate, 
but my understanding is that the compiler can still allocate it 
on the GC heap? So it is still a reference type, I guess?

But how about "emplace", does "emplace" imply the the compiler 
cannot put it on the GC heap? Yes, I am aware that this is 
library construct, but still relevant.

The reason I am asking this is because implementing reference 
counting in a clean fashion requires classes to be proper 
reference types, so that means disabling scoped classes and 
emplaced classes, and only allow it where a reference/pointer to 
a class never exceed a reference count of 1.

The core of my question is this: is it at all reasonable for 
"user code" to assume that a class is not allocated on the heap? 
Is it reasonable to write code that assumes that it is allocated 
"inline" as with emplace? Because if it is, then then the 
advantages of having classes as reference types and making them 
different from structs are lost.



More information about the Digitalmars-d-learn mailing list