Is it more performant ?
It should be exactly the same. By definition, it's a simple aggregate containing an array as its only member.
The situation seems to be more complicated, as std::array
does not always produce identical assembly code compared to C-array depending on the specific platform.
I tested this specific situation on godbolt:
#include <array>
void test(double* const C, const double* const A,
const double* const B, const size_t size) {
for (size_t i = 0; i < size; i++) {
//double arr[2] = {0.e0};//
std::array<double, 2> arr = {0.e0};//different to double arr[2] for some compiler
for (size_t j = 0; j < size; j++) {
arr[0] += A[i] * B[j];
arr[1] += A[j] * B[i];
}
C[i] += arr[0];
C[i] += arr[1];
}
}
GCC and Clang produce identical assembly code for both the C-array version and the std::array
version.
MSVC and ICPC, however, produce different assembly code for each array version. (I tested ICPC19 with -Ofast
and -Os
; MSVC -Ox
and -Os
)
I have no idea, why this is the case (I would indeed expect exactly identical behavior of std::array and c-array). Maybe there are different optimization strategies employed.
As a little extra:
There seems to be a bug in ICPC with
#pragma simd
for vectorization when using the c-array in some situations
(the c-array code produces a wrong output; the std::array
version works fine).
Unfortunately, I do not have a minimal working example for that yet, since I discovered that problem while optimizing a quite complicated piece of code.
I will file a bug-report to intel when I am sure that I did not just misunderstood something about C-array/std::array
and #pragma simd
.
for (auto i = ++std::begin(myArray); . . .
may not even compile (it seems that temporaries of fundamental type are not mutable, at least not with clang 6)struct Direction { int32_t dw; int32_t dh; };
andstatic const Direction DIRECTIONS[DIRECTIONS_COUNT] { { -1, 1}, {0,1}, {1,1} , { 1, 0 }, {1,-1}, {0,-1} , {-1,-1}, {-1,0} };
compiles. But if you change to astd::array<Direction,DIRECTIONS_COUNT>
with same initializer list, suddenly you get "too many initializers" error. (VS 2019 Community with language = C++17)std::array
is just a wrapper for C-style arrays. If I'm not mistaking, in some earlier versions of C++ you couldn't initialize it with single brackets. However, don't take my word for it, since I haven't used them much