[boost][array] Multi-dimensional initialization

At least under MSVC 8 I can do: boost::array< boost::array<int,2>, 4 > foo = { -1,1, 0,0, 1,-1, 2,-2 }; or int foo[4][2] = { {-1,1}, {0,0}, {1,-1}, {2,-2} }; or boost::array<int,2> foo[4] = { {-1,1}, {0,0}, {1,-1}, {2,-2} }; but not boost::array< boost::array<int,2>, 4 > foo = { {-1,1}, {0,0}, {1,-1}, {2,-2} }; just wondering why this is the case.

Michael Marcin wrote:
At least under MSVC 8 I can do:
boost::array< boost::array<int,2>, 4 > foo = { -1,1, 0,0, 1,-1, 2,-2 }; or
int foo[4][2] = { {-1,1}, {0,0}, {1,-1}, {2,-2} };
or
boost::array<int,2> foo[4] = { {-1,1}, {0,0}, {1,-1}, {2,-2} };
but not
boost::array< boost::array<int,2>, 4 > foo = { {-1,1}, {0,0}, {1,-1}, {2,-2} };
just wondering why this is the case.
To initialize a boost::array "by the book" you need double braces: boost::array<int,2> foo = {{ 1, 2 }}; because a boost::array<int, 2> is essentially struct array { int data[2]; }; and, according to the initializer rules, the outer braces are for the array struct, and the inner braces correspond to the data[] array. In most cases you can drop the outer braces. In your last example, however, dropping them creates an ambiguity, and you need: boost::array< boost::array<int,2>, 4 > foo = {{ {-1,1}, {0,0}, {1,-1}, {2,-2} }};

Peter Dimov wrote:
Michael Marcin wrote:
<snip>
To initialize a boost::array "by the book" you need double braces:
boost::array<int,2> foo = {{ 1, 2 }};
because a boost::array<int, 2> is essentially
struct array { int data[2]; };
and, according to the initializer rules, the outer braces are for the array struct, and the inner braces correspond to the data[] array.
In most cases you can drop the outer braces. In your last example, however, dropping them creates an ambiguity, and you need:
boost::array< boost::array<int,2>, 4 > foo = {{ {-1,1}, {0,0}, {1,-1}, {2,-2} }};
Ahah. It makes sense now, thank you.
participants (2)
-
Michael Marcin
-
Peter Dimov