title: C++ sub-object initialization
body: <p>I'm porting some C code to C++ and I can't resolve a particular issue dealing with sub-object inititialization.</p>
  
  <p>The following code is an example of what I mean :
  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define MY_INDEX        1
  #define MY_OTHER_INDEX  3
  
  /* Structure declaration */
  struct my_struct
  {
      int a;
      int b;
      int c;
  };
  
  /* Array declaration and initialization */
  struct my_struct my_array[] =
      {
          [0]                 = (struct my_struct) {0, },
          [MY_INDEX]          = ((struct my_struct) {
                                  .a = 10,
                                  .b = 20,
                                  .c = 30
                                  }),
          [MY_OTHER_INDEX]    = ((struct my_struct) {
                                  .a = 42,
                                  .b = 42,
                                  .c = 42
                                  })
      };
  
  /** Test code */
  int
  main(void)
  {
          unsigned int i;
  
          for (i = 0; i &lt; sizeof(my_array)/sizeof(struct my_struct); i++)
                  printf("Index %u: a=%d, b=%d, c=%d\n",
                          i, my_array[i].a, my_array[i].b, my_array[i].c);
  
          return 0;
  }
  </code></pre>
  
  <p>It can be compiled without warnings nor errors with <code>gcc -ansi -Wall</code>, although adding the <code>-pedantic</code> flag will throw some warnings stating that <em>ISO C90 forbids specifying subobject to initialize</em>.</p>
  
  <p>The initialization of <code>my_array</code> will act like if :</p>
  
  <ul>
  <li>Index 0 will have a,b,c = 0</li>
  <li>Index 1 (<code>MY_INDEX</code>) will have a=10, b=20, c=30</li>
  <li>Index 2 will also have a,b,c = 0</li>
  <li>Index 3 (<code>MY_OTHER_INDEX</code>) will have a,b,c = 42</li>
  </ul>
  
  <p>I really like this form of initialization, I find it concise and readable.</p>
  
  <p>Using this syntax with C++ will result in GCC thinking I'm declaring a lambda function because of the <code>[]</code>, and even without the index GCC tells me it <em>expected primary-expression before 'struct'</em>.</p>
  
  <p>What would be an equivalent in C++ (even with the C++11 standard) ? The <strong>key point</strong> is to be able to specify the structure fields name in the initializer for readability (the real structure have dozen of integer fields and bitfields), being able to also see the index in the initializer would be a plus too.</p>