Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use move semantics with std::string during function return? [duplicate]

Tags:

Possible Duplicate:
C++11 rvalues and move semantics confusion

What I think is correct is

std::string GetLine() { std::string str; std::getline(std::cin, str); return std::move(str); } 

But at this link http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html ( check the header part Returning an explicit rvalue-reference from a function)

which is #1 google search hit for move semantics shows a similar function signature as

int&& GetInt() { int x = 0; // code here return std::move(x); } 

From what I read at other places && means rvalue reference so in this case its returning a reference to an object that doesn't exist.

So which is it?

(Yes I know moving an int has no real benifit but the question is whether to use the return type of std::string or std::string&& in the first function. And if that is how it should be done for all types.)

like image 352
EddieV223 Avatar asked Aug 17 '12 18:08

EddieV223


People also ask

Can you return std :: move ()?

std::move is totally unnecessary when returning from a function, and really gets into the realm of you -- the programmer -- trying to babysit things that you should leave to the compiler.

Does std :: move make a copy?

std::move is actually just a request to move and if the type of the object has not a move constructor/assign-operator defined or generated the move operation will fall back to a copy.

When should I use move semantics?

Move semantics allows you to avoid unnecessary copies when working with temporary objects that are about to evaporate, and whose resources can safely be taken from that temporary object and used by another.

What does std :: move () do?

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. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.


1 Answers

You are absolutely correct that the int&& GetInt() example is wrong, and is returning a reference to an object that is destroyed. However, unless I missed it, the link you posted does not actually show any code returning a reference to a local variable. Instead I see a reference to a global variable being returned, which is okay.

Here is how you use move semantics when returning:

std::string func() {     std::string rv;     /* ... */     return rv; } 

You generally should not use std::move() when returning an object. The reason for this is that moving is already implicitly allowed anytime RVO could occur, and using std::move() will suppress RVO. So using std::move() will never be better and will often be worse than just returning normally.


Again, using std::move() can be worse than simply naming the variable to be returned because it suppresses the return value optimization. The return value optimization allows for an object to be returned to the caller without needing to copy that object

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

— [class.copy] 12.8/31

But using std::move() prevents the return expression from being the name of the object you're returning. Instead the expression is more complicated and the language is no longer allowed to give it special handling.

The reason just naming the object is not worse than using std::move() is because there's another rule that says an expression can already be treated as an rvalue without needing std::move().

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.

like image 99
bames53 Avatar answered Oct 23 '22 01:10

bames53