What do I miss here?
Yubin
> Both func_lref() and func_rref() return references to objects that are
> destroyed at function exit. func() returns a vector by value.
How about this one:
vector<T> func() {
vector<T> vec;
// ...
return vec;
}
// This should construct `somevec' using a move constructor such that it
// would me more efficient
vector<T> somevec = func();
I am also returning a to-be-destroyed object, but it a move constructor is
still invoked.
Yubin
vector<T>& bad_vector_func_1() {
vector<T> vec;
// ...
return vec;
}
vector<T>&& bad_vector_func_1() {
vector<T> vec;
// ...
return move(vec);
}
int& f() {
return 5;
}
> We should move this conversation to another forum. Forget move semantics for
> a moment and tell me what's wrong with this function:
>
> int& f() {
> return 5;
> }
I know what you mean. I just to can't understand the point of the move
semantic, therefore I want to see its difference with a canonical copy
constructor (that is, using a T& rather than T&&). Please don't bother
with the contrived example above.
Yubin
> So, so far for the move semantic, isn't it that what we have is another fancy
> name for the same thing?
'Copy' is for making a copy without modifying original object. 'Move' is for making a copy potentially modifying original objecy. There is a large class of situations where latter performs much better.
Now turn to another topic. As the subject indicates, I cannot see the point of
move constructors when we already have copy constructor. For me, both move
constructors and copy constructors *have* to do copying:
Some people claim that move semantic allows programmers to "move" instead of
doing a bloody copying which might incur huge overhead. From the claims I saw,
it is as if move semantic can silently bind an object to another name instead
of doing any internal data copying. However this is not true. As you can see
from the example above, we still have to copy the internal data.
Yubin
On Wed, Jan 17, 2018 at 08:23:10PM -0600, Michael Kilburn wrote:
> 'Copy' is for making a copy without modifying original object. 'Move' is
> for making a copy potentially modifying original object. There is a large
> class of situations where latter performs much better.
So the point here is that a copy constructor disallow modifying original
object while a move constructor allow that, right. From this perspective, why
would latter performs much better?