is struct delete deterministic? (cf used in Unique)

weaselcat via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 8 00:47:14 PST 2015


On Sunday, 8 March 2015 at 01:20:57 UTC, Timothee Cour wrote:
> To clarify, I'm only asking about a struct allocated via new.
> Unique!T is wrapped around a struct, but it allocates a struct 
> T via 'new',
> so my question still holds: does 'delete t' (where t is a 
> struct allocated
> via new) guarantee deterministic destruction?
>
> I'm guessing yes, otherwise Unique would be broken, but where 
> is that
> specified in the docs?
> And if delete is to be deprecated (according to the docs), what 
> is the
> correct way to do that (despite fact that Unique relies on 
> delete).
>
>
> On Sat, Mar 7, 2015 at 4:02 PM, weaselcat via 
> Digitalmars-d-learn <
> digitalmars-d-learn at puremagic.com> wrote:
>
>> On Saturday, 7 March 2015 at 23:48:39 UTC, Timothee Cour wrote:
>>
>>> I'm a little confused about the following:
>>> clear,delete,destroy.
>>> My understanding is that clear is deprecated and delete is 
>>> planned to be
>>> deprecated, so we should only ever use destroy (which 
>>> deterministic calls
>>> the destructor but doesn't release memory).
>>>
>>> Unique uses delete however in the destructor. Is that still 
>>> guaranteeing
>>> deterministic destruction when the uniqued element is either 
>>> a class or
>>> struct? (ie if the destructor has a file handle resource, 
>>> will it be
>>> deterministically freed?)
>>>
>>
>> structs are allocated on the stack(unless instantiated with 
>> new), and call
>> their destructor when you leave their scope. Unique still 
>> guarantees
>> deterministic destruction because it's wrapped around a 
>> struct, it's a
>> fairly common 'D idiom' I'd say(i.e, look at how File is 
>> implemented - D's
>> runtime and standard library are surprisingly well documented 
>> and easy to
>> read.)
>>
>> I'm not sure why Unique uses delete, might just be bitrot.

Unique!T allocates via new, but itself is meant to be allocated 
on the stack. When it leaves the scope it calls delete on its 
contents(if they're still valid - i.e, haven't been moved to a 
different Unique!T)

delete guarantees deterministic destruction because structs 
guarantee deterministic destruction, and Unique!T itself is a 
struct.

http://dlang.org/cpptod.html#raii

To answer your questions,
1. Yes, Unique!T guarantees deterministic destruction of T(unless 
there are bugs I'm unaware of.)
2. It's on the deprecated page. 
http://dlang.org/deprecate.html#delete


More information about the Digitalmars-d-learn mailing list