Home > Backend Development > C++ > How Does Default Initialization Work with `std::array` in C 11?

How Does Default Initialization Work with `std::array` in C 11?

Patricia Arquette
Release: 2024-11-01 06:13:31
Original
395 people have browsed it

How Does Default Initialization Work with `std::array` in C  11?

Default Initialization in std::array

Default initialization is the initialization that occurs when no explicit initializer is provided. With C 11 std::array, the syntax std::array x; guarantees that all elements of the array will be default-initialized.

According to the C 11 standard (§8.5/11), any object without an explicit initializer is default initialized. This includes std::array objects and traditional C-style arrays. Notably, default initialization has no effect on non-class, non-array types, leaving their value indeterminate.

Value Initialization on All Arrays

While default initialization leaves non-class, non-array types indeterminate, value initialization sets elements to their default values. In C 11, value initialization is achieved by providing an empty initializer for each array element:

int plain_int{};
int c_style_array[13]{};
std::array<int, 13> cxx_style_array{};
Copy after login

This will value-initialize all elements of the arrays, resulting in plain_int and all array elements being initialized to zero.

The above is the detailed content of How Does Default Initialization Work with `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