1

I have a structure that more or less follows this pattern:

struct sTruct {
   int count;
   struct {
       int A;
       int B;
       int C;
   } array[];   //count is the size of this array
};

I would like to be able to initialize these with something like the following syntax:

sTruct gInit1 = { 2, { {1,2,3},{4,5,6} }};

Really, that initialization syntax (or rather, the compactness of it) is more important than the specific struct layout. I do not have access to the standard containers (embedded platform), but I might be able to replicate some of their behavior if needed.

In final form, I would like to initialize an array of roughly 300 of these sTruct containers at once, just to add one more level of parenthesis.

jkerian
  • 16,497
  • 3
  • 46
  • 59

1 Answers1

7

You can't do it. If you gave the array a size you could. An alternative might be:

template < int size >
struct sTruct
{
  struct { int a, int b, int c } array[size];
};
sTruct<2> gInit1 = {{1,2,3},{4,5,6}};

But, of course, all your sTructs are different types and so it may not be what you want. Your only other alternative is going to have to be free-store based and won't give you that syntax until initialization lists in 0x.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • I suppose stuffing different sized sTructs into the same array doesn't make much sense in the first place. I may use something like this template, although I still need to add the "size" variable itself to the outer struct. – jkerian Dec 17 '10 at 17:40
  • If you like this answer you may as well use boost::array. It's more or less exactly my answer but can hold anything and acts like a standard container (except it has a static size and can be initialized with aggregate syntax). – Edward Strange Dec 17 '10 at 17:46