What is the fully qualified name of a friend function defined inside of a class?
I recently saw an example analogous to the following. What is the fully qualified name of val()
below?
#include <iostream>
namespace foo {
class A {
int x;
public:
A(int x = 0) : x(x) { }
friend int val(const A &a) { return a.x; }
};
}
int main() {
foo::A a(42);
// val() found using ADL:
std::cout << val(a) << std::endl;
// foo::val(a); // error: 'val' is not a member of 'foo'
// foo::A::val(a); // error: 'val' is not a member of 'foo::A'
return 0;
}
Is argument-dependent lookup the only way val()
can be found?
Admittedly, this does not stem from a practical problem. I am simply looking to gain a better understanding.
val
is a member offoo
" <- So this is a situation whereval
is indeed a member offoo
, butfoo::val
cannot be used to refer to it. That is important because I just managed to get the compiler to issue an error message where it refers toval
, and it saidfoo::val
. That was confusing given that earlier it said thatval
was not a member offoo
. – Omniscience