了解类模板专业化示例的隐式实例化
Understanding an Implicit Instantiation of Class Template Specialization Example
,因此标准的这一部分给出了此示例(我的问题是内联):
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
的飞行感到困惑,但是如果有人可以排队并告诉我发生的事情将非常有帮助。
template<class X, class Y> struct Inner; // Where are X and Y from? Is this defining a struct?
这是模板的声明结构Inner
,X
和Y
是模板参数。
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
您去这里:
template<class T, class U>
struct Outer {
// Forward-declares a struct with two template parameters
template<class X, class Y> struct Inner;
// Forward-declares a partial specialization of Inner
// (Y is its only template parameter)
template<class Y> struct Inner<T, Y>;
// Defines the forward-declared partial specialization
template<class Y> struct Inner<T, Y> { };
// Declares and defines another partial specialization
// (Y is its only template parameter)
template<class Y> struct Inner<U, Y> { };
};
相关文章:
- 从C++实例化QML
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 约束和显式模板实例化
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 对象实例化调用构造函数的次数太多
- 如何使用非默认构造函数实例化模板化类
- 在C 中,当我对其进行专业化时会实例化模板
- Clang和Gcc不同意实例化后的显式专业化
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 了解类模板专业化示例的隐式实例化
- 实例化和专业化的定义是相互依赖的
- 明确的专业化;'std::hash<_Kty>' 已被实例化
- 模板功能实例化和专业化
- 实例化错误后成员函数模板的专业化,以及成员函数的顺序
- 将专业化定义为另一个类的实例化
- 专业化的隐式实例化
- 实例化错误后如何避免专业化
- 为什么模板专业化中的显式实例化会给我带来错误