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

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

Patricia Arquette
Patricia ArquetteOriginal
2024-11-01 06:13:31334browse

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{};

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!

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