Template bug

The following code compiles fine using g++ (4.1.2 20080704 (Red Hat 4.1.2-44)), but does not compile using nvcc (2.3 or 3.0):

[codebox]template<int DIM, class T>

void get(const T& x) {

x.get<DIM>();

}

struct A {

template<int DIM>

void get() const {}

};

int main()

{

A a;

get<3>(a);

}[/codebox]

The error given by nvcc is:

[codebox]In function ‘void get(const T&) [with int DIM = 3, T = A]’:

instantiated from here

error: no matching function for call to ‘A::get() const’[/codebox]

Referring to the 3rd line of code.

Any ideas why this might be? Compiler bug?

Cheers,

Ben

I think you mean the following, right?

[codebox]

struct A {

template<int DIM>

void get() const {}

};

int main()

{

A a;

a.get<3>();

}

[/codebox]

This code seems to compile with a recent nightly compiler:

[codebox]

jhoberock@jhoberock-dt:~$ cat get.cu ; nvcc get.cu ; echo ; nvcc --version

struct A {

template<int DIM>

void get() const {}

};

int main()

{

A a;

a.get<3>();

}

nvcc: NVIDIA ® Cuda compiler driver

Copyright © 2005-2010 NVIDIA Corporation

Built on Fri_May_28_21:28:40_CDT_2010

Cuda compilation tools, release 3.1, V0.2.1221

[/codebox]

Have you tried nvcc 3.1b?

Whoops, you’re right – I copied your code wrong. I can reproduce this. I’ll file a bug.

Just noticed something about the definition of void get(const T&):

template<int DIM, class T>

void get(const T& x) {

	x.get<DIM>();

}

But the calling line in main is

get<3>(a);

Shouldn’t there be two parameters in the template?

get<3, A>(a);

The compiler can figure out the what the 2nd parameter should be from the context.

Thanks Jared.