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
65
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
  • 1,940
  • 1
  • 21
  • 40
64
votes
1 answer

Should all/most setter functions in C++11 be written as function templates accepting universal references?

Consider a class X with N member variables, each of some copiable and movable type, and N corresponding setter functions. In C++98, the definition of X would likely look something like this: class X { public: void set_a(A const& a) { _a = a; } …
Andy Prowl
  • 114,596
  • 21
  • 355
  • 432
63
votes
4 answers

Why do you use std::move when you have && in C++11?

Possible Duplicate: What is move semantics? I recently attended a C++11 seminar and the following tidbit of advice was given. when you have && and you are unsure, you will almost always use std::move Could any one explain to me why you should use…
pyCthon
  • 10,295
  • 16
  • 62
  • 122
63
votes
2 answers

Can I typically/always use std::forward instead of std::move?

I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers…
Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
60
votes
4 answers

How does Rust provide move semantics?

The Rust language website claims move semantics as one of the features of the language. But I can't see how move semantics is implemented in Rust. Rust boxes are the only place where move semantics are used. let x = Box::new(5); let y: Box =…
user3335
  • 1,025
  • 1
  • 9
  • 12
60
votes
2 answers

Move-only version of std::function

Because std::function is copyable, the standard requires that callables used to construct it also be copyable: n337 (20.8.11.2.1) template function(F f); Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument…
orm
  • 2,445
  • 1
  • 17
  • 33
58
votes
2 answers

Why does std::move prevent RVO?

In many cases when returning a local from a function, RVO kicks in. However, I thought that explicitly using std::move would at least enforce moving when RVO does not happen, but that RVO is still applied when possible. However, it seems that this…
cdoubleplusgood
  • 1,199
  • 1
  • 9
  • 11
57
votes
2 answers

Why is derived class move constructible when base class isn't?

Consider the following example: #include #include #include template struct Foo : public Base { using Base::Base; }; struct Bar { Bar(const Bar&) { } Bar(Bar&&) = delete; }; int main() { …
Dmitry
  • 1,123
  • 8
  • 18
54
votes
3 answers

Move constructor on derived object

When you have a derived object with a move constructor, and the base object also has move semantics, what is the proper way to call the base object move constructor from the derived object move constructor? I tried the most obvious thing first: …
Channel72
  • 22,459
  • 30
  • 97
  • 168
50
votes
1 answer

Overload on reference, versus sole pass-by-value + std::move?

It seems the main advice concerning C++0x's rvalues is to add move constructors and move operators to your classes, until compilers default-implement them. But waiting is a losing strategy if you use VC10, because automatic generation probably won't…
dean
  • 31
  • 3
  • 8
48
votes
2 answers

Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

I was hoping stringstream has a constructor that steals its initial content from a string&&. Do such inter-species "move constructors" generally not exist in the STL? If not, why not?
Museful
  • 5,715
  • 2
  • 36
  • 53
48
votes
2 answers

What does the standard library guarantee about self move assignment?

What does the C++11 standard say about self move assignment in relation to the standard library? To be more concrete, what, if anything, is guaranteed about what selfAssign does? template std::vector selfAssign(std::vector v) { v =…
Bjarke H. Roune
  • 3,337
  • 2
  • 17
  • 24
47
votes
4 answers

Is a moved-from vector always empty?

I know that generally the standard places few requirements on the values which have been moved from: N3485 17.6.5.15 [lib.types.movedfrom]/1: Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be…
Billy ONeal
  • 97,781
  • 45
  • 291
  • 525
47
votes
1 answer

Is std::array movable?

Is std::array movable? In Bjarne Native 2012 presentation slides (slide 41) it lists std::array as one of the only containers that isn't movable. A quick look on gcc 4.8 libraries source code seems to confirm that std::array is not…
Alessandro Stamatto
  • 1,449
  • 2
  • 14
  • 18