Search code examples
c++operator-overloadingvisual-studio-2019overload-resolutionambiguity

C++ compilation error: cannot convert from B to A, no constructor, or constructor overload ambiguity


I have some code which implies a type conversion, which does not compile although there is a conversion method...

class A
{
public:
    A(void) :_m(0) { }
    A(int val) : _m(val) {}
private:
    int _m;
};
class B
{
public:
    B(void) : _m(0) {}
    B(int val) : _m(val) {}
    B(const A&);
    // there is a direct conversion operator here
    operator A(void) const { return A(_m); }
    operator int(void) const { return _m; }
private:
    int _m;
};
int main()
{
    B b;
    A a = (A)b; // error C2440 here
}

Here is the error message:

error C2440: 'type cast': cannot convert from 'B' to 'A'
message : No constructor could take the source type, or constructor overload resolution was ambiguous

Solution

  • The error message means that these two operators

    operator A(void) const { return A(_m); }
    operator int(void) const { return _m; }
    

    can be used in the expression

    (A)b;
    

    As a result using these conversion operators there can be used either the constructor A( int ) or the default copy constructor A( const A & ).

    To make it more clear rewrite the corresponding declaration like

    A a = A( b );
    

    So whether the object b is converted to an object of the type A using the first conversion operator or to an object of the type int using the second conversion operator.

    You could avoid the ambiguity declaring the operators for example like

    operator A(void) const & { return A(_m); }
    operator int(void) const && { return _m; }
    

    that is for lvalues the first operator will be used and for rvalues the second operator will be used.

    Here is your program with the modified operators.

    #include <iostream>
    class A
    {
    public:
        A(void) :_m(0) { }
        A(int val) : _m(val) {}
    private:
        int _m;
    };
    class B
    {
    public:
        B(void) : _m(0) {}
        B(int val) : _m(val) {}
        B(const A&);
        // there is a direct conversion operator here
        operator A(void) const & { return A(_m); }
        operator int(void) const && { return _m; }
    private:
        int _m;
    };
    
    int main()
    {
        B b;
        A a = b; 
        A a1 = B();
    }