I'm learning c++ and using C++ Primer. Consider the following exercise 14.46:
class Complex {
Complex(double);
// ...
};
class LongDouble {
friend LongDouble operator+(LongDouble&, int); // (1)
public:
LongDouble(int);
operator double();
LongDouble operator+(const Complex &); // (2)
// ...
};
LongDouble operator+(const LongDouble &, double); // (3)
LongDouble ld(16.08);
double res = ld + 15.05; // which operator+ ?
When I compile using gcc 4.5 the above program, I get
14_46.cpp:60:21: error: ambiguous overload for ‘operator+’ in ‘ld + 1.5050000000000000710542735760100185871124267578125e+1’
14_46.cpp:60:21: note: candidates are: operator+(double, double) <built-in>
14_46.cpp:35:5: note: LongDouble LongDouble::operator+(const Complex&)
14_46.cpp:45:1: note: LongDouble operator+(const LongDouble&, double)
14_46.cpp:17:5: note: LongDouble operator+(LongDouble&, int)
Why is (3) not selected ? Isn't it exact match?
However, I noticed that removing const-ness of parameter in (3) matches exactly, i.e.,
LongDouble operator+(LongDouble &, double); // (4)
Using (4) there is no ambiguity. Am I missing something here?