From: Jeffrey Schwab on
On 7/20/10 10:00 AM, Seungbeom Kim wrote:

> vec[pos] = std::move(vec.back());

What's in vec.back() after that statement? (I'm still coming to terms
with move semantics.)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
On 2010-07-30 19:25, Jeffrey Schwab wrote:
> On 7/20/10 10:00 AM, Seungbeom Kim wrote:
>
>> vec[pos] = std::move(vec.back());
>
> What's in vec.back() after that statement? (I'm still coming to terms
> with move semantics.)

That depends on the value_type of the vector. If it is a simple type
(such as int) or if it doesn't have a move assignment operator, std::move
has no effect, the above assignment is simply a copy, and vec.back()
remains unchanged. If it owns some resource and has a move assignment
operator (such as std::string), the assignment is a move, which takes
the resource from vec.back() and gives it to vec[pos], causing the latter
to release what it previously owned. Then vec.back() is a valid but "null"
object which probably doesn't own any resource; the exact semantics is
defined by the move constructor. For example, vec.back() could be a std::
string object with a null pointer inside, representing an empty string.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bo Persson on
Jeffrey Schwab wrote:
> On 7/20/10 10:00 AM, Seungbeom Kim wrote:
>
>> vec[pos] = std::move(vec.back());
>
> What's in vec.back() after that statement? (I'm still coming to
> terms with move semantics.)

An object that we don't care much about, in a valid but not very
interesting state. Perhaps similar to a default constructed element.

The move assignment operator (if there is one) "steals" the internals
of the object, leaving it as an empty shell. Possibly to be destroyed
soon, or assigned a new value.


Bo Persson



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Jul 31, 3:25 am, Jeffrey Schwab <j...(a)schwabcenter.com> wrote:
> On 7/20/10 10:00 AM, Seungbeom Kim wrote:
>
> > vec[pos] = std::move(vec.back());
>
> What's in vec.back() after that statement? (I'm still coming to terms
> with move semantics.)

It's not clearly stated by the standard what operations must be valid
on a moved-from object.
It seems that destruction and all forms of copy construction and
assignment must remain valid, which means move semantics are not
really overhead-free, since all these primitives will need to check
for the moved-from state.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bo Persson on
Mathias Gaunard wrote:
> On Jul 31, 3:25 am, Jeffrey Schwab <j...(a)schwabcenter.com> wrote:
>> On 7/20/10 10:00 AM, Seungbeom Kim wrote:
>>
>>> vec[pos] = std::move(vec.back());
>>
>> What's in vec.back() after that statement? (I'm still coming to
>> terms with move semantics.)
>
> It's not clearly stated by the standard what operations must be
> valid on a moved-from object.

It must be in a "valid state", whatever that means for the object.

> It seems that destruction and all forms of copy construction and
> assignment must remain valid, which means move semantics are not.
> really overhead-free, since all these primitives will need to check
> for the moved-from state.

No, they will not have to check for any other states than they already
have. The move assignment and move copy construction will have to
leave the object in some valid state that is cheap to set, possibly
looking like it was just default constructed.

There will be some "overhead" in the move operations, but cheaper than
copy construction or copy assignment (otherwise you would just use
those). For example, with a std::vector the move constructor would set
the moved-from object to contains a null pointer, and have zero size
and capacity.


Bo Persson


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]