为什么显式专业化和部分专业化之间的语法存在差异?
示例:
template <typename T, typename U>
struct A {
void Print() {}
};
template <>
void A<int, float>::Print() {} // Okay
template <typename T>
void A<T, char>::Print() {} // Will produce error
问题:
我知道您必须在上面的代码中定义类模板部分特化才能使其工作,并且我还从标准中知道 类模板部分特化的成员与主模板 (§ 14.5.5.3)
。但是,为什么显式特化和部分特化之间的语法存在差异?
Example:
template <typename T, typename U>
struct A {
void Print() {}
};
template <>
void A<int, float>::Print() {} // Okay
template <typename T>
void A<T, char>::Print() {} // Will produce error
Question:
I know that you have to define the class template partial specialization in the above code for it to work and I also know from the standard that The members of the class template partial specialization are unrelated to the members of the primary template (§ 14.5.5.3)
. However, why the difference in syntax between a explication specialization and a partial specialization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能部分地专门化函数模板,而只能完全地专门化。
第一个实例利用了类模板的成员函数本身就是函数模板这一事实,因此限制适用于它们。
当您部分特化类模板时,您将拥有一个全新的类模板,您必须重新定义它。
You cannot specialize function templates partially, only fully.
The first instance makes use of the fact that member functions of class templates are themselves function templates, and thus the restriction applies to them.
When you partially-specialize the class template, you have an entirely new class template, which you have to define anew.
您正在:
void Print() {}
时它已经被定义,您会看到有{}
.templatevoid Print()
事实上,即使在声明函数时没有定义该函数,仍然会出现错误;你的声明和定义不匹配,编译器不会能够找到原始模板的定义或专用模板的声明与
结构相关的函数的专用模板函数也必须具有专用结构,此代码有效:
因为模板函数已在中声明。它是模板结构。
You are:
void Print() {}
, you see there are{}
.template <typename T, typename U> void Print()
In fact, even if you haven't defined the function when declared it, you will still have an error since your declaration and definition do not match, the compiler will not be able to find a definition for the original template, or a declaration for the specialized template.
A specialized template function for a function that is related to a struct must have a specialized struct as well, This code works:
Because template function has been declared in it's template struct.