Hello,
below is a repost from comp.lang.c++. The problem is how
to use a friend operator defined inside a class by another
friend operator in this class. Which compiler is right?
Victor Bazarov wrote:
The code compiles fine with Comeau C++.
But consider this one, which is a simplified version of my problem:
-----------8<-------------
#include <iostream>
class X {
public:
int v;
int operator +(const int R) {
return 3;
}
friend inline int operator +(const int L, const X& R) {
std::cout << "OK int";
return L;
}
friend inline int operator +(const char L, const X& R) {
std::cout << "OK char";
return operator +(static_cast<int>(L),R); // (*)
}
};
int main(int argc, char *argv[]) {
X x;
'a'+x;
return 0;
}
-----------8<-------------
Desired behaviour is that the program displays "OK charOK int"
GPP 3.4 displays:
test.cpp: In function `int operator+(char, const X&)':
test.cpp:24: error: no matching function for call to `X::operator+(int,
const X&)'
test.cpp:10: note: candidates are: int X::operator+(int)
If I change the (*) line to
return ::operator +(static_cast<int>(L),R);
then it works. VC7.1 in all cases reports an error: