模板类成员函数只专门化 [英] template class member function only specialization
问题描述
我正在阅读模板的完整指南,它说明了以下内容:
I am reading the Complete Guide on Templates and it says the following:
它正在谈论类模板专业化。
Where it is talking about class template specialization.
虽然
可能专门用于单个成员
函数,但一旦你这样做,你
就不能再专门化整个
类。
However, if you specialize a class template, you must also specialize all member functions. Although it is possible to specialize a single member function, once you have done so, you can no longer specialize the whole class.
我真的想知道这是如何是真的,因为你可以专门化没有任何成员函数。它是说,你不能有一个专门化只有一个成员函数,然后另一个与所有成员函数?
I'm actually wondering how this is true, cause you can specialize without any member functions at all. Is it saying that you cannot have a specialization with only one member function and then another with all member functions?
有人可以澄清吗?
推荐答案
对于以下情况:
template <typename T>
struct base {
void foo() { std::cout << "generic" << std::endl; }
void bar() { std::cout << "bar" << std::endl; }
};
template <>
void base<int>::foo() // specialize only one member
{
std::cout << "int" << std::endl;
}
int main() {
base<int> i;
i.foo(); // int
i.bar(); // bar
}
完成后,您就无法将完整模板任何其他事物,因此
Once that is done, you cannot specialize the full template to be any other thing, so
template <>
struct base<int> {}; // error
这篇关于模板类成员函数只专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!