https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125693
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Marc Mutz from comment #5)
> I figured it's protecting against the argument
> aliasing into *this's storage and so by shuffling the elements invalidate
> it's arg.
Right. Something like:
std::vector<std::string> v{ "1", "2" };
v.reserve(3);
v.emplace(v.begin(), std::string_view(v[1]));
Without the temporary, the string view would refer to "1" and we'd get {"1",
"1", "2"}
instead of the expected {"1", "2", "2"}.
> So it takes a copy. On the stack. Boom. The element is larger than
> the maximum possible stack. A rather expensive operation would be to take
> the copy on the heap instead. As in push_back() + rotate into place,
(Unfortunately rotate uses ADL swap and we're not allowed to use that in the
general case, we can only use moves, but that's not relevant to this issue,
just an aside.)
We could switch between storing the temporary on the stack or the heap based on
its size.