AFAIK, an array cannot be copied nor assigned so:
int a[5] = {1, 2};// 1 2 0 0 0
int b = a;// error
b = a; // error
But how does the compiler copies arrays that are member data of a class/struct type through the trivial copy-ctor and copy-assignment operator?
// trivial struct
struc Arr{
int arr_[7];
};
Arr a = {5, 7, 2, 10, 9, 1, 2};
Arr b = a;// trivial cpy-ctor
a = b; // trivial cpy-assignment operator
this->arr_ = rhs.arr_
? Or it iterates over all elements of rhs.arr_
and cpy-assign them to their coresponding elements in rhs
?So does the compiler does something like:
this->arr_ = rhs.arr_
?
No. As you said, this wouldn't compile.
Or it iterates over all elements of
rhs.arr_
and copy-assign them to their coresponding elements inlhs
?
For the copy assignment operator - yes.
For the copy constructor - no. There, each element is copy-constructed (not -assigned) from the corresponding element of the other array. Like this:
Arr(const Arr &other) : arr_{other.arr_[0], other.arr_[1], ..., other.arr_[6]} {}
This is impossible to do with a manually written loop. If you want to do it manually, you need to manually spell out each element.
But, of course, if both copy constructor and copy assignment are trivial (meaning they effectively copy the struct byte by byte, without doing anything else), it doesn't matter if the copy-constructor copy-constructs each element or copy-assigns it, because both do the same thing.
Note that being trivial is unrelated to being compiler-generated. The question gave me a feel that there might some confusion here.