"Value class instance" pattern?

bearophile bearophileHUGS at lycos.com
Sat Jul 13 09:49:51 PDT 2013


Benjamin Thaut:

> Yes the assignment in this(DefaultCtor) is needed, because the 
> construction process of a D object is defined as:

OK.


> The debugging values are needed because most debuggers won't be 
> able to evaluate properties while debugging.

OK.


Is that ClassCompose destructor enough (with something added for 
the debug build)?

     ~this() {
         if (!isDestructed) {
             _instance.destroy;
             isDestructed = true;
         }
     }

Trying to use ClassCompose in my code I have had some problems 
caused by const classes and ClassCompose dtor. Maybe such dtor 
(and isDestructed) can be versioned out for composed-in classes 
that only contain values...



> Yes this looks pretty similar to scoped in phobos but I always 
> thought that scoped was inteded for objects to be placed on the 
> stack instead of inside other objects.

Right. I think Scoped can't be used for class composition.
But isn't such ClassCompose enough for both purposes? Or is it 
better to have in Phobos both Scoped and something similar to 
ClassCompose?


> Thats also why scoped does some additional alignment which is 
> garantueed when you do it within a class.

Maybe such alignment can be added to the ClassCompose too.


> Also from reading the sourcecode of scoped, doesn't the address 
> of the scoped object change between construction and 
> destruction? Because it is created inside the scoped function 
> and then returned, which will do a move. And then later 
> destroyed at its final destination. Wouldn't that break with 
> interior pointers?

Code like Scoped or ClassCompose is fit for Phobos because it's 
commonly useful, and because similar questions show that it's far 
from obvious code.

Perhaps Andrei can offer us some guidance.

Bye,
bearophile


More information about the Digitalmars-d mailing list