This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Improve insert/emplace robustness to self insertion
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: François Dumont <frs dot dumont at gmail dot com>
- Cc: libstdc++ at gcc dot gnu dot org, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 1 Jul 2016 10:54:08 +0100
- Subject: Re: Improve insert/emplace robustness to self insertion
- Authentication-results: sourceware.org; auth=none
- References: <5762FDAC dot 5000802 at gmail dot com> <20160616202106 dot GB11538 at redhat dot com> <57665607 dot 7080004 at gmail dot com> <20160620074230 dot GB6159 at redhat dot com> <5772D70D dot 4020103 at gmail dot com> <20160629091056 dot GH7722 at redhat dot com> <577424D3 dot 10008 at gmail dot com> <20160629203625 dot GK7722 at redhat dot com> <20160629213021 dot GN7722 at redhat dot com> <57757830 dot 7040106 at gmail dot com>
On 30/06/16 21:51 +0200, FranÃois Dumont wrote:
On 29/06/2016 23:30, Jonathan Wakely wrote:
iterator
insert(const_iterator __position, value_type&& __x)
{ return emplace(__position, std::move(__x)); }
That's suboptimal, since in the general case we need an extra
construction for emplacing, but we know that we don't need to do that
when inserting rvalues.
Why not ? I realized with your remarks that I was missing some
tests in the new self_insert.cc. The ones to insert an rvalue coming
from the vector itself. In the attached patch there is those 2 tests,
do you agree with expected behavior ? For the moment it doesn't check
that the source value has been indeed moved cause it doesn't, I will
update it once it does.
No, I don't agree, because this is undefined behaviour:
vv.insert(vv.begin(), std::move(vv[0]));
We don't need to support that case.
17.6.4.9 [res.on.arguments] says:
â If a function argument binds to an rvalue reference parameter, the
implementation may assume that this parameter is a unique reference
to this argument.
i.e. when passed an rvalue we can assume it is not a reference to
something in the container.
That's why we should not perform any more operations when inserting
rvalues than we do now. Any increase in copies/moves for inserting
rvalues is a regression, and should be avoided.