Linked Questions

528 votes
6 answers
146k views

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<...
Tarantula's user avatar
  • 20.1k
185 votes
6 answers
127k views

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? ...
user2015453's user avatar
  • 5,204
3 votes
3 answers
893 views

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<...
user2061057's user avatar
  • 1,042
4 votes
1 answer
5k views

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-...
Samuel Moriarty's user avatar
2 votes
1 answer
2k views

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 ...
Leedehai's user avatar
  • 4,050
1 vote
2 answers
120 views

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 ...
aochagavia's user avatar
  • 6,346
2 votes
0 answers
229 views

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) ...
Arya Pourtabatabaie's user avatar
-3 votes
1 answer
85 views

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 ...
Roman's user avatar
  • 13
0 votes
0 answers
74 views

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 ...
RajV's user avatar
  • 7,272
1 vote
0 answers
76 views

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 ...
glades's user avatar
  • 5,374
409 votes
5 answers
509k views

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 ...
abergmeier's user avatar
  • 14.2k
151 votes
19 answers
181k views

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 ...
user avatar
115 votes
6 answers
141k views

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<...
Pranit Kothari's user avatar
20 votes
1 answer
2k views

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 ...
Martin Ba's user avatar
  • 39.4k
5 votes
2 answers
854 views

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&...
Hatted Rooster's user avatar

15 30 50 per page