Wake up Brazil
Wake up Brazil

Reputation: 3411

Which namespace does contain the declaration of this friend function?

The friend function below is not found by ordinary lookup (§7.3.1.2/3), but is found by ADL (§3.4.2/4 second bullet point), so the code compiles and executes normally (live example). But the function f isn't declared in any namespace. For example, if you try replacing the call f(x); by any of these calls ::f(x);, A::f(x); or A::X::f(x);, the code will not compile. Which namespace does contain the declaration of this friend function? Does the Standard say anything about this?

#include <iostream>
namespace A {
    class X {
        int i;
        friend void f(X x) { std::cout << x.i << '\n'; }
    public:
        X():i(101){}
    };
}

int main()
{
    A::X x;
    f(x);
}

Upvotes: 4

Views: 322

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

From the C++ Standard

11.3 Friends

6 A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope. [ Example:

class M { friend void f() { } // definition of global f, a friend of M,
                              // not the definition of a member function
};

—end example ]

Amd the other quote (7.3.1 Namespace definition)

3 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, function, class template or function template98 the friend is a member of the innermost enclosing namespace. The friend declaration does not by itself make the name visible to unqualified lookup (3.4.1) or qualified lookup (3.4.3). [ Note: The name of the friend will be visible in its namespace if a matching declaration is provided at namespace scope (either before or after the class definition granting friendship). —end note ] If a friend function or function template 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 a friend 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. [ Note: The other forms of friend declarations cannot declare a new member of the innermost enclosing namespace and thus follow the usual lookup rules. —end note ]

I would like to mention that enclosing a function name in parentheses suppresses the argument-dependent lookup.

For this code

int main()
{
    A::X x;
    ( f )(x);
}

function f will not be found.

Upvotes: 5

Related Questions