Hole of new? (Re: Array of array)

Mafi mafi at example.org
Mon Jan 2 15:02:03 PST 2012


Am 02.01.2012 23:33, schrieb Timon Gehr:
> On 01/02/2012 11:21 PM, RenatoL wrote:
>> Just curious... the answer of the compiler it's a bit unclear to
>> me...
>>
>> T[] is a dynamic array of type T.
>> T[][] is a dynamic array of T[]. But this doesn't work. Why?
>
> It does work. Why do you think it does not?
>
> T[] a; // ok
> T[][] b; // ok
> auto c = new T[5]; // ok
> auto d = new T[][5]; // ok
> auto e = new T[]; // fail, nonsensical
> auto f = new T[][]; // fail, nonsensical
>

Here we come to an interesting point I often thought of. How do you 
allocate a T[] itself (so you get a T[]*) or a ClassType reference (so 
you get a ClassType*) on the heap (without casting)?
As far as I know it's not possible with new.
new T[n] is of type T[].
new T[]* is of type T[]**.
new ClassType is of type ClassType.
new ClassType* is of type ClassType**.

Is this a Hole of new?

Mafi


More information about the Digitalmars-d-learn mailing list