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
132
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
  • 205,094
  • 128
  • 528
  • 886
123
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
  • 5,486
  • 3
  • 30
  • 49
122
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
  • 256,549
  • 94
  • 388
  • 662
120
votes
8 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
  • 228,013
  • 71
  • 433
  • 510
110
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,311
  • 2
  • 9
  • 7
108
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
  • 15,723
  • 4
  • 60
  • 67
104
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
  • 49,529
  • 25
  • 126
  • 221
103
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,448
  • 4
  • 23
  • 36
103
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,838
  • 10
  • 52
  • 99
93
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,825
  • 5
  • 58
  • 90
89
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
  • 3,236
  • 2
  • 25
  • 38
85
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
  • 7,562
  • 10
  • 46
  • 70
84
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,795
  • 2
  • 21
  • 28
82
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
  • 21,587
  • 28
  • 106
  • 187
76
votes
4 answers

Why do some people use swap for move assignments?

For example, stdlibc++ has the following: unique_lock& operator=(unique_lock&& __u) { if(_M_owns) unlock(); unique_lock(std::move(__u)).swap(*this); __u._M_device = 0; __u._M_owns = false; return *this; } Why not just…
Display Name
  • 2,323
  • 1
  • 26
  • 45