Questions tagged [move-semantics]

Move semantics is a programming language feature that allows a copy operation to be replaced by a more efficient "move" when the source object is a temporary or an otherwise expiring object.

Use this tag for questions about move semantics, move constructors and move assignment.

Move semantics is a programming language feature that allows a copy operation to be replaced by a more efficient "move" when the source object is a temporary or an otherwise expiring object.

For more information on move semantics in C++, see Rvalue references and move constructors.

Related tags are , and .

1821 questions
46
votes
1 answer

Move or Named Return Value Optimization (NRVO)?

Lets say we have the following code: std::vector f() { std::vector y; ... return y; } std::vector x = ... x = f(); It seems the compiler has two approaches here: (a) NRVO: Destruct x, then construct f() in place of x. (b)…
Clinton
  • 20,364
  • 13
  • 59
  • 142
45
votes
5 answers

Why does moving a pointer variable not set it to null?

When implementing move constructors and move assignment operators, one often writes code like this: p = other.p; other.p = 0; The implicitly defined move operations would be implemented with code like this: p = std::move(other.p); Which would be…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
43
votes
6 answers

How to actually implement the rule of five?

UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five…
stijn
  • 31,563
  • 13
  • 95
  • 145
42
votes
2 answers

Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

In C++11 we can transfer the ownership of an object to another unique_ptr using std::move(). After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr. std::unique_ptr p1(new…
Roy
  • 1,306
  • 1
  • 12
  • 11
42
votes
1 answer

Why does Visual Studio not perform return value optimization (RVO) in this case

I was answering a question and recommending return by-value for a large type because I was confident the compiler would perform return-value optimization (RVO). But then it was pointed out to me that Visual Studio 2013 was not performing RVO on my…
Chris Drew
  • 14,004
  • 3
  • 30
  • 51
41
votes
1 answer

Const reference VS move semantics

I was wondering in which situations I still need to use const references in parameters since C++11. I don't fully understand move semantics but I think this is a legit question. This question is meant for only the situations where a const reference…
Tim
  • 5,137
  • 7
  • 32
  • 62
40
votes
4 answers

Is std::move really needed on initialization list of constructor for heavy members passed by value?

Recently I read an example from cppreference.../vector/emplace_back: struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) :…
PiotrNycz
  • 20,687
  • 7
  • 55
  • 102
37
votes
4 answers

Is specializing std::swap deprecated now that we have move semantics?

Possible Duplicate: Move semantics == custom swap function obsolete? This is how std::swap looks like in C++11: template void swap(T& x, T& y) { T z = std::move(x); x = std::move(y); y = std::move(z); } Do I still have to…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
37
votes
3 answers

Passing by value vs const & and && overloads

So after looking up move semantics I see that general consensus is to pass by value when you intend to transfer ownership. But in Scott Meyer's talk on Universal references I've noticed that std::vector::push_back has 2 overloads: void push_back(…
user3624760
36
votes
8 answers

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 be used to optimize such string a = x + y stuff.…
UmmaGumma
  • 5,633
  • 1
  • 28
  • 45
35
votes
1 answer

Is std::move(*this) a good pattern?

In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun()…
alfC
  • 10,293
  • 4
  • 42
  • 88
35
votes
2 answers

What are the rules for automatic generation of move operations?

In C++98, the C++ compiler could automatically generate copy constructor and copy assignment operator via member-wise copy, e.g. struct X { std::string s; std::vector v; int n; }; The compiler automatically generates copy…
Mr.C64
  • 37,988
  • 11
  • 76
  • 141
34
votes
5 answers

Why is passing by value (if a copy is needed) recommended in C++11 if a const reference only costs a single copy as well?

I am trying to understand move semantics, rvalue references, std::move, etc. I have been trying to figure out, by searching through various questions on this site, why passing a const std::string &name + _name(name) is less recommended than a…
John Bonata
  • 353
  • 3
  • 6
34
votes
1 answer

why is the destructor call after the std::move necessary?

In The C++ programming language Edition 4 there is an example of a vector implementation, see relevant code at the end of the message. uninitialized_move() initializes new T objects into the new memory area by moving them from the old memory area.…
34
votes
6 answers

What is the behaviour of compiler generated move constructor?

Does std::is_move_constructible::value == true imply that T has a usable move constructor? If so, what is the default behaviour of it? Consider the following case: struct foo { int* ptr; }; int main() { { std::cout <<…
Frahm
  • 757
  • 7
  • 15