eliminate new operator paraphernalia

Michel Fortin michel.fortin at michelf.com
Sun Feb 14 12:26:39 PST 2010


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?

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.

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).

Also, why remove anonymous classes? They're just a shortcut syntax for:

	void func() {
		class Anonymous {
			...
		}
		auto a = new Anonymous;
		...
	}

I don't really see what's the problem in allowing this:

	void func() {
		auto a = new class {
			...
		}
		...
	}

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list