简体   繁体   English

了解C ++中的初始化顺序

[英]Understanding initialization order in C++

Is cppreference.com an official page in any way that one can rely on in terms of the documentation presented there? cppreference.com是一个官方页面,可以依赖于那里提供的文档吗? Or should I simply read the language docs in case I need 100% certainty? 或者我应该只是阅读语言文档,以防我需要100%的确定性?

Look at this article. 看看这篇文章。 First, several methods of variable initialization have been listed: 首先,列出了几种变量初始化方法:

Depending on context, the initializer may invoke one of the following: 根据上下文,初始化程序可以调用以下之一:

  • Value initialization 值初始化
  • Direct initialization 直接初始化
  • Copy initialization 复制初始化
  • List initialization 列表初始化
  • Aggregate initialization 聚合初始化
  • Reference initialization 参考初始化

If no initializer is provided, the rules of default initialization apply. 如果未提供初始化程序,则应用默认初始化规则。

The next part explains two phases of initialization for non-local variables: static and dynamic. 下一部分解释了非局部变量的两个初始化阶段:静态和动态。 Zero initialization happens in the static phase. 零初始化发生在静态阶段。 It doesn't say a word when default initialization takes place though. 虽然默认初始化发生时没有说出一个字。

When you go to the description of default initialization , there's an example saying: 当你转到默认初始化的描述时,有一个例子说:

int n; // static non-class, a two-phase initialization is done:
       // 1) zero initialization initializes n to zero
       // 2) default initialization does nothing, leaving n being zero

Based on this I'd assume default initialization happens some time after zero-initialization (zero init is in the static initialization phase). 基于此,我假设默认初始化在零初始化之后的某个时间发生(零初始化处于静态初始化阶段)。 But this is the only place where actually somebody mentioned when default init takes place relative to other initializations. 但是这是实际有人在默认init相对于其他初始化时提到的地方。

Is the cppreference just incomplete, or the information is there and I can't find it? cppreference是不完整的,还是信息在那里,我找不到它?

cppreference is not "official"; cppreference不是“官方的”; it is a wiki. 这是一个维基。 Anyone can edit. 任何人都可以编辑 It is generally considered to be of fairly high quality, but you should always treat the standard and nothing but the standard as your canonical, authoritative source for information. 它通常被认为是相当高的质量,但你应该始终把标准和标准作为你的规范,权威的信息来源。

As for initialisation, your interpretation is correct: zero-initialisation happens first, then whatever other kind of initialisation is required (be it default initialisation or something else). 至于初始化,您的解释是正确的:首先进行零初始化, 然后进行任何其他类型的初始化(无论是默认初始化还是其他)。

This is the only interpretation I can see for the quoted text so I don't see what part of the article is "incomplete". 这是我可以看到引用文本的唯一解释,所以我没有看到文章的哪一部分是“不完整的”。

Is cppreference.com an official page in any way cppreference.com是一个官方页面

No, not at all. 一点都不。

that one can rely on in terms of the documentation presented there? 一个人可以依赖于那里提供的文件?

It is the best free online C++ documentation I am aware of, and it has good reputation on Stackoverflow. 这是我所知道的最好的免费在线C ++文档,它在Stackoverflow上有很好的声誉。

Or should I simply read the language docs in case I need 100% certainty? 或者我应该只是阅读语言文档,以防我需要100%的确定性?

The C++ ISO standard is the one and only official source, so only that can give you 100% certainty. C ++ ISO标准是唯一的官方来源,因此只有这样才能给您100%的确定性。 The standard document is not free, but drafts are free and give you, say, 99.99% certainty. 标准文件不是免费的,但草稿是免费的 ,比如99.99%的确定性。

Of course, "100% certainty" may be misleading anyway. 当然,“100%确定性”可能会产生误导。 Standards are written by human beings and are never free of errors or contradictions, especially when you are dealing with a language as complex as C++. 标准是由人类编写的,并且永远不会出现错误或矛盾,尤其是在处理像C ++这样复杂的语言时。 If they were, then there would not be any official issues . 如果是,那就不会有任何官方问题 There is even a relatively recent official defect report for zero-initialisation . 甚至还有一个相对较新的关于零初始化的官方缺陷报告

And you also have to keep in mind that "the C++ ISO standard" could mean "one of the C++ ISO standards which have been published so far" (1998, 2003, 2011, 2014), "the current C++ ISO standard" (2014) or perhaps even the upcoming one (2017). 您还必须记住, “C ++ ISO标准”可能意味着“迄今为止已发布的C ++ ISO标准之一” (1998年,2003年,2011年,2014年), “当前的C ++ ISO标准” (2014年) )或者甚至是即将到来的(2017年)。

So maybe the sentence above should be rephrased into: A C++ ISO standard is the one and only official source. 所以,也许上面的句子应该被改写成:A C ++ ISO标准是唯一的官方消息。

But this is the only place where actually somebody mentioned when default init takes place relative to other initializations. 但是这是实际有人在默认init相对于其他初始化时提到的地方。

That's not true. 这不是真的。 For example, http://en.cppreference.com/w/cpp/language/zero_initialization says: 例如, http//en.cppreference.com/w/cpp/language/zero_initialization说:

As described in non-local initialization, static and thread-local variables that aren't constant-initialized (since C++14) are zero-initialized before any other initialization takes place. 如非本地初始化中所述,非常规初始化的静态和线程局部变量(因为C ++ 14)在进行任何其他初始化之前都是零初始化的。

There are probably other such explanations throughout the site. 整个网站可能还有其他类似的解释。 But even if your quoted part was the only place in which it is mentioned explicitly, why should that make the explanation incomplete? 但即使你引用的部分是明确提到它的唯一地方,为什么这会使解释不完整呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM