destructor, postblit constructor --- force calling always

Benjamin Thaut code at benjamin-thaut.de
Sun Mar 30 12:41:48 PDT 2014


Am 30.03.2014 21:35, schrieb Carl Sturtivant:
>
> Context: struct values that are (non-ref) parameters & local variables.
>
> Is there a way to arrange that a particular struct's special
> constructors (postblit,destructor) should always be called even on move,
> or on destruction of a default initialized value, temporary or not,
> etcetera, i.e. ensure they should always be called?
>
> Is a struct's destructor always called when it goes out of scope, even
> if it is default initialized?
>

D's structs don't have identity. That means, they can be moved without 
notice at any point in the program. AFAIK the compiler even does that 
when handling exceptions in a few cases (e.g. with structs on the 
stack). Having moveable value types allows for a lot of optimizations, 
both on the compiler side as well as when implementing e.g. containers 
in user code. So no there is no way to always get notified. I also 
proposed a move constructor in the past, but the idea was not well 
recieved. When I needed a move constructor, usually adding another level 
of indirection solved the problem.

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d-learn mailing list