Path: utzoo!attcan!uunet!cs.utexas.edu!rutgers!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: the best match for an overloaded function Message-ID: <9820@alice.UUCP> Date: 24 Aug 89 17:47:24 GMT References: <1834@hcr.UUCP> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 81 In article <1834@hcr.UUCP>, stan@hcr.UUCP (Stan Jarzabek) writes: > What does it mean to be "the best match" for an overloaded function? > Here is an example: > struct X {}; > struct Z > { Z(X) {} > }; > int f(int,X) {} > int f(short,Z) {} > main() > { > short s; > X x; > f(s,x); > } > According to the definition from the manual, there is no best match for f(s,x), > as the intersection of sets is empty. The 2.0 selects f(int,X), which confirms > the intuition and contradicts the manual definition. I think this is a bug in cfront. The best match for `s' is clearly `short' and the best match for `x' is equally clearly `X', so there's no reason to choose f(int,X) over f(short,Z). > In the example below, the compiler behaves inconsistently, and contradicts both > the intuition and the definition from the manual. > struct X > { > int a; > X(int i) { a = i; } > operator int() { return a; } > }; > int f(short,short,short) { return 1; } > int f(char,char,short) { return 2; } > int f(X,short,short) { return 3; } > main() > { > short s = 1; > X x(1); > f(x,s,x); //2.0: f(X,short,short) is called > f(x,x,s); //2.0: ambiguous call > } > The last example shows that the intuitive meaning of "the best match" may not > be obvious. Here I think cfront is correct and matches what the manual says. Let's look first at the first call, f(x,s,x). The best match for the first `x' argument is surely X. The best match for `s' is `short', and the best match for `x' is also `short' -- not the greatest but it's the best available. Therefore this calls f(X,short,short). In the second call, f(x,x,s), the best match for the first `x' is again X. The second argument is an X and the choice is between short and char. There is no conversion from X to short or char, just to int. There is no reason to prefer X->int->short or x->int->char, so the second argument is ambiguous. Hence the entire call is ambiguous. Note that the third argument for each f() is short. Thus for all practical purposes the third argument does not participate in function matching. For curiosity's sake, what do you find counterintuitive about the `best match' rule? -- --Andrew Koenig ark@europa.att.com