[英]Partial specialization with class template
I was reading the answer given here: https://stackoverflow.com/a/23550519/1938163 我正在阅读此处给出的答案: https : //stackoverflow.com/a/23550519/1938163
and I'm wondering why the last line is a template struct's partial specialization 我想知道为什么最后一行是模板结构的部分专业化
template<typename T> MyClass { public: };
template <typename T> struct Foo {void foo() {}};
template<> struct Foo<int> {void foo() { std::cout << "fooint";} };
// Why is this a partial specialization?
template<typename T> struct Foo< MyClass<T> > {void foo() {std::cout << "foo myclass";} };
I thought that a partial specialization consisted in replacing parameter arguments completely like the following 我认为部分专长在于完全替换参数参数,如下所示
template <typename T, typename G> struct FooBar {};
template <typename G> struct FooBar<int, G>{}; // Partial specialization
Full specialization is when template parameters are all replaced by concrete types and the template parameter list is empty. 完全专业化是指模板参数全部由具体类型替换并且模板参数列表为空。
MyClass<T>
is not concrete; MyClass<T>
不是具体的; and 和
template<typename T> struct Foo<MyClass<T>> { ... };
it is still parametrized by T
, and the template parameter list still contains T
. 它仍然由
T
参数化,并且模板参数列表仍然包含T
For instance, 例如,
template<> struct Foo<MyClass<int>> { ... };
would be a full specialization of Foo
that is more specialized than Foo<MyClass<T>>
. 将是
Foo
的完全专业化,它比Foo<MyClass<T>>
更专业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.