eliminate new operator paraphernalia

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 15 13:37:27 PST 2010


On Sun, 14 Feb 2010 13:17:44 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> 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.
>
> What do you think?

Except for anonymous classes, that works for me.  I don't think I've ever  
used class-specific allocators, and except for slightly ugly syntax, you  
can cover custom allocators with your new syntax.

One thing however, let's look at how the syntax will change:

auto t = new T(x, y, z);

...

auto t = new(customAlloc(???)) T(x, y, z);

Whatever you do for ??? to get the allocation size of T needs to be easy.   
I think right now, it is not a simple property of the class name, I think  
it should be, to make this as painless as possible.  Even with that,  
unfortunately, you must repeat the type, which may be significant if the  
typename is long or complex.

What about a template-ish approach?

auto t = new!(alloc) T(x, y, z);

where this is rewritten by the compiler to do:

auto t = new(alloc!T()) T(x, y, z);

Where alloc is a template function that has one type parameter, and  
returns an address to be passed to placement-new.  This allows  
compile-time introspection (including David's precise scanning feature),  
and avoids the double specification of type.

In essence, I'm adding another way to allocate, but I think it is as  
visible, and as easy to use, as placement new.  Plus it allows any kind of  
introspection you wish to use on allocation.

As far as anonymous classes, how do they clash with anything?  I don't see  
the point of removing them.  Yes, they are used extensively in Java  
because Java lacks true delegates, but there are other reasons to use it  
other than for callbacks.

I also agree with another poster that:

new T[length]

might be better suited as:

new T[](length)

Especially in the wake of T[N] where N is a constant being considered a  
real value type.

-Steve



More information about the Digitalmars-d mailing list