0

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
Zoso
  • 3,273
  • 1
  • 16
  • 27
  • If someone thinks this is a dupe, they should provide a simple answer targetting my specific question and then dupe it, so that I have some context. The other question doesn't answer my question at all and now I'm pretty sure that this question won't be reviewed for re-opening. – Zoso Nov 21 '21 at 12:32
  • 1
    I think that's plain unqualified name lookup, not ADL (https://en.cppreference.com/w/cpp/language/unqualified_lookup section "Static data member definition") – Mat Nov 21 '21 at 12:32
  • 1
    http://eel.is/c++draft/class.static.data#3 – Mat Nov 21 '21 at 12:38
  • @Mat, I missed your comment. I updated my answer with my finding. Would you want to provide an answer? I can accept it. – Zoso Nov 21 '21 at 12:39

0 Answers0