Path: utzoo!attcan!ncrcan!hcr!stan From: stan@hcr.UUCP (Stan Jarzabek) Newsgroups: comp.lang.c++ Subject: the best match for an overloaded function Message-ID: <1834@hcr.UUCP> Date: 23 Aug 89 19:17:04 GMT Reply-To: stan@hcrvx1.UUCP (Stan Jarzabek) Organization: HCR Corporation, Toronto Lines: 84 What does it mean to be "the best match" for an overloaded function? The new C++ manual says: "the best-matching function is the intersection of sets of functions that best match on each argument". This definition disagrees with the intuition and, fortunately, that is not how the AT&T 2.0 implements selecting the best matching 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. So, what is the definition of the best match for an overloaded function, as implemented in the AT&T 2.0? 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. The overloading resolution mechanism should be based on some reasonably simple principle which would allow the user to staticly determine whether a given function call is ambiguous or not, and if not, which function will be called. In particular, function calls in the last example should be resolved in the same way, i.e. either a) function f(X,short,short) should be selected for both calls, or b) both calls should be considered ambiguous. The interpretation a) in the last example reflects the principle: "the best match has the minimum number of arguments that match at the highest level" (I refer to the conversion levels [1] - [6], pp. 88-89 in the Reference Manual.) In interpretation b), the underlying principle is: "if there are more than one functions requiring argument conversions at the highest level the function call is ambiguous." The former interpretation is more general, but the latter is safer. The AT&T 2.0 algorithm for finding the best match seems to be based on some more complex principle. The overloading resolution mechanism in the AT&T 2.0 cfront sometimes behaves inconsistently which is probably due to some bugs, but does anybody know how this was intended to work? At the language definition level, what does it mean to be the best match for an overloaded function with many arguments? Stan Jarzabek, HCR Corporation, Toronto