This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FWD: How to reuse a friend operator?


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:

error C3767: '+' matching function is not accessible
could be the friend function at 'vcadv.cpp(19)' : '+' [may be found via
argument-dependent lookup]

or the friend function at 'vcadv.cpp(25)' : '+' [may be found via
argument-dependent lookup]

Comeau on-line tester says:

error: the global scope has no "operator+"
              return ::operator +(static_cast<int>(L),R);
                       ^

When I remove "::", it says:

 error: a nonstatic member reference must be relative to a
          specific object
              return operator +(static_cast<int>(L),R);



So which behaviour is correct according to the C++ standard? :-)

    Best regards
    Piotr Wyderski


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]