理解类模板专业化示例的隐式实例化

时间:2017-12-14 13:53:34

标签: c++ metaprogramming declaration definition template-specialization

所以标准的this section给出了这个例子(我的问题是内联的):

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飞来飞去而感到困惑,但如果有人可以一行一行地告诉我发生的事情会非常有帮助。

2 个答案:

答案 0 :(得分:3)

template<class X, class Y> struct Inner;      
// Where are X and Y from? Is this defining a struct?

这是模板结构Inner的声明,XY是模板参数。

template<class Y> struct Inner<T, Y>;         
// Is this defining a struct specialization? If so what is Y?

这是部分特化的声明,Y是模板参数。

template<class Y> struct Inner<T, Y> { };     
// I feel like this is a redefinition of the line above, could it ever not be?

这是部分特化的定义,所以这里没有重新定义。

然后将它们用作:

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

答案 1 :(得分:1)

你走了:

 pd.Series(['A', 'B']).isin(df.columns).all()