I'm actually not sure why your bool example works, maybe someone else can chime in. Your declaration is not an initialization. The std::array will perform default initialization, which crucially is not the same as value-initialization. For primitive types, each element is undefined, meaning it has garbage. It's different for std::string because that's a class.
To fix it, you can ask for value-initialization instead using std::array<int, 5>{};.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm actually not sure why your
bool
example works, maybe someone else can chime in. Your declaration is not an initialization. Thestd::array
will perform default initialization, which crucially is not the same as value-initialization. For primitive types, each element is undefined, meaning it has garbage. It's different forstd::string
because that's a class.To fix it, you can ask for value-initialization instead using
std::array<int, 5>{};
.