www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - multidim array [] and * overload fail

reply "Ilya Zaitseff" <sark7 mail333.com> writes:
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
Aug 25 2004
parent reply Dave <Dave_member pathlink.com> writes:
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 } void foo(int[] a): 1 void foo(int* a): 2
Aug 26 2004
parent reply "Ilya Zaitseff" <sark7 mail333.com> writes:
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
 }



 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.
Aug 26 2004
parent reply Dave <Dave_member pathlink.com> writes:
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]); } 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
Aug 27 2004
parent "Ilya Zaitseff" <sark7 mail333.com> writes:
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]); } 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.
Aug 29 2004