Home > Article > Backend Development > Why Does GCC 4.6.1 Struggle with Initialization Lists for std::array?
It is possible to create a std::array using initialization lists in several ways. However, GCC 4.6.1 may encounter errors when attempting this.
The syntax for creating a std::array using initialization lists is:
<code class="cpp">std::array<T, size> array = { { value1, value2, ..., valueN } };</code>
where T is the array's element type, size is the array's size, and value1 to valueN are the array's initial values.
std::array is an aggregate struct, which allows it to be aggregate-initialized. To aggregate-initialize the array inside the struct, use an additional set of curly braces:
<code class="cpp">std::array<std::string, 2> strings = {{ "a", "b" }};</code>
This syntax avoids the constructor that takes an initializer list, which may cause problems in GCC 4.6.1.
The C 11 standard suggests that the extra curly braces can be elided in aggregate initialization. Therefore, the inability of GCC 4.6.1 to compile initialization lists for std::array without the extra braces is likely a compiler bug.
The above is the detailed content of Why Does GCC 4.6.1 Struggle with Initialization Lists for std::array?. For more information, please follow other related articles on the PHP Chinese website!