Unfortunately, std::array
does not have an initializer list constructor. Indeed, it has no user-defined constructor whatsoever -- this "feature" is a leftover from C++03 where omitting all user-defined constructors was the only way to enable the C-style brace initialization. It is IMHO a defect in the current standard.
So why doesn't built-in brace initialization work in this case? Let's see what std::array
looks like under the hood:
template <typename T, int i> struct array {
T data[i];
// ...
}
Ok, so doesn't that mean we'd have to use double braces in the initializer (one pair for array
, another pair for the data
member?
std::array<int, 2> a = { {1, 2} };
C (and consequently C++) has a special rule about brace elision, permitting the omission of the inner braces unless there is an ambiguity. array
exploits this feature, allowing us to write
std::array<int, 2> a = { 1, 2 };
So why doesn't the example in the original post work? Because brace elision is only permitted in the context of a C-style aggregate initialization, not if there's anything more complicated involved, such as an user-defined initializer list constructor.
The following should work, though, as ugly as it is:
std::vector<std::array<int, 2>> vp = { {{1,2}}, {{3,4}} };
The fact that it does not, at least on gcc 4.5 and gcc 4.6, seems to me to indicate a compiler bug. I'm not completely sure about it, though.
This question is somewhat relevant: How do I initialize a member array with an initializer_list?