Linked Questions
61 questions linked to/from C++11 rvalues and move semantics with return statement
229 votes
4 answers
146k views
c++11 Return value optimization or move? [duplicate]
I don't understand when I should use std::move and when I should let the compiler optimize... for example: using SerialBuffer = vector< unsigned char >; // let compiler optimize it ...
33 votes
4 answers
15k views
Is this correct usage of C++ 'move' semantics? [duplicate]
Tonight I've been taking a look at some code I've been working on over the last few days, and began reading up on move semantics, specifically std::move. I have a few questions to ask you pros to ...
25 votes
3 answers
36k views
How to use move semantics with std::string during function return? [duplicate]
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); } ...
35 votes
3 answers
25k views
Returning std::vector with std::move [duplicate]
I have a very basic question: is it a good idea to return a std::vector<A> using std::move? For, example: class A {}; std::vector<A> && func() { std::vector<A> v; /* ...
12 votes
1 answer
21k views
move semantic and struct [duplicate]
I have a function struct foo { std::vector<int> v; }; foo func(); will the vector inside foo be moved or copied when returning from the function?
8 votes
1 answer
713 views
How to return && object from function? [duplicate]
I have TTempTable class with move symantics. I wrote TTempTable&& MyFunction() { TTempTable tmp = f(...); ... return std::move(tmp); } and got no compiler errors. Was this correct?
4 votes
3 answers
229 views
c++ std::move confusion [duplicate]
I'm confused as to what is going on in the following code snippet. Is move really necessary here? What would be the most optimal + safe way of returning the temporary set? set<string> getWords()...
2 votes
1 answer
233 views
Returning an rvalue - what is wrong with this code? [duplicate]
I came across the following code snippet std::string&& test() { std::string m="Hello"; return (std::move(m)); } int main() { std::string&& m = test(); } I understand the ...
1 vote
0 answers
117 views
using std::move for return result of function [duplicate]
which is right? std::string get_string() { std::string result; // ... more operations. return std::move(result); } Or std::string&& get_string() { std::string ...
0 votes
0 answers
89 views
Why is string's operator+ that takes an r-value reference declared to return by value? [duplicate]
I am trying to come to terms with r-value references. I discovered that string's operator+ is always declared to return by value. Why is operator+ defined to return by value: basic_string operator+(...
2 votes
1 answer
148 views
How to know if compiler will use copy elision and if I need to use std::move [duplicate]
How can I trust the compiler for non-guaranteed copy elision (in case of return value optimization) in this example code: struct X { X() : size(10000), very_large_buffer(new char[size]) { } ...
82 votes
11 answers
67k views
Does return statement copy values
I am wondering about this because of scope issues. For example, consider the code typedef struct { int x1;/*top*/ int x2;/*bottom*/ int id; } subline_t; subline_t subline(int x1, int x2, ...
62 votes
7 answers
42k views
Avoiding copy of objects with the "return" statement
I have a very basic question in C++. How to avoid copy when returning an object ? Here is an example : std::vector<unsigned int> test(const unsigned int n) { std::vector<unsigned int>...
54 votes
5 answers
14k views
Is RVO (Return Value Optimization) applicable for all objects?
Is RVO (Return Value Optimization) guaranteed or applicable for all objects and situations in C++ compilers (specially GCC)? If answer is "no", what are the conditions of this optimization for a ...
37 votes
8 answers
7k views
What optimization does move semantics provide if we already have RVO?
As far as I understand one of the purposes of adding move semantics is to optimize code by calling special constructor for copying "temporary" objects. For example, in this answer we see that it can ...