[llvm-bugs] [Bug 31507] New: Call to friend function template of a class template considered ambiguous

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jan 1 12:48:03 PST 2017


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

            Bug ID: 31507
           Summary: Call to friend function template of a class template
                    considered ambiguous
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: t at sharklasers.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

The following code attempts to declare a function template as a friend to a
class template and defines it subsequently:

template <typename T>
class A {

    template <typename T1, typename T2>
    friend auto func(T1 a, T2 b);

private:
    T a;
};

template <typename T1, typename T2>
auto func(T1 x, T2 y) {
    return x.a + y.a;
}

int main() {
    A<int> a;
    A<double> b;

    func(a, b);
}

Clang version 4.0.0 (trunk 290784) rejects this code saying:
test.cpp:21:5: error: call to 'func' is ambiguous
    func(a, b);
    ^~~~
test.cpp:6:17: note: candidate function [with T1 = A<int>, T2 = A<double>]
    friend auto func(T1 a, T2 b);
                ^
test.cpp:13:6: note: candidate function [with T1 = A<int>, T2 = A<double>]
auto func(T1 x, T2 y) {

GCC, ICC and MSVC accept that code. With the following change:

template <typename T>
class A {

    template <typename T1, typename T2>
    friend auto func(T1 x, T2 y) {
        return x.a + y.a;
    }

private:
    T a;
};

int main() {
    A<int> a;
    A<double> b;

    func(a, b);
}

Clang accepts, however GCC, ICC and MSVC reject with similar error messages:
test.cpp: In instantiation of ‘class A<double>’:
test.cpp:16:15:   required from here
test.cpp:6:17: error: redefinition of ‘template<class T1, class T2> auto
func(T1, T2)’
     friend auto func(T1 x, T2 y) {
                 ^~~~
test.cpp:6:17: note: ‘template<class T1, class T2> auto func(T1, T2)’
previously defined here
test.cpp: In instantiation of ‘auto func(T1, T2) [with T1 = A<int>; T2 =
A<double>; T = int]’:

-- 
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/20170101/08dd6a31/attachment.html>


More information about the llvm-bugs mailing list