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
1846
votes
12 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
  • 41,038
  • 26
  • 94
  • 136
901
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
  • 43,567
  • 23
  • 113
  • 204
766
votes
8 answers

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

What is it? What does it do? When should it be used? Good links are appreciated.
Basilevs
  • 18,490
  • 15
  • 51
  • 98
474
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
  • 16,620
  • 11
  • 48
  • 70
245
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,637
  • 4
  • 17
  • 23
208
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,112
  • 2
  • 10
  • 14
166
votes
6 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
  • 24,047
  • 15
  • 89
  • 182
147
votes
7 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,733
  • 2
  • 11
  • 12
145
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
  • 237,063
  • 85
  • 359
  • 638
141
votes
1 answer

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,243
  • 2
  • 14
  • 23
135
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
  • 70,115
  • 19
  • 169
  • 239
135
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,234
  • 5
  • 22
  • 25
133
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
  • 179,402
  • 46
  • 391
  • 527
133
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
  • 51,545
  • 25
  • 157
  • 246
130
votes
4 answers

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

Is this struct Example { int a, b; Example(int mA, int mB) : a{mA}, b{mB} { } Example(const Example& mE) : a{mE.a}, b{mE.b} { } Example(Example&& mE) : a{move(mE.a)}, b{move(mE.b)} { } Example&…
Vittorio Romeo
  • 82,972
  • 25
  • 221
  • 369
1
2 3
99 100