View mode: basic / threaded / horizontal-split · Log in · Help
January 02, 2012
Array of array
auto r = new int[][5];
this is ok

auto r = new int[][];
this is not ok
Error: new can only create structs, dynamic arrays or class objects
, not int[][]'s

why?
January 02, 2012
Re: Array of array
On 01/02/2012 11:04 PM, RenatoL wrote:
> auto r = new int[][5];
> this is ok
>
> auto r = new int[][];
> this is not ok
> Error: new can only create structs, dynamic arrays or class objects
> , not int[][]'s
>
> why?

What would you expect the code to do?
What you are trying to achieve is similar to:

class Array(T){this(size_t length){...}}
auto r = new Array!(Array!int); // error: missing constructor argument
January 02, 2012
Re: Array of array
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?
January 02, 2012
Re: Array of array
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
January 02, 2012
Hole of new? (Re: Array of array)
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
January 02, 2012
Re: Array of array
I have:

auto r = new int[][];

Error: new can only create structs, dynamic arrays or class objects
, not int[][]'s

while

auto r = new int[][3];

is ok.
January 02, 2012
Re: Array of array
On 01/03/2012 12:03 AM, RenatoL wrote:
> I have:
>
> auto r = new int[][];
>
> Error: new can only create structs, dynamic arrays or class objects
> , not int[][]'s
>
> while
>
> auto r = new int[][3];
>
> is ok.

new int[][3] is an alternate form of new int[][](3); new int[][3] 
allocates an int[][] with 3 default-initialized elements.
January 02, 2012
Re: Hole of new? (Re: Array of array)
On 01/03/2012 12:02 AM, Mafi wrote:
> 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

Yes, but you can use (new T[][1]).ptr; to allocate T[] itself (and get a 
T[]*). Maybe new T[] should be interpreted as allocating a T[] and give 
back a T[]*.
January 02, 2012
Re: Array of array
On 3 January 2012 00:27, Timon Gehr <timon.gehr@gmx.ch> wrote:

> On 01/03/2012 12:03 AM, RenatoL wrote:
>
>> I have:
>>
>> auto r = new int[][];
>>
>> Error: new can only create structs, dynamic arrays or class objects
>> , not int[][]'s
>>
>> while
>>
>> auto r = new int[][3];
>>
>> is ok.
>>
>
> new int[][3] is an alternate form of new int[][](3); new int[][3]
> allocates an int[][] with 3 default-initialized elements.
>

I assume `int[][] sth;` does what RenatoL wants to accomplish by `auto sth
= new int[][];`. I do however have a question:
is `auto sth = new int[][5];` the same as `int[][5] sth;`? If not, what is
the difference?
January 02, 2012
Re: Array of array
RenatoL wrote:

> Error: new can only create structs,
> dynamic arrays or class objects, not int[][]'s

There is an error in the error message:
 new can only create _static_ arrays.

-manfred
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home