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

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

Susan Sarandon
Release: 2024-10-30 22:14:30
Original
363 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template