C ++模板功能的专业化:“非法使用显式模板参数" [英] C++ template specialization of function: "illegal use of explicit template arguments"
本文介绍了C ++模板功能的专业化:“非法使用显式模板参数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下模板专业化代码:
template<typename T1, typename T2>
void spec1()
{
}
测试案例1:
template< typename T1> //compile error
void spec1<int>()
{
}
测试案例2:
template< typename T2> //compile error
void spec1<int>()
{
}
生成以下编译错误:
错误C2768:'spec1':非法使用显式模板参数
error C2768: 'spec1' : illegal use of explicit template arguments
有人知道为什么吗?
推荐答案
功能模板不能部分专门化,只能完全地专门化,即:
Function templates cannot be partially specialised, only fully, i.e. like that:
template<>
void spec1<char, int>()
{
}
为什么不能部分专门设置功能模板,您可能需要阅读.
For why function templates cannot be partially specialised, you may want to read this.
当您部分专长时(仅适用于类),您必须这样做:
When you specialise partially (only possible for classes), you'd have to do it like that:
template <typename T1>
class class1<T1, int>
{
};
所以您必须再次列出T1
.
您的专业化写作方式,对于spec1<int, int>
来说是模棱两可的.
The way your specialisations are written, they would be ambiguous for spec1<int, int>
.
这篇关于C ++模板功能的专业化:“非法使用显式模板参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文