Thread overview
multidim array [] and * overload fail
Aug 26, 2004
Ilya Zaitseff
Aug 27, 2004
Dave
Aug 27, 2004
Ilya Zaitseff
Aug 27, 2004
Dave
Aug 29, 2004
Ilya Zaitseff
August 26, 2004
void foo(int[] a) {}
void foo(int* a) {}

void main()
{
  int[2][2] a;
  foo(a[0]); // error
}

[WinXP DMD 0.100] output:

function foo overloads void(int[]a) and void(int*a) both match argument list for foo
August 27, 2004
Ilya Zaitseff wrote:

> void foo(int[] a) {}
> void foo(int* a) {}
> 
> void main()
> {
>    int[2][2] a;
>    foo(a[0]); // error
> }
> 
> [WinXP DMD 0.100] output:
> 
> function foo overloads void(int[]a) and void(int*a) both match argument
> list for foo

Are you saying the error message is confusing? If so I agree, otherwise I guess I'm confused.

//void foo(int[] a) {}
//void foo(int* a) {}
void foo(int[] a) { printf("void foo(int[] a): %d\n",a[1]); }
void foo(int* a) { printf("void foo(int* a): %d\n",*a); }

void main()
{
// int[2][2] a;
// foo(a[0]); // error
   int[][] a; // dynamic array
   a.length = 2;
   a[0].length = 2;
   a[0][] = 1;
   a[1].length = 2;
   a[1][] = 2;
   foo(a[0]); // Ok
   foo(&a[1][0]); // Ok
}

# dmd zz.d
# ./zz
void foo(int[] a): 1
void foo(int* a): 2

August 27, 2004
Dave wrote:

> Are you saying the error message is confusing? If so I agree, otherwise I
> guess I'm confused.
>
> //void foo(int[] a) {}
> //void foo(int* a) {}
> void foo(int[] a) { printf("void foo(int[] a): %d\n",a[1]); }
> void foo(int* a) { printf("void foo(int* a): %d\n",*a); }
>
> void main()
> {
> // int[2][2] a;
> // foo(a[0]); // error
>    int[][] a; // dynamic array
>    a.length = 2;
>    a[0].length = 2;
>    a[0][] = 1;
>    a[1].length = 2;
>    a[1][] = 2;
>    foo(a[0]); // Ok
>    foo(&a[1][0]); // Ok
> }
>
> # dmd zz.d
> # ./zz
> void foo(int[] a): 1
> void foo(int* a): 2
>

It is bug of static arrays, not dynamic.

If change my example:

void foo(int[] a) { printf("%d", a.length}
//void foo(int* a) {}

void main()
{
  int[3][2] a;
  foo(a[0]);
}

After run, it output: 3

i.e., compiler correctly pass length of inner array to foo().
So, foo(int[] a) must have bigger priority to be selected than foo(int* a), and ambiguity must not appear.
August 27, 2004
In article <opsddfer1taaezs2@ilya.tec.amursk.ru>, Ilya Zaitseff says...
>
>Dave wrote:
>
>It is bug of static arrays, not dynamic.
>
>If change my example:
>
>void foo(int[] a) { printf("%d", a.length}
>//void foo(int* a) {}
>
>void main()
>{
>   int[3][2] a;
>   foo(a[0]);
>}
>
>After run, it output: 3
>
>i.e., compiler correctly pass length of inner array to foo().
>So, foo(int[] a) must have bigger priority to be selected than foo(int*
>a), and ambiguity must not appear.

In that case, then I don't think this is a bug.. I see what you are saying here,
but both signatures for foo(...) are pretty much ambiguous for a[0] (for a
static array). I have not seen anything in the D docs. that specify that a[0]
must resolve to the first foo() given the choice between the two.

If there is something documented that I'm not aware of, please pass it on and ignore the following.

To get rid of the ambiguity, the first foo() could be changed to foo(int[2] a)
or the call to foo() argument could be cast:

void foo(int[] a) { printf("%d\n",a[1]); }
void foo(int* a) { printf("%d\n",*a); }
void foo(int[2] a) { printf("%d\n",a[1]); }

void main()
{
int[2][2] a;

a[0][0] = 10;
a[0][1] = 20;
a[1][0] = 30;
a[1][1] = 40;

foo(cast(int*)a[0]);
foo(cast(int[])a[0]);
foo(a[1]);
}

# dmd zz.d
# ./zz
10
20
40

I think the error message could be a little clearer <g>, but from what I can tell it is correctly reporting the issue. Passing object a[0] into either definition of foo() is also legal and consistent with the array assignment syntax described here, unless I'm missing something:

http://digitalmars.com/d/arrays.html

- Dave


August 29, 2004
Dave wrote:

> In article <opsddfer1taaezs2@ilya.tec.amursk.ru>, Ilya Zaitseff says...
>>
>> Dave wrote:
>>
>> It is bug of static arrays, not dynamic.
>>
>> If change my example:
>>
>> void foo(int[] a) { printf("%d", a.length}
>> //void foo(int* a) {}
>>
>> void main()
>> {
>>   int[3][2] a;
>>   foo(a[0]);
>> }
>>
>> After run, it output: 3
>>
>> i.e., compiler correctly pass length of inner array to foo().
>> So, foo(int[] a) must have bigger priority to be selected than foo(int*
>> a), and ambiguity must not appear.
>
> In that case, then I don't think this is a bug.. I see what you are saying here,
> but both signatures for foo(...) are pretty much ambiguous for a[0] (for a
> static array). I have not seen anything in the D docs. that specify that a[0]
> must resolve to the first foo() given the choice between the two.
>
> If there is something documented that I'm not aware of, please pass it on and
> ignore the following.
>
> To get rid of the ambiguity, the first foo() could be changed to foo(int[2] a)
> or the call to foo() argument could be cast:
>
> void foo(int[] a) { printf("%d\n",a[1]); }
> void foo(int* a) { printf("%d\n",*a); }
> void foo(int[2] a) { printf("%d\n",a[1]); }
>
> void main()
> {
> int[2][2] a;
>
> a[0][0] = 10;
> a[0][1] = 20;
> a[1][0] = 30;
> a[1][1] = 40;
>
> foo(cast(int*)a[0]);
> foo(cast(int[])a[0]);
> foo(a[1]);
> }
>
> # dmd zz.d
> # ./zz
> 10
> 20
> 40
>
> I think the error message could be a little clearer <g>, but from what I can
> tell it is correctly reporting the issue. Passing object a[0] into either
> definition of foo() is also legal and consistent with the array assignment
> syntax described here, unless I'm missing something:
>
> http://digitalmars.com/d/arrays.html
>
> - Dave
>
>

Maybe you right, Dave :)

Implicit cast to [] for inner static array can be perfomance-costly, so
explicit cast is trade-off solution, i think.