使用nvcc在CUDA中编译模板函数时出错

时间:2011-09-16 14:54:46

标签: templates compilation compiler-errors cuda nvcc

我有以下CUDA代码:

enum METHOD_E {
    METH_0 = 0,
    METH_1
};

template <enum METHOD_E METH>
inline __device__ int test_func<METH>()
{
    return int(METH);
}

__global__ void test_kernel()
{
    test_func<METH_0>();
}

void test()
{
    test_kernel<<<1, 1>>>();
}

编译时出现以下错误:

>nvcc --cuda test.cu
test.cu
test.cu(7): error: test_func is not a template

test.cu(14): error: identifier "test_func" is undefined

test.cu(14): error: expected an expression

3 errors detected in the compilation of "C:/Users/BLAH45~1/AppData/Local/Temp/tm
pxft_00000b60_00000000-6_test.cpp1.ii".

编程指南的D.1.4节(4.0,我正在使用的工具包的版本)建议模板应该有效,但是我无法使用它们。

任何人都可以建议更改此代码,使其编译(不删除模板!)?

2 个答案:

答案 0 :(得分:2)

您的test_func定义错误:

test_func()应该只是test_func()

这对我有用:

enum METHOD_E {
    METH_0 = 0,
    METH_1
};

template < enum METHOD_E METH>
__device__
inline
int test_func ()
{
    return int(METH);
}

__global__ void test_kernel()
{
    test_func<METH_0>();
}

void test()
{
    test_kernel<<<1, 1>>>();
}

答案 1 :(得分:1)

这是你想要的,还是我的问题出错?

enum METHOD_E {
    METH_0 = 0,
    METH_1
};

template <enum METHOD_E METH>
inline __device__ int test_func()
{
    return int(METH);
}

template <>
inline __device__ int test_func<METH_0>()
{
    return -42;
}
相关问题