C++ Logo

std-discussion

Advanced search

Local classes and friend functions

From: Vladimir Grigoriev <vlad.moscow_at_[hidden]>
Date: Fri, 29 Sep 2023 20:48:35 +0300
In the C++23 Standard there is written ( 11.9.4 Friends):
 
«11 If a friend declaration appears in a local class (11.6) and the name specified is an unqualified name, a prior declaration is looked up without considering scopes that are outside the innermost enclosing non-class scope. For a friend function declaration, if there is no prior declaration, the program is ill-formed. For a friend class declaration, if there is no prior declaration, the class that is specified belongs to the innermost enclosing non-class scope, but if it is subsequently referenced, its name is not found by name lookup until a matching declaration is provided in the innermost enclosing non-class scope.»
 
and there is also provided an example (I have simplified it)
 
void f()
{
    extern void b();
 
    class A
    {
        friend void b(); // OK
    };
}
In this case a question arises: How can the friend function b access an object of the local class A? The name A is not visible outside the bloc k scope of the function f. If it is possible can anybody provide an example of such a function?
 
W ith best regards,
(Vlad from Moscow)
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com

Received on 2023-09-29 17:48:44