On Thursday, 14 February 2013 at 10:58:19 UTC, Namespace wrote:
struct S
{
static struct Payload
{
//Tons of data here
}
Payload* _p;
//fonctions
}
Ref counted is implemented that way. most of the containers
are also implemented that way. associative arrays are also
implemented that way under the hood.
But you have to allocate '_p' again on the heap.
Well, yeah, that's the point. I'm not saying it's a best fit for
everything. You are basically trading construction costs for copy
costs.
I see no advantage over classes, except that these structures
are just not null by default.
Actually, (and IMO, this is a very big problem), these structures
are *always* null by default. There is no easy way to "default
initialize" structs in D :(
Is that the advantage?
The advantage is deterministic RAII. With classes you only get
non-deterministic RAII.
For example: File. File will close the underlying FILE* when the
last File is destroyed. But no later.