This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 110b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-01-15


1477. Definition of a friend outside its namespace

Section: _N4868_.9.8.2.3  [namespace.memdef]     Status: CD3     Submitter: John Spicer     Date: 2012-03-09

[Moved to DR at the April, 2013 meeting.]

According to _N4868_.9.8.2.3 [namespace.memdef] paragraph 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 or function95 the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup (6.5.3 [basic.lookup.unqual]) or by qualified lookup (6.5.5 [basic.lookup.qual]) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship).

Taken literally, that would mean the following example is ill-formed:

  namespace N {
    struct A {
      friend int f();
    };
  }
  int N::f() { return 0; }
  int i = N::f();    // ill-formed: N::f not found

because the definition of N::f appears in global scope rather than in namespace scope.

Proposed resolution (October, 2012):

Change _N4868_.9.8.2.3 [namespace.memdef] paragraph 3 as follows:

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 function95 the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by The friend declaration does not by itself make the name visible to unqualified lookup (6.5.3 [basic.lookup.unqual]) or by qualified lookup (6.5.5 [basic.lookup.qual]). [Note: The name of the friend will be visible in its namespace if until a matching declaration is provided in that at namespace scope (either before or after the class definition granting friendship). end note] If a friend function is called...