some problems about exact match in the overload resolution.

W

Wayne Shu

Hi everyone.

In the following program, foo is an ambiguous call.

#include <iostream>
using namespace std;

void foo(int *);
void foo(int (&)[5]);

int main()
{
int arr[5] = {0, 1, 2, 3, 4};

foo(arr);

return 0;
}

why?
void foo(int (&)[5]); is an exact match
void foo(int (&)[5]); require an array to pointer conversion.

Regards.
 
V

Victor Bazarov

Wayne said:
Hi everyone.

In the following program, foo is an ambiguous call.

#include <iostream>
using namespace std;

void foo(int *);
void foo(int (&)[5]);

int main()
{
int arr[5] = {0, 1, 2, 3, 4};

foo(arr);

return 0;
}

why?
void foo(int (&)[5]); is an exact match
void foo(int (&)[5]); require an array to pointer conversion.

You mean foo(int*) requires the conversion...

No, foo(int(&)[5]) is not an exact match. It requires binding
of a reference. An exact match would be a function with an array
as the argument, which is impossible.

V
 
J

James Kanze

In the following program, foo is an ambiguous call.
#include <iostream>
using namespace std;
void foo(int *);
void foo(int (&)[5]);
int main()
{
int arr[5] = {0, 1, 2, 3, 4};

foo(arr);

return 0;

}
why?
void foo(int (&)[5]); is an exact match

Actually, it's what the standard calls an lvalue transformation.
void foo(int (&)[5]); require an array to pointer conversion.

You mean "foo( int* )", of course. For purposes of overload
resolution, the standard considers it an lvalue transformation
as well; for purposes of ranking, both are considered exact
matches. There are additional considerations which can be taken
into account, but none apply here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top