Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The scope of in-class-defined friend function?

Tags:

c++

A few days ago I asked a question about the scope of in-class-defined friend functions (Which scope does an in-class-defined friend function belong to?), and I get to know that the function is in the scope of the enclosing namespace but won't be searchable until explicitly declared outside the class (ADL is an exception).

Today I found some relevant statements in the C++ standard (section 11.3):

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 ]

Such a function is implicitly inline. A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not (3.4.1).

We can see that there are two scope-related statements here: "has namespace scope" and "is in the (lexical) scope of the class in which it is defined". I'm confused here. If the former relates to my previous question (Which scope does an in-class-defined friend function belong to?), then what does the latter stand for?

like image 651
goodbyeera Avatar asked Feb 06 '14 16:02

goodbyeera


1 Answers

A "namespace-scope function" is a function that is a member of a namespace (i.e the "scope" here means the "home scope" of the function).

The later statement links to 3.4.1, which has to say

Name lookup for a name used in the definition of a friend function (11.3) defined inline in the class granting friendship shall proceed as described for lookup in member function definitions.

like image 117
Johannes Schaub - litb Avatar answered Oct 15 '22 09:10

Johannes Schaub - litb