我正在玩模板专业化,但我发现了一个我似乎无法解决的问题;这是我的代码:
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
我对么?
如果我是正确的,我猜这是函数模板不允许部分模板专业化的问题,那么解决方案是什么,制作一个结构,并在上面使用专业化?