eliminate new operator paraphernalia

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Feb 14 12:41:30 PST 2010


Michel Fortin wrote:
> On 2010-02-14 13:17:44 -0500, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> said:
> 
>> Currently new is baroque to the extreme. Should we eliminate the 
>> class-specific allocators in favor of a simple scheme for placement 
>> new? All that's really needed is to construct an object of a given 
>> type at a given address. All of the syntactic mess around it is 
>> unnecessary.
>>
>> I think class-specific new and delete are not a useful feature.
>>
>> Second, the whole new anonymous class thing is for Java's sake. Do you 
>> think we need to keep all that?
>>
>> I suggest the following syntaxes for a type T, an integral length, an 
>> initializerlist a la "e1, e2, e3, ..." that could be empty, and an 
>> addr convertible to void*:
>>
>> new T[length]
>> new T(initializerlist)
>> new(addr) T[length]
>> new(addr) T(initializerlist)
>>
>> and call it a day.
> 
> Hum, what's the syntax for placement delete?

There is no need for placement delete. There will be a function clear() 
in object.d that only calls the destructor. (That is needed regardless.) 
You can carry deallocation with your own API functions.

> While I welcome a real placement-new syntax, I'm not sure adding it 
> justifies any of the other changes you're proposing. Placement-new can 
> be see as just another allocator taking a pointer as an argument and 
> returning that same pointer as the address to use for whatever you're 
> initializing.

Creating an object at a given address is really everything you need from 
the language to do whatever you want.

> So why remove custom allocators? Those can be useful to catch all 
> allocations of a specific class in an existing code base and improve it 
> for some specific needs without having to change code everywhere. I'd 
> rather see that better generalized (we talked about making 'new' a 
> template earlier).

There are many problems with custom allocators. They hook the syntax and 
do something that's unbecoming. new T must place T on the 
garbage-collected heap, period. That gives the guarantee that the object 
has infinite lifetime and therefore dependable behavior. If the same 
syntax can be used to implement various other semantics, you can't count 
on anything in generic code.

> Also, why remove anonymous classes?

Walter thinks we need to keep them.


Andrei



More information about the Digitalmars-d mailing list