Initializing an Inline Array

I used to use

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

to initialize the array. But now it seems that this no longer works, I must explicitly indicate how many elements there are, with

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

therefore, compilers forgot how to count?

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

+8
c ++ arrays c ++ 11
source share
2 answers

This never worked in the current version of C ++, you could only initialize (or not initialize) dynamically allocated arrays.

What always worked is initialization without a dynamically allocated array:

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

Perhaps you are confusing this?

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

+16
source share

C ++ was never allowed to initialize an array with an unknown element size, as stated above. The only thing I know is to specify the number of elements or use pointers.

+2
source share

Source: https://habr.com/ru/post/650624/


All Articles