Home >Backend Development >C++ >What is the Scope of Inline Friend Functions in C ?
Unveiling the Scope of Inline Friend Functions
In the labyrinth of C intricacies, understanding the realm of inline friend functions can be a daunting task. To unravel this enigma, let's delve into the key question: what is the actual scope of such functions?
Friend Function Declaration in Namespace Scope
When an inline friend function is declared within a class, astoundingly, it does not automatically reside within the class scope. Instead, it establishes a presence in the nearest enclosing namespace scope. However, this existence remains obscured, concealed from view by the watchful eyes of unqualified and qualified lookups.
Enter Argument-Dependent Lookup: A Pathway to Visibility
Despite the elusiveness of inline friend functions in standard lookups, there exists a beacon of hope known as argument-dependent lookup (ADL). This clandestine approach allows the compiler to unveil the hidden function when a non-qualified function call is made within the context of an object or expression of the containing class.
A Code Manifestation of Scope Ambiguity
To illustrate the scope intricacies, consider the following code snippet:
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 } }
Each attempt to invoke the baz() function directly fails, highlighting the invisible barriers that prevent direct access through unqualified and qualified lookups. However, in the call_friend() member function, baz() shines forth, unhindered by the scope constraints, thanks to the benevolent force of ADL.
Standard Seal of Authority
The definitive explanation of this perplexing behavior resides within the ISO/IEC 14882:2011 standard:
"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)."
This excerpt underscores the ephemeral nature of friend functions declared without explicit namespace qualification. They inhabit the ethereal realms of the enclosing namespace and remain elusive to the standard gaze, except when summoned through the mystic rites of ADL.
The above is the detailed content of What is the Scope of Inline Friend Functions in C ?. For more information, please follow other related articles on the PHP Chinese website!