Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an object guaranteed to be moved when it is returned?

I know that when passing an object by value to a function, the move constructor is always called if there is one, assuming no copy elision. What about returning an object by value?

For example, say we have a class Foo which has a move constructor, and we have a function that returns a Foo object.

Foo g() {
    Foo f;

    // do something with f

    return f;
}

If we assume there is no RVO, is the move constructor guaranteed to be called?

Update: I guess I didn't show my intention clearly. I just want to know I can in the worst case have the object moved not copied. Either RVO or NRVO happens, I am happy. And I should also say that move constructor and move assignment are not deleted and are properly implemented.

like image 471
haotang Avatar asked Jun 18 '12 17:06

haotang


People also ask

What does do not rely on value of moved from an object?

Do not rely on the value of a moved-from object unless the type of the object is documented to be in a well-specified state. While the object is guaranteed to be in a valid state, relying on unspecified values leads to unspecified behavior.

When should we use std:: move?

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object.


2 Answers

Yes. See [class.copy] p32

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided. — end note ]

like image 92
Jonathan Wakely Avatar answered Sep 21 '22 20:09

Jonathan Wakely


In this case, since the return value has a name (f), it would be NRVO (named return value optimization) that would apply.

So, the technical answer based only on wording, is that the absence of RVO won't prevent copy elision, since NRVO could still allow it.

Past that, I believe the selection between move/copy of the return value can/will depend on the definition of Foo -- there are definitely times it'll be copied instead of moved, such as if you've explicitly deleted the move constructor and move assignment operators, or you haven't defined move construction/assignment, and it isn't eligible for them being synthesized implicitly.

Edit: [responding to edited question]: Having a move constructor still doesn't guarantee that the result will be moved. One obvious example would be if you had deleted the move assignment operator, and were assigning the result (rather than using it to initialize). In this case, the deleted move assignment operator would prevent moving the return value.

To answer what you may have been getting at, though, the general rule is that moving will be done if possible, and it'll fall back to copying if and only if something prevents the result from being moved.

like image 45
Jerry Coffin Avatar answered Sep 18 '22 20:09

Jerry Coffin