3

The following code works with GCC's C compiler, but not with the C++ compiler. Is there a "shortcut" to achieve the same result in C++?

int array[10] = {
    [1] = 1,
    [2] = 2,
    [9] = 9
};

EDIT: Humm, I found this, clarifies everything. http://eli.thegreenplace.net/2011/02/15/array-initialization-with-enum-indices-in-c-but-not-c/

bugflux
  • 123
  • 1
  • 6

2 Answers2

5

This form of initialization is only defined in the C99 standard. It does not apply to C++. So, you'll have to assign your elements one-by-one:

int array[10] = { 0 };
array[1] = 1;
array[2] = 2;
array[9] = 9;
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
4

While gcc may support some sort of extension to C++, it is generally advisable to avoid compiler- and platform-specific extensions wherever possible.

Use the standard C++ syntax for array initialization:

int array[10] = { 0, 1, 2, 0, 0, 0, 0, 0, 0, 9 };

Or write a function to do the initialization of specific elements:

std::array<int, 10> create_initialized_array()
{
    std::array<int, 10> values = { 0 };
    values[1] = 1;
    values[2] = 2;
    values[9] = 9;
    return values;
}

std::array<int, 10> array = create_initialized_array();

Or use a lambda expression:

std::array<int, 10> array = ([]() -> std::array<int, 10>
{
    std::array<int, 10> values = { 0 };
    values[1] = 1;
    values[2] = 2;
    values[9] = 9;
    return values;
})();
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • can you look at my proposal to very similar question: http://stackoverflow.com/a/12839880/1463922. Wouldn't be fun to make such initialization as part of std::array? – PiotrNycz Oct 11 '12 at 12:50
  • @PiotrNycz: It's an interesting idea. However, one could not add such constructors to `std::array`. `std::array` is specified as being an _aggregate_, which means that it cannot have any user-declared constructors. The rationale for this was to allow aggregate initialization (the `T x[N] = { /* initializers */ };` syntax). It _might_ be possible to remove the aggregate requirement and add support for `initializer_list`, but that would break some currently-valid C++11 (and TR1) code (e.g. code that relies on brace elision, which I do not believe is valid for initializer lists). – James McNellis Oct 11 '12 at 16:27
  • I did not mean to add "user-declared-constructor" whatever it means. I just meant to add constructor allowing, like in C, initialize only selected elements, that's all. C: `int [100]={[55]=7};`, with my array: `CArray a { E[55]=7 };`. Of course simple initialization still is valid: `CArray a { 0,0,...,7 };` – PiotrNycz Oct 11 '12 at 16:35
  • 1
    Right, but adding a constructor that accepts an initializer list counts as _adding a constructor_. `std::array` cannot have any user-declared constructors because it is an aggregate. Adding support for designated initializers would be interesting to consider, though. Generalizing designated initializers to work for arbitrary types could prove tricky, but not impossible. I am entirely unfamiliar with the new C++11 initializer lists feature, so I'm probably not the best person to ask about this. – James McNellis Oct 11 '12 at 16:38
  • Thanks, when I find time I'll make a question on SO. – PiotrNycz Oct 11 '12 at 16:44