6

Ben Voigt has pointed out here that:

Zero initialization is one of the steps of static initialization. But you're right that you can't blindly substitute the latter (tag), since zero initialization is also performed for value initialization. However, there's no need for (a tag named) zero-initialization in the context of C++, because tags already exist for both static initialization and value initialization, and those are more relevant.

I thought there was a case where it made sense to "Zero-Initialize" rather than "Static-Initializing" or "Value-Initializing" or is "Zero-Initialization" never going to happen in the wild, and I should use more specific terms like: "Static-Initialization" or "Value-Initialization"?

To be fair most of my experience on these topics comes from studying the answers to this question, so I'm sure Ben Voigt is right, I'd just like someone to spell out why.

Community
  • 1
  • 1
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
  • It seems that this question is more for meta.SO than SO as it is about tags... – Jarod42 Jun 09 '16 at 17:05
  • @Jarod42 Well the question in my mind is one of concepts. I think I'm missing what Ben Voigt is saying because I don't understand the difference between these initializations. I'm not sure how we can get into that technicality on the meta. – Jonathan Mee Jun 09 '16 at 17:12
  • @Jarod I don't think so. It is a meta question, as Jonathan is asking not about C++ but about terminology used by the Standard and experts to talk about C++. But SO is still the right site, meta.SO is only for the subset of meta questions which are about the Stack Overflow software and policies and this isn't. – Ben Voigt Jun 09 '16 at 17:16
  • 1
    You could argue that Programmers is a better home... But not meta.SO – Ben Voigt Jun 09 '16 at 17:18
  • Zero-initialization is a *technical* part of some other *initialization* really *"asked"* by user. The "complexity" is for those initializations. In addition zero-initialization doesn't group those initialization either. – Jarod42 Jun 09 '16 at 17:24
  • @BenVoigt It seems there may be consensus that this should be on http://programmers.stackexchange.com? I still feel it's a http://stackoverflow.com question. But I have opened a dupe over there: http://programmers.stackexchange.com/q/321766/98845 – Jonathan Mee Jun 09 '16 at 17:35
  • I feel like as written, this is a very very poor fit for Programmers. But you could write a question asking about (1) the terminology and (2) whether grouping the three cases of "zero initialization" is conceptually useful, or they're used in such different contexts from each other than common treatment is fruitless. – Ben Voigt Jun 09 '16 at 17:40
  • Please do not [cross-post](http://meta.stackexchange.com/tags/cross-posting/info): http://programmers.stackexchange.com/q/321766/ –  Jun 09 '16 at 18:03
  • What is the actual question? – Barry Jun 09 '16 at 18:26
  • @Barry I know that :"Static-Initialization" and "Value-Initialization" utilize "Zero-Initialization". Is "Zero-Initialization"something that occurs in the wild, or does it only occur as a step in other initializations? – Jonathan Mee Jun 09 '16 at 18:31

1 Answers1

4

Zero-initialization can occur on its own; when a character array is initialized using a string literal that is shorter than the array, the remaining characters are zero-initialized. But in all other cases, zero-initialization occurs during value-initialization, or as the static-initialization step of initializing an object with static or thread-local storage duration (this can occur on its own, or preparatory to dynamic initialization).

So unless you're asking about the zero representations for character types (and I can't see there being many questions in that topic) one of the other tags or will apply, and I can't see much value in using up your tag quota to apply as well.

ecatmur
  • 137,771
  • 23
  • 263
  • 343
  • Zero-Initialization is just, default-initialization then? – Jonathan Mee Jun 09 '16 at 17:37
  • I don't know why we even still have that rule, since the more general " If there are fewer initializer-clauses in the list than there are elements in the aggregate, then each element not explicitly initialized shall be initialized from its default member initializer (9.2) or, if there is no default member initializer, from an empty initializer list (8.5.4)." already guarantees the same behavior, as initialization of character types from an empty initializer list is value initialization is zero initialization. – Ben Voigt Jun 09 '16 at 17:37
  • 1
    @JonathanMee: No, it's not default initialization, which can call a user constructor. There are really two different categories. The first is what happens when an object is created from `{}` and the type's default constructor is trivial a/k/a value-initialization (formally this doesn't happen for character arrays, but I know of no actual difference between what happens there and for all other aggregates with more elements than initializers). This case can't ever arise if the type does have a user-defined default constructor. – Ben Voigt Jun 09 '16 at 17:43
  • @JonathanMee certainly not; default-initialization does nothing to primitives. The connection between zero-initialization and default-initialization is that when an object with static or thread-local storage duration (so not an automatic object) is declared without an initializer, zero-initialization is performed *before* default-initialization (so a primitive declared with static storage duration and without an initializer will be observed to have a zero value). – ecatmur Jun 09 '16 at 17:46
  • 1
    @JonathanMee: The other case is a partial mitigation of the "static initialization order fiasco". For automatic storage, the value of an object before its initialization has started is an indeterminate value, and inspecting it leads to undefined behavior (slowly for character types, immediately for all others). But objects of static storage duration can sometimes be inspected prior to initialization, the value is zero not indeterminate, and this applies even if the object type does have a default constructor, because dynamic construction hasn't yet occurred. – Ben Voigt Jun 09 '16 at 17:46
  • @ecatmur: There's definitely no "without an initializer" condition on that. Any object with static storage duration and dynamic initialization can, due to the SIOF, potentially be inspected before initialization starts. Well, so can automatic objects, consider `T t = f(&t);` .. the call to `f` takes place before initialization of `t` and is given an alias to `t`... but for automatic objects the zero-before-initialization guarantee doesn't apply. – Ben Voigt Jun 09 '16 at 17:47
  • 1
    "preparatory to static initialization" - you mean preparatory to *dynamic initialization*. "static initialization" doesn't mean "initialization of an object of static storage duration"; it means something else. – T.C. Jun 09 '16 at 21:31