零初始化,静态初始化和价值初始化有何不同? [英] How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

查看:167
本文介绍了零初始化,静态初始化和价值初始化有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ben Voigt 指出了

零初始化是静态初始化的步骤之一.但是您是对的,您不能盲目地替换后者(标签),因为对于值初始化也执行零初始化.但是,在C ++的上下文中,不需要进行零初始化(标记为零),因为静态初始化和值初始化都已经存在标记,并且它们更相关.

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"?

公平地说,我在这些主题上的大部分经验来自研究此问题的答案,因此我确定Ben Voigt是对的,我只想找一个人阐明原因.

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.

推荐答案

零初始化可以自己进行;当使用比数组短的字符串文字初始化字符数组时,其余字符将被零初始化.但是在所有其他情况下,零初始化发生在 值初始化期间,或者 as 以静态或线程局部存储持续时间初始化对象的静态初始化步骤(此操作可以单独发生,也可以为动态初始化做准备.

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.

这篇关于零初始化,静态初始化和价值初始化有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆