简体   繁体   English

C ++模板功能的专业化:“非法使用显式模板参数”

[英]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.

相关问题 嵌套模板特化导致“非法使用显式模板参数”? - Nested template specialization results in “Illegal use of explicit template arguments”? 函数模板c ++的显式特化 - explicit specialization for function template c++ 函数模板显式特化c ++ - Function template explicit specialization c++ 为什么这个 C++ 显式模板专业化代码是非法的? - Why is this C++ explicit template specialization code illegal? C++ 模板 function 具有多种模板类型的显式特化 - C++ template function explicit specialization with multiple template types 模板类中模板函数的显式特化的 C++ 语法? - C++ syntax for explicit specialization of a template function in a template class? 具有可变参数模板参数的成员函数的显式特化 - Explicit specialization of member function with variadic template arguments c ++模板函数专门化 - 错误的模板参数数量? - c++ template function specialization - wrong number of template arguments? C ++模板显式专业化-调用现有成员函数 - C++ template explicit specialization - calling existing member function 在为类方法执行指针部分特化时,获取“非法使用显式模板参数” - Getting “illegal use of explicit template arguments” when doing a pointer partial specialization for a class method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM