문제

I was reading the answer given here: 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; and

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

it is still parametrized by T, and the template parameter list still contains T. For instance,

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

would be a full specialization of Foo that is more specialized than Foo<MyClass<T>>.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top