This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C++0x: rvalue references, std::vector::push_back, move semantics
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Sebastian Mach <phresnel at googlemail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 3 Feb 2010 23:49:08 +0000
- Subject: Re: C++0x: rvalue references, std::vector::push_back, move semantics
re http://gcc.gnu.org/ml/gcc-help/2010-02/msg00055.html
Modify the move constructor slightly, to show 'this' as well as 'f'
and it becomes clear:
foo (foo && f) { std::cout << "Cr " << this << ' ' << &f << "\n"; }
gives
----
C 0x7fff0df100de
C 0x7fff0df100df
operator A|B{{
Cr 0x16c0010 0x7fff0df100df
Cr 0x16c0031 0x7fff0df100de
Cr 0x16c0030 0x16c0010
}}
----
so I hope you can see that an object is move-constructed at location
0x16c0010 from lhs, then another object is move-constructed at
0x16c0031 from rhs, then the object at 0x16c0010 is moved to
0x16c0030.
The second push_back causes a reallocation, which moves ret[0]
If you add ret.reserve(2) before the calls to push_back you don't get
the reallocation:
----
C 0x7ffff943f68e
C 0x7ffff943f68f
operator A|B{{
Cr 0xbaa010 0x7ffff943f68f
Cr 0xbaa011 0x7ffff943f68e
}}
----
This is really nothing to do with rvalues, but rather vector's
reallocation policy.
Hope that helps,
Jonathan