Compiler Bug - Templates

I’ve been doing some pseudo-reflection stuff in CUDA using templates and ran into some bugs that crash the compiler. The problem seems to occur on both OS X and Linux, but in a slightly different form. Here’s a sample case:

template <typename T, typename U>

struct ttype

{ };  

template <typename T>

struct TypeInfo

{ };

template < > struct TypeInfo< float >

{ typedef float DeviceType; };

template < > struct TypeInfo< int >

{ typedef int DeviceType; };

template < typename t1, typename t2 >

struct TypeInfo< ttype<t1, t2> >

{

	typedef ttype< typename TypeInfo<t1>::DeviceType , typename TypeInfo<t2>::DeviceType > DeviceType;

};

	

int main()

{

	TypeInfo< ttype< float, int> >::DeviceType* pv; // Line 1

//	ttype< float, int >* pv; // Line 2

}

When Line 1 is uncommented, I have the error. If I expand the typedefs by hand, I get Line 2. If I comment Line 1 and uncomment Line 2, then there is no error. g++ compiles either without issue (g++ -x c++ test.cu -o test).

Moreover, if I try this with only one template argument for the ttype struct, there is no error.

Linux:


When I compile this on 64-bit Linux (Ubuntu 9.04.1, CUDA 2.1), the compiler segfaults:

$ nvcc test.cu -o test

test.cu(25): warning: variable "pv" was declared but never referenced

test.cu(25): warning: variable "pv" was declared but never referenced

Segmentation fault

It also gives me the same error message twice, which I find a bit odd.

$ nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2007 NVIDIA Corporation

Built on Wed_Dec__3_16:25:17_PST_2008

Cuda compilation tools, release 2.1, V0.2.1221
$ g++ --version

g++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)

Copyright (C) 2007 Free Software Foundation, Inc.

This is free software; see the source for copying conditions.  There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

OS X:


I tried it on OS X (10.5, where I have Cuda 2.0), but got a different error with the same code:

$ nvcc test.cu -o test

test.cu(25): warning: variable "pv" was declared but never referenced

test.cu(25): warning: variable "pv" was declared but never referenced

nvcc error   : 'cudafe++' died due to signal 10 (Bus error)
$ nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2007 NVIDIA Corporation

Built on Wed_Jul_16_12:14:50_PDT_2008

Cuda compilation tools, release 2.0, V0.2.1221
$ g++ --version

i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)

Copyright (C) 2005 Free Software Foundation, Inc.

This is free software; see the source for copying conditions.  There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.