How often do you see the sizeof(array)/sizeof(array[0]) statement used to get the size of an array? I really hope it′s not too often, because it′s 2024 already. In this note, we′ll talk about the...
I think this was focused on maintaining code. Replacing C-style arrays with std::array can be a daunting task, depending on how the project is structured.
I don’t really see how it’s daunting enough to avoid mentioning. You can replace a C array on the stack by just swapping it with std::array. Yes, it can depend on the project structure, but that’s equivalently true for any STL container the author recommended.
I don’t really see how it’s daunting enough to avoid mentioning.
I think it’s a good call not to mention them because they are irrelevant given the topic. If your code base and/or the consumers of your code base are using C-style arrays for input and/or output, it’s hardly helpful to suggest changing all your interfaces to use another data type. It’s outright impossible if you’re dealing with extern C interfaces.
Depending on what OP is trying to do; swapping out the internal implementation with array and then decaying to the old terrible .data() ‘pointer that could be anything’ when having to cross interfaces and boundaries could be a winner. At least their own stuff gets the benefits of known size, bounds checking and being a real class, and the code improvements can be done a little at a time. I’d almost consider this the main use case for array - unless for some reason you just absolutely must allocate on the stack, then vector is better in every way.
Same argument for smart pointers. If some legacy code returns an owning pointer, then get it wrapped up as soon as possible. At least your own code won’t leak.
Why no mention of std::array?
I think this was focused on maintaining code. Replacing C-style arrays with std::array can be a daunting task, depending on how the project is structured.
I don’t really see how it’s daunting enough to avoid mentioning. You can replace a C array on the stack by just swapping it with std::array. Yes, it can depend on the project structure, but that’s equivalently true for any STL container the author recommended.
I think it’s a good call not to mention them because they are irrelevant given the topic. If your code base and/or the consumers of your code base are using C-style arrays for input and/or output, it’s hardly helpful to suggest changing all your interfaces to use another data type. It’s outright impossible if you’re dealing with extern C interfaces.
Depending on what OP is trying to do; swapping out the internal implementation with
array
and then decaying to the old terrible.data()
‘pointer that could be anything’ when having to cross interfaces and boundaries could be a winner. At least their own stuff gets the benefits of known size, bounds checking and being a real class, and the code improvements can be done a little at a time. I’d almost consider this the main use case forarray
- unless for some reason you just absolutely must allocate on the stack, thenvector
is better in every way.Same argument for smart pointers. If some legacy code returns an owning pointer, then get it wrapped up as soon as possible. At least your own code won’t leak.
deleted by creator