Home  >  Article  >  Backend Development  >  How is Default Initialization Handled for `std::array` in C 11?

How is Default Initialization Handled for `std::array` in C 11?

Susan Sarandon
Susan SarandonOriginal
2024-10-30 22:14:30245browse

How is Default Initialization Handled for  `std::array` in C  11?

Default Initialization of C 11's std::array

Default initialization, performed when no explicit initializer is specified, is guaranteed by the C language standard for all objects without an explicit initializer, including instances of std::array and T[N] (§8.5/11).

However, it's important to note that default initialization has no effect on non-class, non-array types, leaving their value indeterminate (§8.5/6). For example, a default-initialized array of such types will have indeterminate values:

<code class="cpp">int plain_int;
int c_style_array[13];
std::array<int, 13> cxx_style_array;</code>

To explicitly initialize all elements to T{}, use value-initialization (8.5/7):

<code class="cpp">int plain_int{};
int c_style_array[13]{};
std::array<int, 13> cxx_style_array{};</code>

The above is the detailed content of How is Default Initialization Handled for `std::array` in C 11?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn