了解类模板专门化示例的隐式实例化 [英] Understanding an Implicit Instantiation of Class Template Specialization Example
问题描述
因此标准的本部分给出了此示例(我的问题是内联的):
So this section of the standard gives this example (My questions are inline):
template<class T, class U>
struct Outer {
template<class X, class Y> struct Inner; // Where are X and Y from? Is this defining a struct?
template<class Y> struct Inner<T, Y>; // Is this defining a struct specialization? If so what is Y?
template<class Y> struct Inner<T, Y> { }; // I feel like this is a redefinition of the line above, could it ever not be?
template<class Y> struct Inner<U, Y> { };
};
诚然,我不明白标准的链接部分,因为我不明白这里发生了什么.我想我对所有template
飞来飞去感到困惑,但是如果有人可以一行一行地告诉我正在发生的事情,那将非常有帮助.
Admittedly I can't understand the linked section of the standard cause I can't understand what's happening here. I think I'm just getting confused by all the template
s flying around, but If someone could just go line by line and tell me what's happening that would be very helpful.
推荐答案
template<class X, class Y> struct Inner;
// Where are X and Y from? Is this defining a struct?
这是模板结构Inner
的声明,X
和Y
是模板参数.
This is the declaration of template struct Inner
, X
and Y
is the template parameters.
template<class Y> struct Inner<T, Y>;
// Is this defining a struct specialization? If so what is Y?
这是部分专业化的声明,Y
是模板参数.
This is the declaration of a partial specialization, Y
is the template parameter.
template<class Y> struct Inner<T, Y> { };
// I feel like this is a redefinition of the line above, could it ever not be?
这是部分专业化的定义,因此这里没有重新定义.
This is the definition of the partial specialization, so no redefinition here.
然后将它们用作:
Outer<foo1, foo2>::Inner<foo3, foo4>* i1;
// the primary template is used, with T=foo1, U=foo2, X=foo3, Y=foo4
// btw the primary template is not defined in this example
Outer<foo1, foo2>::Inner<foo1, foo3> i2;
// the 1st partial specialization is used, with T=foo1, U=foo2, Y=foo3
Outer<foo1, foo2>::Inner<foo2, foo3> i3;
// the 2st partial specialization is used, with T=foo1, U=foo2, Y=foo3
这篇关于了解类模板专门化示例的隐式实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!