| Thread overview |
|---|
March 08, 2013 typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Hi!
Explain me please,what's wrong with this code:
struct NDimensionalArray(T,alias size)
if(is(typeof(size) _ == int[n],int n) &&
n > 0)
{
static if(n > 1)
NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
else
T m_array[size[0]];
alias m_array this;
}
void main()
{
NDimensionalArray!(int,[2,2]) array;
array[0][1] = 1;
}
It fails with message:
Error: template instance NDimensionalArray!(int, [2, 2]) NDimensionalArray!(int, [2, 2]) does not match template declaration NDimensionalArray(T, alias size) if (is(typeof(size) _ == int[n],int n) && n > 0)
on dmd git-head.
| ||||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zhenya | This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like s[2,2] but nothing has happened on this so far. Bye, bearophile | |||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote:
> This is an answer to just your title question.
>
> A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like s[2,2] but nothing has happened on this so far.
>
> Bye,
> bearophile
Thank you very much.
| |||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:
> On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote:
>> This is an answer to just your title question.
>>
>> A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like s[2,2] but nothing has happened on this so far.
>>
>> Bye,
>> bearophile
>
> Thank you very much.
Although it's a bit strange,since we can always pass heap allocated array by
using new.
| |||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:
Your constraint could be:
if(is(typeof(size) _ == int[]) && size.length > 0)
Also it looks like you are passing 1 too many template args?
static if(n > 1)
NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
^^
| |||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to cal | On Friday, 8 March 2013 at 23:09:07 UTC, cal wrote:
> On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:
>
> Your constraint could be:
>
> if(is(typeof(size) _ == int[]) && size.length > 0)
>
> Also it looks like you are passing 1 too many template args?
>
> static if(n > 1)
> NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
> ^^
Yes,it's a typo.But it seems to be ugly pass dynamically allocated array through
a template parameter,because it's size should be compile-time constant.
| |||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote:
> Yes,it's a typo.But it seems to be ugly pass dynamically allocated array through
> a template parameter,because it's size should be compile-time constant.
Its size is a compile-time constant because it is an array literal. The size is known inside the constraint.
| |||
March 08, 2013 Re: typeof([2,2]) !=? int[2] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to cal | On Friday, 8 March 2013 at 23:18:52 UTC, cal wrote:
> On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote:
>> Yes,it's a typo.But it seems to be ugly pass dynamically allocated array through
>> a template parameter,because it's size should be compile-time constant.
>
> Its size is a compile-time constant because it is an array literal. The size is known inside the constraint.
Yes,really.
I thought that dynamic allocation is runtime operation
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply