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

问题描述

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

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

以下原因导致 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]

§8.3.5 \ 9函数[dcl.fct]

§ 8.3.5\9 Functions [dcl.fct]

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

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

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

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".实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:42