nvcc 4.0 : unable to declare a "friend std::ostream& operator<< <>" meth

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.

I checked with the compiler team and they stated that this should work as desired with the compiler that shipped with CUDA 4.1. Please try with the CUDA 4.1 compiler, and let me know if you continue to encounter this issue after upgrading to CUDA 4.1. Thanks.

Hi,

I’ve just updated my nvcc compiler (and also my driver) and now everything is ok: I can compile my simple example and I can integrate my external C++ libraries…

My nvcc version :

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2011 NVIDIA Corporation

Built on Thu_Jan_12_14:41:45_PST_2012

Cuda compilation tools, release 4.1, V0.2.1221

Thank you very much

M.D.