Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Overload operator bool() gives an ambiguous overload error with operator+

I'm compiling some c++ code of a class MegaInt which is a positive decimal type class that allows arithmetic operations on huge numbers.

I want to overload operator bool to allow code like this:

MegaInt m(45646578676547676);  
if(m)  
    cout << "YaY!" << endl;

This is what I did:

header:

class MegaInt
{
    public:
        ...
    operator bool() const;
};

const MegaInt operator+(const MegaInt & left, const MegaInt & right);
const MegaInt operator*(const MegaInt & left, const MegaInt & right);

implementation:

MegaInt::operator bool() const
{
    return *this != 0;
}
const MegaInt operator+(const MegaInt & left, const MegaInt & right)
{
    MegaInt ret = left;
    ret += right;
    return ret;
}

Now, the problem is if I do:

MegaInt(3424324234234342) + 5;

It gives me this error:

ambiguous overload for 'operator+' in 'operator+(const MegaInt&, const MegaInt&) note: candidates are: operator+(int, int) | note: const MegaInt operator+(const MegaInt&, const MegaInt&)|

I don't know why. How is the overloaded bool() causing operator+ to become ambiguous?¸

Thank You.


Well, everyone gave me great answers, unfortunately, none of them seem to solve my problem entirely.

Both void* or the Safe Bool Idiom works. Except for one tiny problem, I hope has a workaround:

When comparing with 0 like:

if (aMegaInt == 0)

The compiler gives an ambiguous overload error again. I understand why: it doesn't know if we're comparing to false or to MegaInt of value 0. None the less, in that case, I'd want it to cast to MegaInt(0). Is there a way to force this?

Thank You Again.

like image 963
Didier A. Avatar asked Mar 15 '11 02:03

Didier A.


People also ask

Which operator is overloaded for C operation?

Input/Output Operators Overloading in C++ C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object.

Which operator we Cannot overload in C?

(::) Scope resolution operator cannot be overloaded in C language.

What is operator overloading in C with example?

Operator Overloading in C++ This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

Which operator you Cannot overload?

You cannot overload the following operators: . You cannot overload the preprocessor symbols # and ## . An operator function can be either a nonstatic member function, or a nonmember function with at least one parameter that has class, reference to class, enumeration, or reference to enumeration type.


2 Answers

The C++ compiler is allowed to automatically convert bool into int for you, and that's what it wants to do here.

The way to solve this problem is to employ the safe bool idiom.

Technically, creating an operator void* is not an example of the safe bool idiom, but it's safe enough in practice, because the bool/int problem you're running into is a common error, and messes up some perfectly reasonable and otherwise correct code (as you see from your question), but misuses of the void* conversion are not so common.

like image 92
Ken Bloom Avatar answered Sep 28 '22 18:09

Ken Bloom


The wikipedia entry on explicit conversion operators for C++0x has a decent summary of why you see this error pre-C++0x. Basically, the bool conversion operator is an integral conversion type, so it will be used in an integral arithmetic expression. The pre-C++0x fix is to instead use void * as the conversion operator; void * can be converted to a boolean expression, but not to an integral expression.

like image 20
MSN Avatar answered Sep 28 '22 16:09

MSN