title: Clang vs. GCC: Friend-ing a global function via qualified/unqualified names
body: <p>I'm trying to figure out whether to file a bug report against Clang, GCC, or both (I've tested against Clang trunk and GCC 4.7.2: if someone could verify this against GCC trunk that would be helpful):</p>
  
  <p>Basically, the following code three-line file compiles fine with <code>-fsyntax-only</code>, in default and C++11 modes:</p>
  
  <pre><code>class A {
      friend void f();
  };
  </code></pre>
  
  <p>Note that there's no prior declaration of <code>f()</code>, but this is explicitly OK.</p>
  
  <p>However, Clang (but not GCC) rejects the following:</p>
  
  <pre><code>class A {
      friend void ::f();
  };
  </code></pre>
  
  <p>The error from Clang is "no function named 'f' with type 'void ()' was found in the specified scope", but I can't find any justification in the standard for treating this case differently than the previous one, so I think it's a bug; I might be wrong though (I'm reading from N3242, however, which AFAIK is the last public draft before C++11).</p>
  
  <p>This next example, however, is rejected by GCC rather than Clang:</p>
  
  <pre><code>void f() { }
  
  void g()
  {
      class A {
          friend void ::f();
      };
  }
  </code></pre>
  
  <p>The error from GCC is "friend declaration ‘void f()’ in local class without prior declaration", which doesn't seem to make sense since <code>void ::f()</code> should refer to the <code>f()</code> in the global namespace, which is declared. (Never mind that <code>friend</code>-ing a global function from a local class is nonsensical, it doesn't seem to be illegal...)</p>
  
  <p>Finally, this last example is rejected both by Clang and GCC:</p>
  
  <pre><code>void g()
  {
      class A {
          friend void ::f();
      };
  }
  </code></pre>
  
  <p>The errors are "friend declaration ‘void f()’ in local class without prior declaration" and "no function named 'f' with type 'void ()' was found in the specified scope", respectively. Now, there does appear to be some justification for rejecting a friend declaration of a previously undeclared function in a local class, from 11.4p11, but the paragraph actually reads:</p>
  
  <blockquote>
    <p>If a friend declaration appears in a local class (9.8) <strong>and the name specified is an unqualified name</strong>, a prior
    declaration is looked up without considering scopes that are outside the innermost enclosing non-class scope.
    For a friend function declaration, if there is no prior declaration, the program is ill-formed...</p>
  </blockquote>
  
  <p>The illegality of this declaration apparently is based on the second sentence in this paragraph, but it's unclear to me whether the sentence should apply in the case of a qualified name as well, as is used in this case. (Arguably, it could be that the entire paragraph applies to the "local class" case regardless of whether or not an "unqualified name" is used, and the "and the name specified is an unqualified name" clause only applies to the lookup described in the first sentence, but it seems like a stretch...the only reason I'm imagining this as a possibility is because both compilers reject it.)</p>
  
  <p>Anyway, so from what I can tell, all four of these cases (regardless of how sensible they are or not) should be legal; even if not, at least one of Clang and GCC is wrong in the second and third cases. Can anyone find an error in my logic?</p>
  
  <p>The third and fourth cases are admittedly nonsensical; but I can see the second case breaking someone's valid and useful code so I'm somewhat surprised it's never been caught, if it is indeed a bug.</p>