Top 5

Steven Schveighoffer schveiguy at yahoo.com
Fri Oct 10 13:42:18 PDT 2008


"Andrei Alexandrescu" wrote
> Steven Schveighoffer wrote:
>> "Andrei Alexandrescu" wrote
>>> Jarrett Billingsley wrote:
>>>> On Fri, Oct 10, 2008 at 11:54 AM, Benji Smith 
>>>> <dlanguage at benjismith.net> wrote:
>>>>
>>>>>> new T[x] is a brain-dead syntax that I wish Walter hadn't imported in 
>>>>>> the
>>>>>> first place.
>>>>> Really? I think it's very valuable.
>>>>>
>>>>> The "new T[x]" syntax lets you construct an array as an RValue. 
>>>>> Without that
>>>>> syntax, you have to declare an array before using it.
>>>> No, what he's getting at is that "new T[x]" does not mean "allocate a
>>>> statically-sized array", it means "allocate a dynamically-sized
>>>> array".  "new T" for any T should mean "allocate a T", not "allocate
>>>> something that's kind of close to a T."
>>>>
>>>> What Andrei is implying, then is that for dynamic arrays, we should
>>>> have to use the (already-legal) "new T[](n)" form, and "new T[x]"
>>>> would mean to allocate a statically-sized array on the heap.
>>> Well yah but I think this will confuse people coming from C++. I just 
>>> wish new was abolished entirely:
>>>
>>> struct S {}
>>> auto a = S();
>>> auto b = Object();
>>> auto c = char[](15);
>>> auto d = char[15]();
>>>
>>> So in general Type followed by "(" ...optional arguments... ")" yields a 
>>> value.
>>
>> to clarify (for myself mostly), you are implying:
>>
>> typeof(a) == S *
>
> typeof(a) = S just like now

Ah, ok.  I thought anything with () was allocated on the heap...

>
>> typeof(b) == Object
>
> yah
>
>> typeof(c) == char[]
>
> yah, and 15 chars get allocated
>
>> typeof(d) == char[15]*
>
> just char[15].

What is the point of that?  Why wouldn't you just say:

char[15] d;

>> Is that correct?
>>
>> What is the syntax for allocating a struct on the stack using a 
>> constructor?
>
> Library call
>
> S * pS = allocate!(S)(... optional args ...);

Ugh.  How many extra 'wrapper' functions are built into the code because of 
this?  I suppose it probably would be inlined.

If new is abolished as a keyword, couldn't we use it instead of allocate?

S * pS = new!(S)(...);

The proposal makes sense, I'd be concerned that syntax like this is 
ambiguous:

auto x = X();
auto y = Y();

You don't know if those are value or reference types, so you don't know how 
they are used.  As opposed to:

auto x = new X();
auto y = Y();

Where you see that x is a reference type.

It might be more confusing to someone who cares about the value semantics.

-Steve






More information about the Digitalmars-d mailing list