问题描述
我有代码,如
template <class T> class Widget
{
void fun() {}
}
//Okay: specialization of a member function of widget
template <> void Widget<char>:: fun()
{
void fun() {}
}
但是,下面是错误,因为我被告知。但不明白为什么。
But, below is error as I am been told. But not understand why.
template<class T, class U> class Gadget
{
void fun() {}
}
//Error! cannot partially specialize a member function of Gadget
template<class U> void Gadget<char,U>::fun()
{
..specialized implementation
}
为什么是第二个错误?如何改变它,使它正确?
感谢!!!!
Why is the second wrong? how to change it to make it right?thanks!!!!
推荐答案
不可能部分专门化只有一个成员函数,专业化整个班级。这是在 C ++
中的工作原理。
It is impossible to partially specialize just one single member function, you have to partially specialize the whole class. That's how things work in C++
.
原因是你不能有部分专用的函数,而且成员函数本身就是函数。通过部分专门化整个类,成员函数将看起来像较少类型的模板(在部分专用类)。
The reason is that you cannot have partially specialized functions, and member functions are themselves functions. By partially specializing the whole class, the member functions will "look" like templates with fewer types (in that partial specialized class).
为什么你不能有部分专门的函数另一个故事,我没有一个好的答案/理解为什么这是强制的。
Why you cannot have partially specialized functions is another story, and I don't have a good answer/understanding why is this enforced.
关于使它工作,为什么你不部分专门化的类,然后只重新定义所需的函数。
About making it work, why don't you partially specialize the class, then re-define only the function that you need.
这篇关于为什么不能部分专门化成员函数,在c ++模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!