Bug#312362: g++-4.0: friend is ineffective in namespace
Fumitoshi UKAI <ukai@debian.or.jp> writes:
> I think this is a bug of g++-4.0.
No, it's a documented change in behaviour. From
http://gcc.gnu.org/gcc-4.0/changes.html
* Friends of classes defined outside their namespace are correctly handled:
namespace N {
class A;
}
class N::A {
friend class B; // Refer to N::B in GCC 4.0.0
// but ::B in earlier versions of GCC
};
> It can be reprodusible by this code:
>
> % cat a.cc
> namespace ns {
> class A;
> };
> class ns::A {
> public:
> A(int j) { i = j; };
> private: int i;
> friend A operator+(const A& x, const A& y);
ns::operator+ in gcc-4, ::operator+ in earlier versions.
> };
> using namespace ns;
> A operator+(const A& x, const A& y) {
> return A(x.i + y.i);
> }
This is ::operator+ which is not a friend in gcc-4. If you use
A ns::operator+(const A& x, const A& y) {
return A(x.i + y.i);
}
you define the friend and it will compile, but I don't know if that's
what you want. If you want to make ::operator+ a friend in gcc-4 use
something like
namespace ns {
class A;
};
ns::A operator+(const ns::A& x, const ns::A& y);
class ns::A {
public:
A(int j) { i = j; };
private: int i;
friend A (::operator+)(const A& x, const A& y);
};
using namespace ns;
A operator+(const A& x, const A& y) {
return A(x.i + y.i);
}
--
Philip Martin
Reply to: