From: Bo Persson on
Mathias Gaunard wrote:
> On Aug 2, 1:10 am, "Bo Persson" <b...(a)gmb.dk> wrote:
>
>> No, they will not have to check for any other states than they
>> already have.
>
> My point is that you will need to introduce an extra "empty" state
> in order to make use of move semantics, and that state may require
> additional branches in each of the object primitives.

Many objects already have an empty state, like the one they get from a
default constructor. If it doesn't, the move operations can leave the
object in an non-empty state. The only requrement is that the object
remains in a valid state. You don't have to invent a special
moved-from state if that is expensive.


Bo Persson


--
[ 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-08-03 15:23, Bo Persson wrote:
> Mathias Gaunard wrote:
>> On Aug 2, 1:10 am, "Bo Persson" <b...(a)gmb.dk> wrote:
>>
>>> No, they will not have to check for any other states than they
>>> already have.
>>
>> My point is that you will need to introduce an extra "empty" state
>> in order to make use of move semantics, and that state may require
>> additional branches in each of the object primitives.
>
> Many objects already have an empty state, like the one they get from a
> default constructor. If it doesn't, the move operations can leave the
> object in an non-empty state. The only requrement is that the object
> remains in a valid state. You don't have to invent a special
> moved-from state if that is expensive.

Right. You don't even have to change the object, in which case a move is
identical to a copy. Most likely it is when the object doesn't own
a resource which you allocate and get a handle for, e.g. dynamically
allocated memory, files, sockets, etc. For example:

struct point
{
double coordinates[3];
double mass;
// ...
};

you just copy the values, and don't even have to reset the original.

However, if you don't have to change the source object, you probably
don't want to bother to define a move constructor, because the copy
constructor does the same thing and will be used in place of the move
constructor if it is not defined. And you often don't even have to define
the copy constructor operator, because the default copy constructor will
just work fine.

So, when you want to define a move constructor, it is when the object owns
a resource, and the copy constructor has to allocate another resource so
that both objects maintain ownership to their respective resources, but
the point of the move constructor is that you don't have to allocate
another resource but can just "steal" the existing one. And you have to
"reset" the source object somehow because it has lost the ownership.

# For every occurrence of "constructor" above, you can substitute
"assignment operator" and everything still holds. It is just too
tedious to write "constructor/assignment operator" each time. :(

--
Seungbeom Kim

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