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 .

2073 questions
2065
votes
11 answers

What is move semantics?

I just finished listening to the Software Engineering radio podcast interview with Scott Meyers regarding C++0x. Most of the new features made sense to me, and I am actually excited about C++0x now, with the exception of one. I still don't get move…
dicroce
  • 45,396
  • 28
  • 101
  • 140
1146
votes
7 answers

push_back vs emplace_back

I'm a bit confused regarding the difference between push_back and emplace_back. void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); As there is a push_back overload taking a rvalue reference I don't quite…
ronag
  • 49,529
  • 25
  • 126
  • 221
1071
votes
9 answers

What is std::move(), and when should it be used and does it actually move anything?

What is it? What does it do? When should it be used? Good links are appreciated.
Basilevs
  • 22,440
  • 15
  • 57
  • 102
517
votes
6 answers

C++11 rvalues and move semantics confusion (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 return_vector(void) { std::vector tmp…
Tarantula
  • 19,031
  • 12
  • 54
  • 71
267
votes
3 answers

What is "rvalue reference for *this"?

Came across a proposal called "rvalue reference for *this" in clang's C++11 status page. I've read quite a bit about rvalue references and understood them, but I don't think I know about this. I also couldn't find much resources on the web using the…
ryaner
  • 3,927
  • 4
  • 20
  • 23
213
votes
2 answers

Can modern C++ get you performance for free?

It is sometimes claimed that C++11/14 can get you a performance boost even when merely compiling C++98 code. The justification is usually along the lines of move semantics, as in some cases the rvalue constructors are automatically generated or now…
alarge
  • 2,162
  • 2
  • 13
  • 14
208
votes
8 answers

Why would I std::move an std::shared_ptr?

I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr Value) { Invocation = std::move(Value); } Why would I want to std::move an std::shared_ptr?…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
195
votes
2 answers

Cannot move out of borrowed content / cannot move out of behind a shared reference

I don't understand the error cannot move out of borrowed content. I have received it many times and I have always solved it, but I've never understood why. For example: for line in self.xslg_file.iter() { self.buffer.clear(); for…
Peekmo
  • 2,843
  • 2
  • 19
  • 25
173
votes
10 answers

How do I use a custom deleter with a std::unique_ptr member?

I have a class with a unique_ptr member. class Foo { private: std::unique_ptr bar; ... }; The Bar is a third party class that has a create() function and a destroy() function. If I wanted to use a std::unique_ptr with it in a stand…
huitlarc
  • 1,993
  • 2
  • 13
  • 12
166
votes
3 answers

What is the advantage of using forwarding references in range-based for loops?

const auto& would suffice if I want to perform read-only operations. However, I have bumped into for (auto&& e : v) // v is non-const a couple of times recently. This makes me wonder: Is it possible that in some obscure corner cases there is some…
Ali
  • 56,466
  • 29
  • 168
  • 265
165
votes
6 answers

When should std::move be used on a function return value?

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? struct Foo {}; Foo meh() { Foo foo; //do…
user2015453
  • 4,844
  • 5
  • 25
  • 27
164
votes
2 answers

What can I do with a moved-from object?

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be sufficient. For example, take the function template…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
162
votes
4 answers

Is a `=default` move constructor equivalent to a member-wise move constructor?

Is this struct Example { string a, b; Example(Example&& mE) : a{move(mE.a)}, b{move(mE.b)} { } Example& operator=(Example&& mE) { a = move(mE.a); b = move(mE.b); return *this; } } equivalent to this struct Example { string a,…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
145
votes
6 answers

Move assignment operator and `if (this != &rhs)`

In the assignment operator of a class, you usually need to check if the object being assigned is the invoking object so you don't screw things up: Class& Class::operator=(const Class& rhs) { if (this != &rhs) { // do the assignment …
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
139
votes
2 answers

Why is `std::move` named `std::move`?

The C++11 std::move(x) function doesn't really move anything at all. It is just a cast to r-value. Why was this done? Isn't this misleading?
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
1
2 3
99 100