After searching aroung SO, one question taught me that the lexical scope of an inline friend function is the class it's defined in, meaning it can access e.g. the typedef
s in the class without qualifying them. But then I wondered what is the actual scope of such a function? GCC at least rejects all my attempts to call it. Can a function such as in the example ever be called through means other than ADL, which is not possible here thanks to no arguments?
Standard quotations are appreciated, as I currently can't access my copy of it.
The following code
namespace foo{ struct bar{ friend void baz(){} void call_friend(); }; } int main(){ foo::baz(); // can't access through enclosing scope of the class foo::bar::baz(); // can't access through class scope } namespace foo{ void bar::call_friend(){ baz(); // can't access through member function } }
results in these errors:
prog.cpp: In function ‘int main()’: prog.cpp:9: error: ‘baz’ is not a member of ‘foo’ prog.cpp:10: error: ‘baz’ is not a member of ‘foo::bar’ prog.cpp: In member function ‘void foo::bar::call_friend()’: prog.cpp:15: error: ‘baz’ was not declared in this scope
If the friend function is a member of another class, you need to use the scope resolution operator ( :: ). For example: class A { public: int f() { } }; class B { friend int A::f(); }; Friends of a base class are not inherited by any classes derived from that base class.
Explanation: Friend functions are not in the scope of a class and hence cannot be called through a class object. A friend function can access all types of members of the class. They can be invoked as a normal function.
There's no difference. Both friend and inline are independent declaration specifiers, which can be specified in any order. There's not much point in declaring the function inline in a friend declaration though. More generally, there's no point in specifying inline in any non-defining function declaration.
Friend function in C++ is used when the class private data needs to be accessed directly without using object of that class. Friend functions are also used to perform operator overloading. As we already know about the function overloading, operators can also be overloaded with the help of operator overloading.
When you declare a friend
function with an unqualified id in a class it names a function in the nearest enclosing namespace scope.
If that function hasn't previously been declared then the friend
declaration doesn't make that function visible in that scope for normal lookup. It does make the declared function visible to argument-dependent lookup.
This is emphasised in many notes, but the definitive statement is in 7.3.1.2/3 (of ISO/IEC 14882:2011):
Every name first declared in a namespace is a member of that namespace. If a
friend
declaration in a non-local class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). If a friend function is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2). If the name in afriend
declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With