This surprised me. This works:
struct foo {
int x;
friend int x(foo f) { return f.x; }
friend int y(foo f);
};
int y(foo f) { return x(f); } // no problem
But this is an error:
struct foo {
int x;
friend int x(foo f) { return f.x; }
friend int y(foo f) { return x(f); } // error: invalid use of foo::x data member
};
Why aren't both of these (dis)allowed?
The reason is that in the first case, the friendship injected the function declaration into the enclosing namespace, so the global-scoped call to x
can see only one x
.
In the second example, x
has two meanings at that scope: The global friend function and the variable (which presumably shadows the global friend function).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With