Custom delete not getting called

Daniel Gibson metalcaedes at gmail.com
Fri Jun 24 22:52:36 PDT 2011


Am 25.06.2011 07:50, schrieb Ali Çehreli:
> On Sat, 25 Jun 2011 07:36:21 +0200, Daniel Gibson wrote:
> 
>> Am 25.06.2011 06:33, schrieb Ali Çehreli:
>>> On Sat, 25 Jun 2011 06:24:31 +0200, Andrej Mitrovic wrote:
>>>
>>>> These are going away, I don't know why they're still being kept in the
>>>> docs.
>>>
>>> If I understood correctly, you mean that the custom new and delete
>>> operators for user defined types are going away? If so, I am wasting my
>>> time writing something about those. :)
>>>
>>> Is there a replacement for the custom new and delete?
>>
>> emplace() (and clear() to call the destructor)
> 
> I am aware of the discussions about delete going away. I always thought 
> that it was about the 'delete o;' usage that was going away.
> 
> emplace() is to construct an object at a location that you already have.
> 
> clear() is to call the destructor.
> 
> Custom new and delete are (were?) different:
> 
> - Custom new gets called to give you a chance to allocate the memory for 
> an object (Note that this is different than emplace() where you want to 
> construct the object on memory that is already available.)
> 
> - Custom delete is the reverse of custom new. (And this is not clear(), 
> as when custom delete is called, the object has already been destroyed.)
> 

emplace() and clear() are the functions you'll use in your custom new
and delete implementation :-)
See http://pastebin.com/9Qgf3vc7 for a  simple example.

Of course this is not 100% the same thing as custom new/delete was, but
it gives you more flexibility, e.g. you can use it with any class (not
just custom classes) and you can even use several different allocators
(including D's default garbage collected new) at the same time for the
same class (of course then you need to be really careful to use the
proper deallocator).

Cheers,
- Daniel

> I want to ask again: Are custom new and delete going away? Oh! Let me 
> check TDPL... I can't find those there. I guess they are deprecated. 
> Cool! Less to write about. :)
> 
> Ali



More information about the Digitalmars-d mailing list