18

A C++Next blog post said that

A compute(…)
{
    A v;
    …
    return v;
}

If A has an accessible copy or move constructor, the compiler may choose to elide the copy. Otherwise, if A has a move constructor, v is moved. Otherwise, if A has a copy constructor, v is copied. Otherwise, a compile time error is emitted.

I thought I should always return the value without std::move because the compiler would be able to figure out the best choice for users. But in another example from the blog post

Matrix operator+(Matrix&& temp, Matrix&& y)
  { temp += y; return std::move(temp); }

Here the std::move is necessary because y must be treated as an lvalue inside the function.

Ah, my head almost blow up after studying this blog post. I tried my best to understand the reasoning but the more I studied, the more confused I became. Why should we return the value with the help of std::move?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
StereoMatching
  • 4,971
  • 6
  • 38
  • 70

4 Answers4

28

So, lets say you have:

A compute()
{
  A v;
  …
  return v;
}

And you're doing:

A a = compute();

There are two transfers (copy or move) that are involved in this expression. First the object denoted by v in the function must be transferred to the result of the function, i.e. the value donated by the compute() expression. Let's call that Transfer 1. Then, this temporary object is transferred to create the object denoted by a - Transfer 2.

In many cases, both Transfer 1 and 2 can be elided by the compiler - the object v is constructed directly in the location of a and no transferring is necessary. The compiler has to make use of Named Return Value Optimization for Transfer 1 in this example, because the object being returned is named. If we disable copy/move elision, however, each transfer involves a call to either A's copy constructor or its move constructor. In most modern compilers, the compiler will see that v is about to be destroyed and it will first move it into the return value. Then this temporary return value will be moved into a. If A does not have a move constructor, it will be copied for both transfers instead.

Now lets look at:

A compute(A&& v)
{
  return v;
}

The value we're returning comes from the reference being passed into the function. The compiler doesn't just assume that v is a temporary and that it's okay to move from it1. In this case, Transfer 1 will be a copy. Then Transfer 2 will be a move - that's okay because the returned value is still a temporary (we didn't return a reference). But since we know that we've taken an object that we can move from, because our parameter is an rvalue reference, we can explicitly tell the compiler to treat v as a temporary with std::move:

A compute(A&& v)
{
  return std::move(v);
}

Now both Transfer 1 and Transfer 2 will be moves.


1 The reason why the compiler doesn't automatically treat v, defined as A&&, as an rvalue is one of safety. It's not just too stupid to figure it out. Once an object has a name, it can be referred to multiple times throughout your code. Consider:

A compute(A&& a)
{
  doSomething(a);
  doSomethingElse(a);
}

If a was automatically treated as an rvalue, doSomething would be free to rip its guts out, meaning that the a being passed to doSomethingElse may be invalid. Even if doSomething took its argument by value, the object would be moved from and therefore invalid in the next line. To avoid this problem, named rvalue references are lvalues. That means when doSomething is called, a will at worst be copied from, if not just taken by lvalue reference - it will still be valid in the next line.

It is up to the author of compute to say, "okay, now I allow this value to be moved from, because I know for certain that it's a temporary object". You do this by saying std::move(a). For example, you could give doSomething a copy and then allow doSomethingElse to move from it:

A compute(A&& a)
{
  doSomething(a);
  doSomethingElse(std::move(a));
}
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Do you mean the compiler can implicitly move or elide automatic object only?So in the second case, we should use std::move to help the compiler understand that &&v is a rvalue because our mister compiler not smart enough to know that we don't need v anymore? – StereoMatching Nov 17 '12 at 14:06
  • 1
    Well, The Truth is that a named rvalue reference is an lvalue expression. The expression `v` is an lvalue, even though its type is an rvalue reference. It's not that the compiler isn't smart enough - it would be dangerous for it to treat it as an rvalue. I'll add a bit to my answer to explain why. – Joseph Mansfield Nov 17 '12 at 14:11
  • Thanks, I feel that I know what is rvalue reference "once again" .This thing is pretty complicated, I wonder that this feature would be used by those libraries developers and compiler vendors only. – StereoMatching Nov 17 '12 at 14:31
  • What happens of the parameter itself is a copy: `A compute(A a)`. Will that NRVO or do we need to explicitly `std::move` it? – Galik Aug 06 '14 at 12:15
5

An implicit move of function results is only possible for automatic objects. An rvalue reference parameter does not denote an automatic object, hence you have to request the move explicitly in that case.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • The key point is that `v` in the first example is being destroyed anyway at the end of the function, so it makes sense for the last operation on it to be a move rather than a copy. However in the second example, the object referred to by `temp` will live on past the end of the function. – M.M Sep 14 '17 at 22:48
5

The first takes advantage of NVRO which is even better than moving. No copy is better than a cheap one.

The second cannot take advantage of NVRO. Assuming no elision, return temp; would call the copy constructor and return std::move(temp); would call the move constructor. Now, I believe either of these have equal potential to be elided and so you should go with the cheaper one if not elided, which is using std::move.

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • 3
    Calling `std::move` will inhibit NRVO, since the expression of the return statement is not "the name of a non-volatile automatic object" anymore (not that `temp` was such a name before, but anyways). – Xeo Nov 17 '12 at 13:32
1

In C++17 and later

C++17 changed the definition of value categories so that the kind of copy elision you describe is guaranteed (see: How does guaranteed copy elision work?). Thus if you write your code in C++17 or later you should absolutely not return by std::move()'ing in this case.

einpoklum
  • 118,144
  • 57
  • 340
  • 684