llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.88k stars 11.93k forks source link

invalid `use of class template %s requires template arguments` #18600

Open llvmbot opened 10 years ago

llvmbot commented 10 years ago
Bugzilla Link 18226
Version 3.2
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

On the following code clang gives error:

1.cpp:16:26: error: use of class template y requires template arguments
     friend void x<U>::f(y);
                         ^

But gcc and msvc compiles this code without any error.

template <typename U>
struct y;

template <typename T>
struct x
{
    void f(y<T> a)
    {
        a.a = 5;
    }
};

template <typename U>
struct y
{
     friend void x<U>::f(y); // error here

private:
     int a;
};

int main()
{
    x<int>().f(y<int>());
}
llvmbot commented 10 years ago

Smaller repro, fails to compile with clang r196769:

template <typename T>
struct x;

template<typename T>
struct y {
    friend void x<T>::f(y);
};

This results in:

% ~/LLVM/build/Release+Asserts/bin/clang++ -c clang.cppclang.cpp:6:25: error: use of class template 'y' requires template arguments
    friend void x<T>::f(y);
                        ^
clang.cpp:5:8: note: template is declared here
struct y {
       ^
1 error generated.

Interestingly, if the befriended method in x is called y instead of f, I get an additional error:

template <typename T>
struct x;

template<typename T>
struct y {
    friend void x<T>::y(y);
};
% ~/LLVM/build/Release+Asserts/bin/clang++ -c clang.cpp
clang.cpp:6:25: error: use of class template 'y' requires template arguments
    friend void x<T>::y(y);
                        ^
clang.cpp:5:8: note: template is declared here
struct y {
       ^
clang.cpp:6:23: error: constructor cannot have a return type
    friend void x<T>::y(y);
           ~~~~       ^
2 errors generated.