If I want to build a very simple array like
int myArray[3] = {1,2,3};
Should I use std::array
instead ?
std::array<int, 3> a = {{1, 2, 3}};
What are the advantages of using std::array over usual ones? Is it more performant ? Just easier to handle for copy/access ?
What are the advantages of using
std::array
over usual ones?
It has friendly value semantics, so that it can be passed to or returned from functions by value. Its interface makes it more convenient to find the size, and use with STL-style iterator-based algorithms.
Is it more performant ?
It should be exactly the same. By definition, it's a simple aggregate containing an array as its only member.
Just easier to handle for copy/access ?
Yes.
A std::array
is a very thin wrapper around a C-style array, basically defined as
template<typename T, size_t N>
class array
{
public:
T _data[N];
T& operator[](size_t);
const T& operator[](size_t) const;
// other member functions and typedefs
};
It is an aggregate, and it allows you to use it almost like a fundamental type (i.e. you can pass-by-value, assign etc, whereas a standard C array cannot be assigned or copied directly to another array). You should take a look at some standard implementation (jump to definition from your favourite IDE or directly open <array>
), it is a piece of the C++ standard library that is quite easy to read and understand.
std::array
is designed as zero-overhead wrapper for C arrays that gives it the "normal" value like semantics of the other C++ containers.
You should not notice any difference in runtime performance while you still get to enjoy the extra features.
Using std::array
instead of int[]
style arrays is a good idea if you have C++11 or boost at hand.
std::array
has value semantics while raw arrays do not. This means you can copy std::array
and treat it like a primitive value. You can receive them by value or reference as function arguments and you can return them by value.
If you never copy a std::array
, then there is no performance difference than a raw array. If you do need to make copies then std::array
will do the right thing and should still give equal performance.
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.org:
#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 working minimal 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 missunderstood something about c_array/std::array and #pragma simd.
来源:https://stackoverflow.com/questions/30263303/stdarray-vs-array-performance