What namespace contains the declaration of this function?

The function friendbelow was not found by the usual search (§7.3.1.2 / 3), but ADL was found (clause 3.3.4 / 4 seconds), so the code compiles and runs normally ( live example ). But a function is fnot declared in any namespace. For example, if you try to replace a call f(x);with any of these calls ::f(x);, A::f(x);or A::X::f(x);, the code will not compile. What namespace contains the declaration of this function? Does the Standard talk about this?

#include <iostream>
namespace A {
    class X {
        int i;
        friend void f(X x) { std::cout << x.i << '\n'; }
    public:
        X():i(101){}
    };
}

int main()
{
    A::X x;
    f(x);
}
+4
source share
1 answer

From the C ++ standard

11.3

6 if (9.8), unqualified . [:

class M { friend void f() { } // definition of global f, a friend of M,
                              // not the definition of a member function
};

-end ]

(7.3.1 )

3 , , . , , 98 . (3.4.1) (3.4.3). [: , ( ). -end note] , , , (3.4.2). , , , , - . [: , , . -end note ]

, .

int main()
{
    A::X x;
    ( f )(x);
}

f .

+5

All Articles