Inline array initialization

33,710

Solution 1

This has never worked in the current version of C++, you have only been able to zero-initialize (or not initialize) dynamically allocated arrays.

What has always worked is non-dynamically allocated array initialization:

int myarray[] = {1, 2, 3, 4, 5};

Perhaps you are confusing it with this?

Even in C++0x it is not legal syntax to omit the explicit array size specifier in a new expression.

Solution 2

C++ have never allowed to initialize array with an unknown size of elements like above. The only 2 ways I know, is specify the number of elements or use pointers.

Share:
33,710
Gzorg
Author by

Gzorg

Updated on January 13, 2020

Comments

  • Gzorg
    Gzorg over 4 years

    I was used to using

    new int[] {1,2,3,4,5};
    

    for initializing array. But it seems nowadays, this does not work anymore, i have to explicitly state how many elements there are, with

    new int[5] {1,2,3,4,5};
    

    so compilers forgot how to count ?

    And to make this a closed question, is there a way to omit the number of elements ?