"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Can\'t I Directly Call Inline Friend Functions from Within Their Defining Class?

Why Can\'t I Directly Call Inline Friend Functions from Within Their Defining Class?

Published on 2024-12-22
Browse:138

Why Can\'t I Directly Call Inline Friend Functions from Within Their Defining Class?

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3