I want to pass an array which can have different sizes to a function. I am on an embedded system with limited capacities. I thought about the following ways of implementing it:
- I don't want to use templates, because the function is called with multiple different array sizes. Every call would create a new set of this function, which would unnecessarily blow up my project.
- Using a simple pointer + length variable seems to be inelegant and unsafe. Also I couldn't nicely iterate over such a construct.
- Chained lists seems to be well overcomplicated for this problem.
- I don't want to use vectors, because they can not be stored statically. So maybe there could be a time, when not enough heap is available for my vector. The firmware needs to be as reliable as possible.
Is there a way of accomplishing this? Are my thoughts about the systems reliability too cautious?
Edit
- Span would be a nice possibility in my opinion. But, In my case I don't have C++20 support.
- Wrapper function for the template as @Some programmer dude suggested would also be an option! An advantage against a "normal" template function would be, that the function itself can still be in the source and doesn't need to be in the header file.
int a[3]{};int b[4]{};void func(int (&ref)[]){ref[0] = 4;}int main(){func(a);func(b);}is valid C++. See demostd::spanmaybe?