How is ambiguity determined in the overload resolution algorithm?

后端 未结 3 1988
南方客
南方客 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:13

    Ambiguity is determined by the ranking:

    1. Exact match: no conversion required, lvalue-to-rvalue conversion, qualification conversion, user-defined conversion of class type to the same class
    2. Promotion: integral promotion, floating-point promotion
    3. Conversion: integral conversion, floating-point conversion, floating-integral conversion, pointer conversion, pointer-to-member conversion, boolean conversion, user-defined conversion of a derived class to its base

    Exact match wins vs Promotion which wins vs Conversion.

    In the example:

    void func(int, bool, float, int){cout << "int,bool,float,int" << endl;}
    void func(int, bool, int, int){cout << "int,int,int,int" << endl;}
    
    int main()
    {
        func(1,1,3.4,4);
    }
    

    Argument 1(1) is an exact match on both
    Argument 2(1) is an exact match on both
    Argument 3(3.4) can be converted into float and int - Ambiguity Neither is better.
    Argument 4(4) is an exact match on both

    But if we did this: func(1,1,3.4f,4);
    (3.4f) is now an exact match!
    void func(int, bool, float, int) then wins the battle.

提交回复
热议问题