Why is this call to member function ambiguous?

前端 未结 3 1307
野趣味
野趣味 2020-12-18 20:06

Consider this class:

class Base{
public:
    void func(double a) = delete;
    void func(int a) const {}
};

int main(){
    Base base;

    base.func(1);
           


        
相关标签:
3条回答
  • 2020-12-18 20:32

    Non-static member functions, like the two:

    void func(double);    // #1
    void func(int) const; // #2
    

    accept also an implicit object parameter which is considered in overload resolution ([over.match]/p1) as any other argument:

    Overload resolution is a mechanism for selecting the best function to call given a list of expressions that are to be the arguments of the call and a set of candidate functions that can be called based on the context of the call. The selection criteria for the best function are the number of arguments, how well the arguments match the parameter-type-list of the candidate function, how well (for non-static member functions) the object matches the implicit object parameter, and certain other properties of the candidate function.

    After incorporating the implicit object parameter into the member functions signatures, the compiler sees two overloads:

    void func(Base&, double);    // #1
    void func(const Base&, int); // #2
    

    and tries to select the best viable function based on the call:

    Base base;
    base.func(1);
    

    The conversion from base (which is a non-const lvalue of type Base) to Base& has an Exact Match rank (direct reference binding yields an Identity conversion) -- see Table 13. The conversion from base to const Base& is also of an Exact Match rank, however, [over.ics.rank]/p3.2.6 declares #1 to have a better conversion sequence:

    — S1 and S2 are reference bindings ([dcl.init.ref]), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers. [ Example:

    int f(const int &);
    int f(int &);
    int g(const int &);
    int g(int);
    
    int i;
    int j = f(i);    // calls f(int &)
    int k = g(i);    // ambiguous
    

    Now for the second parameter, a conversion from an integral prvalue 1 to double is a Floating-integral conversion ([conv.fpint]) which is given a Conversion rank. On the other hand, 1 to int is an Identity conversion which is of an Exact Match rank. For this argument, #2 is considered to have a better conversion sequence ([over.ics.rank]/p3.2.2):

    — the rank of S1 is better than the rank of S2, or S1 and S2 have the same rank and are distinguishable by the rules in the paragraph below, or, if not that, [...]

    Overload resolution to succeed requires that there exists at most one parameter for which conversion sequences differ ([over.match.best]):

    Given these definitions, 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), or, if not that, [...]

    Here, ICS0(#1) is better than ICS0(#2), but in turn, ICS1(#2) is better than ICS1(#1), so the compiler can't choose between the two overloads and detects ambiguity.

    0 讨论(0)
  • 2020-12-18 20:41

    It because of the const modifier in func(int). The base instance is not const. C++ compilers seem to find the non-const method first if the instance is not const. And then, compilers found that method has been deleted. So the compiler gives a warning.

    Try to remove the const modifier, or move the const modifier to func(double) will get rid of the warning.

    It seems that this warning is not about implicit conversion. Even you invoke func by func((int)(1)) is not good either.

    0 讨论(0)
  • 2020-12-18 20:45

    When a function is overloaded, the overload resolution takes place first. The program is ill-formed if the deleted function is the best match and is selected.

    Therefore, your program would produce the same error as the following, because there is an implicit conversion from int to double and the compiler does not know what function you intend to call:

    class Base{
    public:
        void func(double a) {}
        void func(int a) const {}
    };
    
    0 讨论(0)
提交回复
热议问题