[英]Does typedef-ing a template type instantiate it?
If I use typedef
to rename a specialization of a template class (or struct), will that instantiate the template of that type in the assembly codegen? 如果我使用
typedef
重命名模板类(或结构)的特化,那么它是否会在汇编代码中实例化该类型的模板? For example: 例如:
template<class T> struct Key
{
float _time = 0.0f;
T _value = T();
};
typedef Key<glm::vec3> VectorKey;
typedef Key<glm::quat> QuatKey;
Will this create the Key<glm::vec3>
and Key<glm::quat>
structs in my file so that I now have (essentially) 这会在我的文件中创建
Key<glm::vec3>
和Key<glm::quat>
结构,这样我现在(基本上)
template<class T> struct Key
{
float _time = 0.0f;
T _value = T();
};
struct VectorKey
{
float _time = 0.0f;
glm::vec3 _value = glm::vec3();
};
struct QuatKey
{
float _time = 0.0f;
glm::quat _value = glm::quat();
};
I'm wondering if it's a bad idea to include a typedef
in the header because if this happened, any files that included this header would then have more code included that is unnecessary. 我想知道在标题中包含
typedef
是不是一个坏主意,因为如果发生这种情况,那么包含此标题的任何文件都将包含更多不必要的代码。
No, typedefs are purely syntactical. 不,typedef纯粹是语法上的。 Templates are only instantiated when actually used.
模板仅在实际使用时进行实例化。
As per the C++14 standard (well, the November draft because I can't link directly to the ISO spec) (emphasis mine) 按照C ++ 14标准(好吧, 11月草案因为我无法直接链接到ISO规范)(强调我的)
7.1.3 The
typedef
specifier7.1.3
typedef
说明符
[...][...]
A name declared with thetypedef
specifier becomes a typedef-name .使用
typedef
说明符声明的名称将成为typedef-name 。 Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type.在其声明的范围内, typedef-name在语法上等同于关键字,并按照第8章中描述的方式命名与标识符关联的类型。因此, typedef-name是另一种类型的同义词。 A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does .
typedef-name不会像类声明(9.1)或枚举声明那样引入新类型 。
Whereas the term "instantiating a template" is the same thing as "introducing a new type". 而术语“实例化模板”与“引入新类型”是相同的。
Imagine that during compilation that each new class/type definition encountered adds it to a "list-of-types". 想象一下,在编译期间遇到的每个新类/类型定义都将它添加到“类型列表”中。 Whenever a template is encountered it is not added to the "list-of-types" until instantiated.
每当遇到模板时,它都不会被添加到“list-of-types”,直到实例化为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.