mercredi 25 janvier 2023

Why does std::array require the size as a template parameter and not as a constructor parameter?

There are many design issues I have found with this, particularly with passing std::array<> to functions. Basically, when you initialize std::array, it takes in two template parameters, <class T and size_t size>. However, when you create a function that requires and std::array, we do not know the size, so we need to create template parameters for the functions also.

template <size_t params_size> auto func(std::array<int, params_size> arr);

Why couldn't std::array take in the size at the constructor instead? (i.e.):

auto array = std::array<int>(10);

Then the functions would look less aggressive and would not require template params, as such:

auto func (std::array<int> arr);

I just want to know the design choice for std::array, and why it was designed this way.

This isn't a question due to a bug, but rather a question why std::array<> was designed in such a manner.

Aucun commentaire:

Enregistrer un commentaire