How is ambiguity determined in the overload resolution algorithm?

后端 未结 3 2096
南方客
南方客 2021-01-30 12:34

I\'m trying to understand the overloading resolution method.

Why is this ambiguous:

void func(double, int, int, double) {}
void func(int, double, double,         


        
3条回答
  •  梦谈多话
    2021-01-30 13:02

    The wording from the standard (§[over.match.best]/1) is:

    [...] let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F.
    [...] a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then
    — for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2)

    In your first case, the two functions fail the first test. For the first argument, the first function (taking double) has a worse conversion sequence than the second. For the second argument, the second function has a worse conversion sequence than the first (again, the int has to be promoted to double in one case, but not the other).

    Therefore, neither function passes the first rule, and the call is ambiguous.

    Between the second pair of functions, every argument to the the first function has at least as good of a conversion as the matching argument to the second function. We then go on to the second rule, and find that there is at least one argument (two, as a matter of fact) for which the first function has a better conversion (identity instead of promotion) than the second.

    Therefore, the first function is a better match, and will be selected.

提交回复
热议问题