4

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/

3
  • int array[10] = { 0, 1, 2, 0, 0, 0, 0, 0, 0, 9 }; Commented Jan 28, 2012 at 21:41
  • @Mehrdad Afshari "Designated initializers". Commented Jan 28, 2012 at 21:42
  • For C++, the initializers are here. We still don't have the array initializer... and there are all sorts of limits on the structure initializer. Commented Dec 20, 2021 at 21:11

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

That's not initialization. -1
Yes it is. Why wouldn't it be one?
Assignment is not initialization.
I understand, but if there's no easy solution to OP's question...?
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;
})();

5 Comments

can you look at my proposal to very similar question: stackoverflow.com/a/12839880/1463922. Wouldn't be fun to make such initialization as part of std::array?
@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).
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<int,100> a { E[55]=7 };. Of course simple initialization still is valid: CArray<int,100> a { 0,0,...,7 };
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.
Thanks, when I find time I'll make a question on SO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.