Now that we have std::array what uses are left for C-style arrays?
std::array
is vastly superior to the C arrays. And even if I want to interoperate with legacy code, I can just use std::array::data()
. Is there any reason I would ever want an old-school array?
Unless I've missed something (I've not followed the most recent changes in the standard too closely), most of the uses of C style arrays still remain. std::array
does allow static initialization, but it still won't count the initializers for you. And since the only real use of C style arrays before std::array
was for statically initialized tables
along the lines of:
MyStruct const table[] =
{
{ something1, otherthing1 },
// ...
};
using the usual begin
and end
template functions (adopted in
C++11) to iterate over them. Without ever mentionning the size, which the compiler determines from the number of initializers.
EDIT: Another thing I forgot: string literals are still C style arrays; i.e. with type char[]
. I don't think that anyone would exclude using string literals just because we have std::array
.
No. To, uh, put it bluntly. And in 30 characters.
Of course, you need C arrays to implement std::array
, but that's not really a reason that a user would ever want C arrays. In addition, no, std::array
is not less performant than a C array, and has an option for a bounds-checked access. And finally, it is completely reasonable for any C++ program to depend on the Standard library- that's kind of the point of it being Standard- and if you don't have access to a Standard library, then your compiler is non-conformant and the question is tagged "C++", not "C++ and those not-C++ things that miss out half the specification because they felt it inappropriate.".
Seems like using multi-dimensional arrays is easier with C arrays than std::array
. For instance,
char c_arr[5][6][7];
as opposed to
std::array<std::array<std::array<char, 7>, 6>, 5> cpp_arr;
Also due to the automatic decay property of C arrays, c_arr[i]
in the above example will decay to a pointer and you just need to pass the remaining dimensions as two more parameters. My point is it c_arr
is not expensive to copy. However, cpp_arr[i]
will be very costly to copy.