How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

半腔热情 提交于 2020-01-03 10:47:11

问题


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.


回答1:


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 value-initialization or static-initialization will apply, and I can't see much value in using up your tag quota to apply zero-initialization as well.



来源:https://stackoverflow.com/questions/37732000/how-do-zero-initialization-static-initialization-and-value-initialization-diff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!