[llvm-bugs] [Bug 34329] New: non-template friend does not find function template

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Aug 25 14:35:57 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34329

            Bug ID: 34329
           Summary: non-template friend does not find function template
           Product: new-bugs
           Version: 4.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: aschepler at gmail.com
                CC: llvm-bugs at lists.llvm.org

clang rejects this well-formed code:

template <typename T>
int f(T& obj) { return obj.m; }

class X {
private:
    int m;
    friend int ::f(X&);
};

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

The error message is:
7 : <source>:7:18: error: out-of-line declaration of 'f' does not match any
declaration in the global namespace
    friend int ::f(X&);
                 ^

This situation is described in Standard section [temp.friend].  (The wording
was clarified some in C++11, but has the same meaning as in C++03, and has not
changed since.)

Quote:

For a friend function declaration that is not a template declaration:

- if the name of the friend is a qualified or unqualified template-id, the
friend declaration refers to a specialization of a function template,
otherwise,

- if the name of the friend is a qualified-id and a matching non-template
function is found in the specified class or namespace, the friend declaration
refers to that function, otherwise,

- if the name of the friend is a qualified-id and a matching function template
is found in the specified class or namespace, the friend declaration refers to
the deduced specialization of that function template ([]), otherwise,

- the name shall be an unqualified-id that declares (or redeclares) a
non-template function.

End Quote.

In this code, the first bullet does not apply because ::f is not a template-id
(there are no <> tokens).  ::f is a qualfied-id, so the second bullet should be
checked, but there is no matching non-template function named ::f.  The third
bullet is what makes the code well-formed: the compiler should deduce a
specialization of the function template ::f.  Since deduction should succeed,
the friend names a specialization of ::f.

(gcc 7.2, icc 17, and MSVC 19.00.23506 all accept the above code.)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170825/507331ac/attachment.html>


More information about the llvm-bugs mailing list