The code in this post is bad. Its purpose is only to show what I want to say.
An array has fixed size and compile-time allocation and it’s preferred in some real-time situations. If you want a variable number of elements, you could choose a vector. A vector has runtime allocation on the heap, which cannot always offer the predictability and performance required by some applications.
But what if you need both? Compile-time allocation and a different number of elements each time a function is called with some data from a source. You can use an array and a variable for the number of elements:
// Declare the maximum number of elements and the array constexpr std::size_t max_size = 10; std::array<int, max_size> elements{4, 5}; // Each call, you will get the elements array and // the number of elements sent std::size_t number_of_elements = 2; // You should make sure the number of elements is not greater // than the maximum if (number_of_elements > max_size) { throw std::out_of_range{"too many elements"}; } auto work = [](const std::array<int, max_size>& elements, const std::size_t number_of_elements) { // Then iterate through all the elements passed this time for (auto it = elements.begin(); it != elements.begin() + number_of_elements; ++it) { // ... } }; work(elements, number_of_elements);
Some problems with this approach:
-
- all functions that need this array must include the number of elements in their signature
- the number of elements could be altered by mistake
- each time you iterate the array you must specify which is its end
It would great to have a container similar to an array/vector that would prevent the issues above. Maybe there is already an implementation for this but I didn’t find it. So I wrote a few lines which partially implement this container. The idea is to have perfect integration with all STD algorithms, to be able to use it like it was an array.
My container is far from the idea of perfect, it just shows some features. I’m actually wrapping an STD array and using two iterators: one for writing data into the array and one for reading. The usage is pretty simple. Continue reading Compile-time array with runtime variable size