G'day!
I have a question about using friend
in C ++. Consider the following code snippet:
#include <ostream>
struct F {
};
struct N {
friend std::ostream& operator<< (std::ostream&, const N&);
friend std::ostream& operator<< (std::ostream&, const F&);
};
void foo(std::ostream &out) {
F bar;
out << bar;
}
My understanding has always been that it friend
is similar to static
with the additional property that a function friend
has access to the private part of the class. Under this assumption, the code should compile because there exists operator<<
one that accepts ostream&
a (const) as well F&
.
G ++ 4.0 seems to share my thoughts on this, as it accepts this code. However, the much newer g ++ 4.5 (.2) rejects the code with the message:
ns.cc: In function 'void foo(std::ostream&)':
ns.cc:14:10: error: no match for 'operator<<' in 'out << bar'
is g ++ 4.5 wrong or am i (and g ++ 4.0) wrong?
( F
, operator<<
N
.)
,