https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99117
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> --- Something like this prevents the miscompilation, at the cost of an extra copy: --- a/libstdc++-v3/include/std/valarray +++ b/libstdc++-v3/include/std/valarray @@ -838,7 +838,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // _GLIBCXX_RESOLVE_LIB_DEFECTS // 630. arrays of valarray. if (_M_size == __e.size()) - std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); + { + if (__e()._M_expr1._M_data == _M_data + || __e()._M_expr2._M_data == _M_data) + *this = static_cast<valarray>(__e); + else + std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data)); + } else { if (_M_data) (This isn't usable because it accesses private members so only compiles with -fno-access-control, but I wanted to see if it helped).