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

Templates 使用nvcc在CUDA中编译模板函数时出错,templates,compilation,compiler-errors,cuda,nvcc,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() {

我有以下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>>>();
}
编程指南的D.1.4节(4.0,我正在使用的工具包版本)建议模板应该可以工作,但我无法让它们工作


有人能建议对此代码进行修改,使其能够编译(而不删除模板!)吗?

这是您想要的,还是我把您的问题搞错了

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;
}
enum方法{
METH_0=0,
冰毒1
};
模板
内联u_设备_;内联测试_函数()
{
返回int(METH);
}
模板
内联u_设备_;内联测试_函数()
{
返回-42;
}

这就是你想要的,还是我把你的问题搞错了

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;
}
enum方法{
METH_0=0,
冰毒1
};
模板
内联u_设备_;内联测试_函数()
{
返回int(METH);
}
模板
内联u_设备_;内联测试_函数()
{
返回-42;
}

您的测试函数定义错误:

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>>>();
}
enum方法{
METH_0=0,
冰毒1
};
模板
__装置__
内联
int test_func()
{
返回int(METH);
}
__全局无效测试内核()
{
test_func();
}
无效测试()
{
test_kernel();
}

您的测试函数定义错误:

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>>>();
}
enum方法{
METH_0=0,
冰毒1
};
模板
__装置__
内联
int test_func()
{
返回int(METH);
}
__全局无效测试内核()
{
test_func();
}
无效测试()
{
test_kernel();
}

为什么声明
int test\u func()
包含一个模板参数?我希望有许多不同的方法可以执行不同的操作,并根据模板类型选择它们。目前只有一个简单的模板函数,但一旦我能够编译它,我将用更复杂的专门化来扩充METH_0和METH_1。为什么声明
int test_func()
包含一个模板参数?我想有许多不同的方法来做不同的事情,并根据模板类型选择它们。目前只有一个简单的模板函数,但一旦我能够编译它,我将用更复杂的专门化来扩充METH_0和METH_1。这很有效,谢谢。我接受了另一个答案,因为它更具解释性。这很有效,谢谢。我接受了另一个答案,因为它更具解释性。