array

which is more readable?

int anArray[]={1,2};

or

int anArray[2]={1,2};
I prefer something like
1
2
std::size_t const size = 2;
int anArray[size] = {1, 2};
Though, I suppose something like this would be preference.
whats the use of size_t?
It's an unsigned integer. That is used a lot in the standard such as the size of a container. I suppose I could have done unsigned const size = 2; or even something like std::uint32_t const size = 2;

Though this will compile as well int const size = 2; I jsut prefer to make it unsigned if it is positive.
which is more readable?

They are different.
In the first the size of the array is implicitly taken from the initializer list. One has to count the members of the list.

The second, however decouples array size from the list size. This is legal:
1
2
std::size_t const size = 42;
int anArray[size] = {1, 2};

You still have to count the list in order to know how all elements of the array are initialized. The big bonus here is that the constant 'size' is available when the array is used.
okay thank you
Topic archived. No new replies allowed.