Home >Backend Development >C++ >Can std::array be Default-Initialized?

Can std::array be Default-Initialized?

DDD
DDDOriginal
2024-10-31 03:29:02716browse

Can std::array be Default-Initialized?

Can std::array be Default-Initialized?

By declaring a variable as std::array x;, you can guarantee that all its elements will be default-initialized. According to the C 11 language standard (§8.5/11), any object without an explicit initializer is default-initialized.

However, you should be aware that default initialization has no effect on non-class, non-array types (§8.5/6), leaving their values indeterminate. For example, a default-initialized array of such types will have indeterminate values.

Value-Initialization of Arrays

If you wish to initialize all array elements to a specific value T{}, this is not default-initialization but value-initialization (8.5/7). You can value-initialize arrays easily in C 11 by using empty initializers:

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

This will value-initialize all array elements, resulting in a consistent value of 0 for integers and the default values for their respective data types.

The above is the detailed content of Can std::array be Default-Initialized?. 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