在声明中,"std :: vector< X>"f();"是"std :: vector X".实例化? [英] In the declaration "std::vector<X> f();", is "std::vector<X>" an instantiation?

查看:37
本文介绍了在声明中,"std :: vector< X>"f();"是"std :: vector X".实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++语言标准在标准库中声明了与模板组件有关的以下内容:

The C++ Language Standard states the following concerning template components in the Standard Library:

效果是不确定的...如果在实例化模板组件时将不完整的类型用作模板参数,除非该组件特别允许(C ++ 11§17.6.4.8/2).

The effects are undefined...if an incomplete type is used as a template argument when instantiating a template component, unless specifically allowed for that component (C++11 §17.6.4.8/2).

以下原因导致 std :: vector 类模板的实例化吗?

Does the following cause instantiation of the std::vector class template?

class X;
std::vector<X> f(); // Declaration only; we will define it when X is complete

以另一种方式询问,在函数声明中 std :: vector< X>f(); ,是否使用参数 X 实例化了 std :: vector ?或者,是否在未使用或定义 f()之前未实例化 std :: vector< X> ?

To ask it another way, in the function declaration std::vector<X> f();, is std::vector instantiated with the argument X? Or, is std::vector<X> not instantiated until f() is odr-used or defined?

同样,以下内容是否会导致 std :: vector 类模板的实例化?

Likewise, does the following cause instantiation of the std::vector class template?

class X;
typedef std::vector<X> XVector; // We will complete X before we use XVector

虽然在这些示例中使用 std :: vector ,但该问题同样适用于所有模板.

While I use std::vector in these examples, the question applies equally to all templates.

推荐答案

§14.7.1 \ 1隐式实例化[temp.inst]

§ 14.7.1\1 Implicit instantiation [temp.inst]

除非明确指定了类模板专业化实例化(14.7.2)或显式专门化(14.7.3)的类当在需要以下条件的上下文中引用专业化完全定义的对象类型或类的完整性类型会影响程序的语义.隐式实例化类模板的特殊化导致隐式实例化声明,而不是定义或默认参数,类成员函数,成员类,静态数据成员和会员模板;它导致了隐式实例化成员匿名工会的定义.除非有班级成员模板或成员模板已被显式实例化或明确的专业化,成员的专业化是隐式的在以下情况下引用专业化时实例化要求成员定义存在; 尤其是静态数据的初始化(以及任何相关的副作用)除非静态数据成员本身用于一种需要静态数据成员的定义存在的方式.

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program. The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, static data members and member templates; and it causes the implicit instantiation of the definitions of member anonymous unions. Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

§8.3.5 \ 9函数[dcl.fct]

§ 8.3.5\9 Functions [dcl.fct]

类型不得在返回或参数类型中定义.一个的类型参数或函数定义的返回类型不得为除非函数不完整的类类型(可能是cv限定)定义嵌套在该类的成员规范内(包括在类中定义的嵌套类中的定义).

Types shall not be defined in return or parameter types. The type of a parameter or the return type for a function definition shall not be an incomplete class type (possibly cv-qualified) unless the function definition is nested within the member-specification for that class (including definitions in nested classes defined within the class).

第3.1 \ 2节声明和定义[basic.def]

§ 3.1\2 Declarations and definitions [basic.def]

声明是一个定义,除非它声明的函数没有指定函数的主体(8.4),其中包含外部说明符(7.1.1)或链接规范25(7.5),且都不是初始化程序也不是函数体,它在类中声明了静态数据成员定义(9.4),它是类名声明(9.1),它是opaque-enum-declaration(7.2),或者它是typedef声明(7.1.3),使用声明(7.3.3),静态声明声明(条款7),属性声明(条款7),空声明(条款7),或使用指令(7.3.4).

A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a function-body, it declares a static data member in a class definition (9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attribute-declaration (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).

仅当需要 时才实例化.我在任何地方都找不到清晰的定义,但是第二句引述说那些声明不是定义,对我来说似乎是相同的.

It's only instantiated if it's required. I couldn't find a clear definition anywhere, but the second quote says that those declaratations are not definitions, which seems to be the same to me.

这篇关于在声明中,"std :: vector&lt; X&gt;"f();"是"std :: vector X".实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆