https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99117

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The problem is that sum + e returns an expression template that holds
references to its operands, so that the sum is done lazily.

When that expression template result is assigned back to sum it evaluates the
sum of each element, which is basically:

struct valarray {
  int* __restrict__ _M_data;
  size_t _M_size;
};
valarray sum{ new int[2]{1,1}, 2 };
valarray rhs{ new int[2]{2,2}, 2 };
int* p = sum._M_data;
int* e1 = sum._M_data;
int* e2 = rhs._M_data;
for (size_t i = 0; i < sum._M_size; ++i, ++p)
  *p = e1[i] + e2[i];

Where p and e1 alias, but are marked with __restrict__.

Maybe we need to check for such aliasing in __valarray_copy.

Reply via email to