So there is a question I came across
int f() {
return 5;
}
struct A{
static int sX;
static int f() {
return 1;
}
};
int A::sX = f(); // (1)
This complies fine. I wanted to understand why (1)
ends up having a
call A::f()
in its compiled code. I understand there is some scope rule being invoked here but would like to know the exact rule. I'd expected it to invoke the global f()
. Why doesn't the (unqualified?)name lookup of f
yield the global function?
P.S. I'd looked up ADL
but since f()
doesn't accept any arguments, would ADL
kick in?
Edit: I think I found the answer to my question. Check this link under unqualified name lookup
which states:
For a name used in the definition of a namespace-member variable outside the namespace, lookup proceeds the same way as for a name used inside the namespace:
namespace X { extern int x; // declaration, not definition int n = 1; // found 1st }; int n = 2; // found 2nd. int X::x = n; // finds X::n, sets X::x to 1