There is no doubt std::array makes things easier than c array, but you are programming Arduino and not Windows, it is expected that you know how to handle c arrays
The point is that there are all kinds of inconsistencies when dealing with C-style arrays.
Inconsistencies that you are used to, maybe, but nonetheless inconsistencies that add nothing to the language and that can easily be avoided by just using std::array.
What modern language has arrays that implicitly decay to pointers, that cannot be initialized by other arrays, or that cannot be returned by functions?
I'm not talking about free variables:
How would you implement this in a general way using C-style arrays?
std::array is a standard-layout type, you can use it for that purpose as well.
I can understand that, especially in the scenarios where you work close to the hardware. But in this case, the problem is initialization of a member variable, which cannot be done directly using C-style arrays, so using std::array is the obvious solution, and is consistent with the initialization of other types of member variables.
understanding
Luckily, understanding std::array is very easy, there's no magic involved. From cppreference:
This container is an aggregate type with the same semantics as a struct holding a C-style array
T[N]as its only non-static data member.
In other words, std::array<int, 5> is just struct { int _data[5]; };.
If they're useful in high-level applications, why wouldn't you use them in low-level applications?
What are in your opinion the advantages of C-style arrays over std::array?
If std::array makes things easier, why not use it if you can?
Just because you understand C-style arrays that doesn't mean you have to use them everywhere, especially if better alternatives exist.
Some standard library containers like std::vector (which uses dynamic allocation) could be better on Windows while not being a great solution on Arduino, but std::array is equivalent to a C-style array in terms of layout, memory usage, performance, and so on. If it's better on Windows, it's better on Arduino as well.
Itâs a wrapper over c array and it is not available on Arduino. Besides, just because one can doesnât mean one should.
That's why the âif you canâ is there, it is available on all Arduinos except AVR.
And even if you're stuck on AVR, you can implement it yourself in under 10 lines of code.
In this case, you use it because it solves the problem of initializing an array member variable, which is not possible using C-style arrays.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.