It depends on the use case. Performance and space-wise, they should be identical, except for the size member that std::array
carries. EDIT: Even the size will be optimized out. So it´s identical even space-wise. Implementations usually can do:
template <class T, std::size_t N>
class array {
...
public:
contsexpr std::size_t size() const { return N; }
}
And get away storing the size of the string.
std::array<T, N>
won't decay to a pointer when passing it around. But in your use case you have a global variable, I guess. I wouldn´t change what it's already working just for making it look nicer.
On the other hand, if you want to pass an std::array
of any size to a function, you will have to template it:
template <std::size_t N>
void f(std::array<MyT, N> const & arr);
Pros of std::array
- Safer, they don't decay to pointers.
- They carry the size.
Cons of std::array
- If you want a general function taking
std::array
,
you will have to do it in the header as a template.
In C arrays, you can do this:
void f(MyT const * arr, std::size_t size);
and it would work with any array, no matter the length.
This is less safe BUT more convenient to hide dependencies in .cpp
files, since later you can code the function inside a .cpp file.
But as I said, taking into account your use case, choose. I wouldn't
change code that is working and is not gonna be exposed (for example,
if it is a global that is never taken as a parameter in functions)
in any unsafe way.