Quote:
Originally Posted by eSelix
I think that Anisha ask why compiler not warn about ambiguity between these two functions
|
I don't think Anisha was asking that. But since you are effectively asking it, I'll explain:
There is no ambiguity because one definition required a non const left operand and the other takes a const left operand.
When the left operand actually is const, the compiler can only use the definition that takes a const left operand. No hint of ambiguity there.
When the left operand is non const, a definition that takes a const left operand could be used (some ambiguity there) but the definition that takes a non const left operand is more specific. The ambiguity is resolved when one definition is more specific than the other.
Next consider the statement
The left operand is a const int and the right operand is a non const binaryOperators. No operator+ takes that pair of inputs, so the compiler must find implicit casts.
When you implicitly cast an integer to a binaryOperators, the result is a temporary object, and a temporary object passed by reference to a function is const. So even if the int had not been const, the binaryOperators object it becomes would be const.
The fact that the right operand is non const makes no difference because both operator+ definitions take only const right hand operands (so neither definition is a less specific fit to the right hand operand).
Only one definition fits the const left hand operand, so that definition is used.