Hi,
I would like to compile a template class but nvcc fails when I try to define the friend std::ostream& operator<< <>(…) method. A simple example :
#include <iostream>
template<typename T>
class Test;
template<typename T>
std::ostream& operator<< (std::ostream& out, const Test<T>& t);
template<typename T>
bool operator== (const Test<T>& t1, const Test<T>& t2);
template<typename T>
class Test
{
public:
Test() { }
friend std::ostream& operator<< <>(std::ostream& out, const Test<T>& t);
friend bool operator== <>(const Test<T>& t1, const Test<T>& t2);
};
template<typename T>
std::ostream& operator<< (std::ostream& out, const Test<T>& t)
{
out << "inside test ";
return out;
}
template<typename T>
bool operator== (const Test<T>& t1, const Test<T>& t2)
{
return true;
}
int main(int argc, char **argv)
{
Test<int> a1;
std::cout << a1 << std::endl;
Test<int> a2;
std::cout << (a1==a2) << std::endl;
return 0;
}
This code compiles with g++ but fails if I try to compile it with nvcc. nvcc returns the following errors :
test.cu(19): error: expected an operator
test.cu(19): error: invalid friend declaration
test.cu(19): error: expected a ";"
line 19 :
friend std::ostream& operator<< <>(std::ostream& out, const Test<T>& t);
Is it a bug or I’ve just made something wrong ?
Note : this code compiles if I comment this line 19, but it’s not a solution because I would like to compile my code with others libraries (and I don’t want to modify the libraries files).
nvcc informations :
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2011 NVIDIA Corporation
Built on Thu_May_12_11:09:45_PDT_2011
Cuda compilation tools, release 4.0, V0.2.1221
Thanks a lot,
M.D.