Initialization Lists and std::array: A GCC Bug
The std::array class in the C Standard Library provides a fixed-size array container. It's commonly believed that this class supports initialization lists.
However, using GCC 4.6.1, attempts to initialize std::array instances using the following syntax fail:
std::array<:string> strings = { "a", "b" };
std::array<:string> strings({ "a", "b" });
Despite initialization lists working with std::vector, this behavior with std::array has raised questions about the C standard or a potential GCC issue.
std::array's Inner Workings
std::array is essentially a struct that encapsulates an array. Its structure resembles:
template
struct std::array {
T a[size];
};
Unlike std::vector, which has a constructor accepting initializer lists, std::array lacks such a constructor.
Aggregate Initialization
Although std::array doesn't have an explicit constructor for initialization lists, it's considered an aggregate type in C 11. As such, it can be aggregate initialized. However, to initialize the array within the struct, an additional set of curly braces is required:
std::array<:string> strings = {{ "a", "b" }};
Potential GCC Bug
The C standard allows the omission of the extra curly braces in such initialization. Therefore, it's likely that GCC's inability to handle this syntax without them is a bug.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3