[英]C++ template specialization of function: “illegal use of explicit template arguments”
The following template specialization code: 以下模板专用代码:
template<typename T1, typename T2>
void spec1()
{
}
Test case 1: 测试用例1:
template< typename T1> //compile error
void spec1<int>()
{
}
Test case 2: 测试案例2:
template< typename T2> //compile error
void spec1<int>()
{
}
generates the following compilation error: 生成以下编译错误:
error C2768: 'spec1' : illegal use of explicit template arguments
错误C2768:'spec1':非法使用显式模板参数
Does anyone know why? 有人知道为什么吗?
Function templates cannot be partially specialised, only fully, ie 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>
{
};
so you have to list T1
again. 因此您必须再次列出
T1
。
The way your specialisations are written, they would be ambiguous for spec1<int, int>
. 专业化的编写方式,对于
spec1<int, int>
来说是模棱两可的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.