类模板的部分特化

Partial specialization with class template

本文关键字:      更新时间:2023-10-16

我正在阅读这里给出的答案:https://stackoverflow.com/a/23550519/1938163

,我想知道为什么最后一行是模板结构体的部分专门化

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";} };

我认为部分专门化包括完全替换形参参数,就像下面的

template <typename T, typename G> struct FooBar {};
template <typename G> struct FooBar<int, G>{}; // Partial specialization

完全特化是指模板参数全部被具体类型替换,模板参数列表为空。MyClass<T>不具体;和

template<typename T> struct Foo<MyClass<T>> { ... };

参数仍然是T,模板参数列表中仍然包含T。例如,

template<> struct Foo<MyClass<int>> { ... };

将是Foo的完全专门化,比Foo<MyClass<T>>更专门化。

相关文章:
  • 没有找到相关文章