Home >Backend Development >C++ >How Can Inline Friend Functions Be Accessed in C ?
Accessing Inline Friend Functions: Lexical and Actual Scope
When declaring inline friend functions within a class, it's important to consider not only their lexical scope but also their actual scope. Lexically, such functions reside within the class they're defined in. However, their actual accessibility may be limited depending on the context.
As per the C standard (ISO/IEC 14882:2011), when a friend function is declared with an unqualified identifier within a class, it references a function within the nearest enclosing namespace scope. However, this friend declaration does not make that function visible for normal lookup in that scope. Instead, it only makes it accessible through argument-dependent lookup (ADL).
This behavior is emphasized in 7.3.1.2/3 of the standard:
"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 or by qualified lookup until a matching declaration is provided in that namespace scope."
In essence, inline friend functions can only be called through ADL, which is not possible when no arguments are provided. Therefore, attempts to call such functions directly through the enclosing scope of the class or through member functions will result in compilation errors, as demonstrated in the provided code examples.
The above is the detailed content of How Can Inline Friend Functions Be Accessed in C ?. For more information, please follow other related articles on the PHP Chinese website!