简体   繁体   中英

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? Or should I simply read the language docs in case I need 100% certainty?

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.

Is the cppreference just incomplete, or the information is there and I can't find it?

cppreference is not "official"; 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

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.

Or should I simply read the language docs in case I need 100% certainty?

The C++ ISO standard is the one and only official source, so only that can give you 100% certainty. The standard document is not free, but drafts are free and give you, say, 99.99% certainty.

Of course, "100% certainty" may be misleading anyway. 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++. 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).

So maybe the sentence above should be rephrased into: A C++ ISO standard is the one and only official source.

But this is the only place where actually somebody mentioned when default init takes place relative to other initializations.

That's not true. For example, http://en.cppreference.com/w/cpp/language/zero_initialization says:

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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