简体   繁体   English

C数组和结构中的C ++统一初始化

[英]C++ uniform initialization in C arrays and structs

I've seen some old code that looks like this: char foo[100] = { 0 }; 我看过一些看起来像这样的旧代码: char foo[100] = { 0 }; . Something similar happens with structs, like so: STRUCT st = { 0 }; 结构STRUCT st = { 0 };发生类似的情况,例如: STRUCT st = { 0 }; . The intention in both cases is clear, namely to zero-initialize the respective variables. 在两种情况下,其意图都是明确的,即对各个变量进行零初始化。 As such, char foo[100] {}; 因此, char foo[100] {}; and STRUCT st {}; STRUCT st {}; would have been more idiomatic. 会更加惯用。

My question is: should the code with <variable> = { 0 } be expected to achieve the same outcome? 我的问题是:应该期望具有<variable> = { 0 }的代码实现相同的结果吗? I've tested this with release-build binaries and the elements appear to be zero-initialized, but is this guaranteed by the standard? 我已经使用release-build二进制文件对此进行了测试,并且元素似乎是零初始化的,但是标准可以保证吗? It seems to me that <variable> = { 0 } should only guarantee for the first element of the variable (array element or struct member) to be zero. 在我看来, <variable> = { 0 }仅应保证变量的第一个元素(数组元素或struct成员)为零。

Also, what behavior is indicated by the similar declarations char foo[100] = {}; 另外,类似的声明char foo[100] = {};指示了什么行为char foo[100] = {}; and STRUCT st = {} ? STRUCT st = {}

(Motivation behind this question is that I'd change all the declarations to the idiomatic form, but if there is no guarantee of zero-initialization then the issue is something more serious and worth opening a ticket over.) (此问题的动机是我将所有声明都更改为惯用格式,但如果不能保证零初始化,那么问题就更严重了,值得重新审视。)

It seems to me that <variable> = { 0 } should only guarantee for the first element of the variable (array element or struct member) to be zero. 在我看来, <variable> = { 0 }仅应保证变量的第一个元素(数组元素或struct成员)为零。

That syntax, for POD types, is the same as <variable> = {}; 对于POD类型,该语法与<variable> = {};

Anything that is not explicitly specified is initialized to zero. 任何未明确指定的内容都将初始化为零。 Use of <variable> = { somve_value } makes a difference only when some_value is other than zero. 仅当some_value不为零时,使用<variable> = { somve_value }some_value

Even though the same syntax can be used for non-POD types, the elements that are of non-POD type that are not explicitly initialized are initialized using their default constructors. 即使非POD类型可以使用相同的语法,也可以使用其默认构造函数来初始化未明确初始化的非POD类型的元素。

From 8.5.1 Aggregates/7 : 8.5.1汇总/ 7开始

If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from an empty initializer list ( [dcl.init.list] ). 如果列表中的初始化子句少于聚合中的成员,则每个未显式初始化的成员都应从一个空的初始化子列表( [dcl.init.list] )中初始化。 [ Example: [ 示例:

 struct S { int a; const char* b; int c; }; S ss = { 1, "asdf" }; 

initializes ss.a with 1 , ss.b with "asdf" , and ss.c with the value of an expression of the form int() , that is, 0 . 初始化ss.a1ss.b"asdf" ,和ss.c与以下形式的表达式的值int()即, 0 end example ] 结束示例 ]

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

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