gpt4 book ai didi

c++ - 了解类模板特化示例的隐式实例化

转载 作者:行者123 更新时间:2023-11-27 22:42:16 24 4
gpt4 key购买 nike

所以 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<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

关于c++ - 了解类模板特化示例的隐式实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com