Linked Questions
49 questions linked to/from c++11 Return value optimization or move?
528 votes
6 answers
146k views
C++11 rvalues and move semantics with return statement
I'm trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example: std::vector<...
185 votes
6 answers
127k views
When should std::move be used on a function return value? [duplicate]
In this case struct Foo {}; Foo meh() { return std::move(Foo()); } I'm pretty sure that the move is unnecessary, because the newly created Foo will be an xvalue. But what in cases like these? ...
3 votes
3 answers
893 views
Should I use std::move on functions returning std::vector? [duplicate]
Consider this kind of a function: std::vector<int> generateVector() { return std::vector<int>(10, 0); } Are there any benefits in calling generateVector() like this: std::vector<...
4 votes
1 answer
5k views
Return rvalue reference vs return by value in function return type [duplicate]
In my code I have a function that constructs a string from a piece of data and then returns it. This string isn't used anywhere else, so it's safe for the receiving side to use move-assignment or move-...
2 votes
1 answer
2k views
C++: should I explicitly use std::move() in a return statement to force a move? [duplicate]
EDIT: it is NOT a duplicate because this question asks about a compiler's decision in O0. It is said here that Name Return Value Optimization (NRVO) is an optimization many compiler support. But is ...
1 vote
2 answers
120 views
Is this a correct usage of rvalue references? [duplicate]
Consider the following function: vector<int> get_vector() { vector<int> xs; // Do some stuff to fill the vector with numbers... return xs; } Would it make sense to write the ...
2 votes
0 answers
229 views
Returning std::string properly using Move Semantics [duplicate]
Consider a function that returns a large string: std::string build_huge_string(unsigned long size) { std::string result; result.reserve(size); for (unsigned long i = 0; i < size; ++i) ...
-3 votes
1 answer
85 views
Will std::move() upon object construction in return statement help or prevent RVO? [duplicate]
Due to widely ranging responses from the community, I am asking this in hopes to debunk implementation-specific responses from stack-overflow users. Which of these is best-practice (offers greatest ...
0 votes
0 answers
74 views
What happens when copy cannot be elided? [duplicate]
I understand copy elision well. But I can't find out what happens when copy elision is not possible. In those situations, if available, is the move constructor or move assignment operator guaranteed ...
1 vote
0 answers
76 views
Are function-local objects guaranteed to be returned as rvalue-references? [duplicate]
In the following simple code I return a function local object from a function (factory function). Does the C++ standard guarantee in every case that this object is being returned as an rvalue ...
409 votes
5 answers
509k views
Start thread with member function
I am trying to construct a std::thread with a member function that takes no arguments and returns void. I can't figure out any syntax that works - the compiler complains no matter what. What is the ...
151 votes
19 answers
181k views
Default value to a parameter while passing by reference in C++
Is it possible to give a default value to a parameter of a function while we are passing the parameter by reference. in C++ For example, when I try to declare a function like: virtual const ULONG ...
115 votes
6 answers
141k views
Why is it OK to return a 'vector' from a function?
Please consider this code. I have seen this type of code several times. words is a local vector. How is it possible to return it from a function? Can we guarantee it will not die? std::vector<...
20 votes
1 answer
2k views
When is explicit move needed for a return statement?
In a comment to another question Jonathan Wakely responds to my statement: You never need explicit move for a local variable function return value. It's implicit move there -> ... never say ...
5 votes
2 answers
854 views
std::move and RVO optimizations
I've recently read how std::move can speed up code by just moving the values instead of copying them. So I made a test program to compare the speed using std::vector. The code: #include <iostream&...