Search code examples
c++friend

Need help understanding a paragraph pertaining to friend declaration


However, you can define a function in a friend declaration. The class must be a non-local class, function, the function name must be unqualified, and the function has namespace scope. The following example demonstrates this:

class A { void g(); };

void z() {
class B { // friend void f() { }; }; 
} 

class C { // friend void A::g() { } 
friend void h() { } 
};

Though I understood what's meant by The class must be a non-local class but after that comma it eludes me or that function word alone surround by comma is typo?. What exactly whole para means word for word I mean. Thanks

P.S Above para is stolen from ibm C++ reference -> https://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzarg/cplr042.htm


Solution

  • Here's the wording from C++14 standard:

    [class.friend]/6 A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope. [ Example:

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

    end example ]