Visibility Scope of Inline Friend Functions
Despite defining inline friend functions within a class, their actual scope extends beyond the class's boundaries. The C standard dictates that an inline friend function's name refers to a function declared in the nearest enclosing namespace scope.
However, this declared function is not accessible through unqualified or qualified lookup until a matching declaration is provided within that namespace's scope. Consequently, direct calls to the friend function from the class itself, enclosing scope, or member functions will result in errors.
This limitation stems from the fact that the friend function is declared in the class without overriding or redefining an existing function name. Therefore, the compiler doesn't recognize the function in the class or enclosing namespace scope until it encounters an explicit declaration in that namespace.
To access the inline friend function, you can employ argument-dependent lookup (ADL). ADL allows the compiler to search for the function based on the types of the arguments used in the call. This approach aligns with the principle that the function is considered "related" to the type being passed as an argument.
The above is the detailed content of Why Can\'t I Directly Call Inline Friend Functions from Within Their Defining Class?. For more information, please follow other related articles on the PHP Chinese website!