Reputation: 343
I'm working on an embedded application in which I'd love to use a container like std::vector<>
. Unfortunately I must not use the heap. So std::vector<>
could not be used. So I'm looking for an alternative.
I've seen boost static_vector
but the boost approach seems too heavy for the microcontroller as far as I've seen. Or are there any experiences using boost on a small microcontroller (for example only the static_vector?)
One assumption could be made: the maximum number of entries during the whole application runtime is known at compile time.
So I'm wondering if there is any open source solution for this or if I have to implement a container by myself which is based on the std::array<>
implementation and adds some logic to enable the following operations:
Add (push_back()
) and remove (erase()
) elements during the runtime. Providing the typical container iterators and a random access. Also the short hand for ( : )
loop should be available.
So my naive approach would be:
Providing the iterators, and the random access seems easy to me, and should be mostly based on the std::array<>
functions
Adding the add (push_back
) and remove (erase
) should be no problem with some logic.
But how is the for ( : )
loop support to implement?
Are there any other things I need to consider?
Upvotes: 7
Views: 7449
Reputation: 7220
You can create a std::vector
with a custom std::allocator
that for instance returns pointers from a static buffer.
Rereading your question, you say that the total number of entries inside the container, is known at compile time, why not use an std::array
then or even a good old fashioned array?
As for the range based for loop (for ( : )
) it simply requires the begin
and end
member or free functions to be defined for the type, so if you do create your own type, you need to define these.
Upvotes: 8