我正在尝试模板特化,但发现了一个无法解决的问题;以下是我的代码:
template<int length, typename T>
void test(T* array)
{
...
test<length-1>(array);
}
template<typename T>
void test<0>(T* array)
{
return;
}
我想要做的是在模板中传递待处理内容的长度。
问题是,这样编译会使输出无限延迟:
a.cpp:83:43: error: template-id 'test<0>' in declaration of primary template
a.cpp: In function 'void test(T*) [with int length= -0x000000081, T = int]':
a.cpp:77:9: instantiated from 'void test(T*) [with int length= -0x000000080, T = int]'
a.cpp:77:9: instantiated from 'void test(T*) [with int length= -0x00000007f, T = int]'
a.cpp:77:9: [ skipping 151 instantiation contexts ]
a.cpp:77:9: instantiated from 'void test(T*) [with int length= 28, T = int]'
a.cpp:77:9: instantiated from 'void test(T*) [with int length= 29, T = int]'
...
a.cpp: In function 'void test(T*) [with int length= -0x000000082, T = int]':
a.cpp:77:9: instantiated from 'void test(T*) [with int length= -0x000000081, T = int]'
a.cpp:77:9: instantiated from 'void test(T*) [with int length= -0x000000080, T = int]'
最后两行与前面的行几乎相同。
在我看来,它似乎没有捕捉到专业化,因此:
a.cpp:83:43: error: template-id 'test<0>' in declaration of primary template
我猜想您的问题是关于函数模板的部分特化问题,是吗?如果是这样,解决方案是使用结构体并对其进行特化。
那么,我的猜测正确吗?