Why is there no uniform initializer for array?

Viewed 73

Supposed I want to dynamically allocate heap with array:

auto my_arr = new int[3] {3, 4, 5};

for (int i = 0; i < 3; i++)
    std::cout << my_arr[i] << ' ';

delete[] my_arr;

Output:

3 4 5

When I use without new like:

auto my_arr = int[3]{3, 4, 5};

It throws an error.

Alternatives:

auto my_arr = std::array{3, 4, 5}; // deduces into `std::array<int, 3>`
auto my_arr2 = {3, 4, 5};          // deduces into `std::initializer_list<int>`

What is the reason why the built-in array has no initialization (constructor-like) when the new expression is permitted?

0 Answers
Related