c++ - How is ambiguity determined in the overload resolution algorithm? -
i'm trying understand overloading resolution method.
why ambiguous:
void func(double, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); }
but isn't?
void func(int, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); }
in first case there 2 exact parameters matches , 2 conversions against 1 exact match , 3 conversions, , in second case there 3 exact matches , 1 conversion against 1 exact matches , 3 conversions.
so why 1 ambiguous , 1 not? logic here?
the overload resolution rules define partial order on set of matches - if overload f1
not better match f2
, not imply f2
better match f1
. exact partial order can thought of comparing 2 points in k
dimensions, number of arguments k
. lets define partial order on points in k
-dim space - (x_1, x_2,..., x_k) < (y_1, y_2,..., y_k) if x_i <= y_i , x_j < y_j @ least 1 j
. partial order on candidate non-template functions defined standard.
lets @ examples :
void func(double, int, int, double) {} vvv vvv vvv better better equal void func(int, double, double, double) {} vvv vvv better equal
so neither overload strictly better other.
in second example:
void func(int, int, int, double) {} vvv vvv vvv vvv equal better better equal void func(int, double, double, double) {} vvv equal
now, first overload better second in 1 argument , never worse second. thus, there no ambiguity - partial order indeed declare first 1 better.
(the above description not consider function templates. can find more details @ cppreference.)
Comments
Post a Comment