I came across this code on reddit. I would have thought that type conversions would have caused this to be invalid.
int a[3] = { { {1, 2}, {3, 4}, 5, 6 }, {7, 8}, {9}, 10 };
On clang, I get a few warnings about excessive elements and braces in a scalar initializer. But the contents of a is [1, 7, 9].
Is this actually legitimate, and if it is, could someone explain what exactly is going on?
The excess elements are just ignored. There are two parts of 6.7.8 Initialization that you care about. First, from paragraph 17:
That one explains why you get 1, 7, and 9 – the current object gets set by those braces. Then as to why it doesn’t care about the extras, from paragraph 20: