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
128
votes
4 answers

When to make a type non-movable in C++11?

I was surprised this didn't show up in my search results, I thought someone would've asked this before, given the usefulness of move semantics in C++11: When do I have to (or is it a good idea for me to) make a class non-movable in C++11? (Reasons…
user541686
  • 189,354
  • 112
  • 476
  • 821
108
votes
2 answers

How does std::move() transfer values into RValues?

I just found myself not fully understanding the logic of std::move(). At first, I googled it but seems like there are only documents about how to use std::move(), not how its structure works. I mean, I know what the template member function is but…
Dean Seo
  • 4,842
  • 3
  • 22
  • 47
103
votes
8 answers

initializer_list and move semantics

Am I allowed to move elements out of a std::initializer_list? #include #include template void foo(std::initializer_list list) { for (auto it = list.begin(); it != list.end(); ++it) { …
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
102
votes
5 answers

Can I list-initialize a vector of move-only type?

If I pass the following code through my GCC 4.7 snapshot, it tries to copy the unique_ptrs into the vector. #include #include int main() { using move_only = std::unique_ptr; std::vector v { move_only(),…
R. Martinho Fernandes
  • 209,766
  • 68
  • 412
  • 492
101
votes
4 answers

Why do we copy then move?

I saw code somewhere in which someone decided to copy an object and subsequently move it to a data member of a class. This left me in confusion in that I thought the whole point of moving was to avoid copying. Here is the example: struct S { …
user2030677
  • 3,160
  • 3
  • 19
  • 30
97
votes
3 answers

How to enforce move semantics when a vector grows?

I have a std::vector of objects of a certain class A. The class is non-trivial and has copy constructors and move constructors defined. std::vector myvec; If I fill-up the vector with A objects (using e.g. myvec.push_back(a)), the vector will…
Bertwim van Beest
  • 1,081
  • 1
  • 8
  • 4
97
votes
2 answers

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object? For instance, is…
Riot
  • 13,698
  • 3
  • 55
  • 59
89
votes
4 answers

Why no default move-assignment/move-constructor?

I'm a simple programmer. My class members variables most often consists of POD-types and STL-containers. Because of this I seldom have to write assignment operators or copy constructors, as these are implemented by default. Add to this, if I use…
Viktor Sehr
  • 12,172
  • 3
  • 52
  • 78
88
votes
3 answers

Reusing a moved container?

What is the correct way to reuse a moved container? std::vector container; container.push_back(1); auto container2 = std::move(container); // ver1: Do nothing //container2.clear(); // ver2: "Reset" container = std::vector() // ver3:…
ronag
  • 43,567
  • 23
  • 113
  • 204
83
votes
3 answers

Passing std::string by Value or Reference

Possible Duplicate: Are the days of passing const std::string & as a parameter over? Should I pass std::string by value or by reference (to a un-inlined function) if move semantics is supported? And what about implementations using small string…
Nordlöw
  • 11,017
  • 6
  • 50
  • 92
80
votes
5 answers

Is there any case where a return of a RValue Reference (&&) is useful?

Is there a reason when a function should return a RValue Reference? A technique, or trick, or an idiom or pattern? MyClass&& func( ... ); I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we? T&…
towi
  • 20,210
  • 25
  • 94
  • 167
79
votes
2 answers

Is unique_ptr guaranteed to store nullptr after move?

Is unique_ptr guaranteed to store nullptr after move? std::unique_ptr p1{new int{23}}; std::unique_ptr p2{std::move(p1)}; assert(!p1); // is this always true?
lizarisk
  • 6,692
  • 9
  • 42
  • 66
76
votes
1 answer

Is the default Move constructor defined as noexcept?

It seems that a vector will check if the move constructor is labeled as noexcept before deciding on whether to move or copy elements when reallocating. Is the default move constructor defined as noexcept? I saw the following documentation but it…
bjackfly
  • 2,928
  • 1
  • 22
  • 31
74
votes
7 answers

Is the pass-by-value-and-then-move construct a bad idiom?

Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move. But what happens if a is an lvalue? It seems…
jbgs
  • 2,515
  • 2
  • 17
  • 26
67
votes
1 answer

Workarounds for no 'rvalue references to *this' feature

I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this…
boycy
  • 1,433
  • 12
  • 24