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
75
votes
6 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 are implemented in Rust. Rust boxes are the only place where move semantics are used. let x = Box::new(5); let y: Box =…
Fish
  • 1,205
  • 1
  • 10
  • 12
71
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
  • 11,746
  • 20
  • 73
  • 135
69
votes
3 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,835
  • 2
  • 22
  • 35
68
votes
2 answers

Why does std::move prevent RVO (return value optimization)?

In many cases when returning a local from a function, RVO (return value optimization) 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.…
cdoubleplusgood
  • 1,309
  • 1
  • 11
  • 12
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,473
  • 12
  • 25
65
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
  • 124,023
  • 23
  • 387
  • 451
65
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
  • 108,238
  • 20
  • 242
  • 324
62
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
  • 24,139
  • 32
  • 108
  • 180
58
votes
1 answer

Move or Named Return Value Optimization (NRVO)?

Lets say we have the following code: std::vector f() { std::vector y; ... return y; } std::vector x = ... x = f(); It seems the compiler has two approaches here: (a) NRVO: Destruct x, then construct f() in place of x. (b)…
Clinton
  • 22,361
  • 15
  • 67
  • 163
58
votes
2 answers

Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

In C++11 we can transfer the ownership of an object to another unique_ptr using std::move(). After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr. std::unique_ptr p1(new…
iampranabroy
  • 1,716
  • 1
  • 15
  • 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,230
  • 10
  • 19
57
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
  • 104,103
  • 58
  • 317
  • 552
57
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,667
  • 2
  • 22
  • 26
51
votes
2 answers

Is it possible to std::move local stack variables?

Please consider the following code: struct MyStruct { int iInteger; string strString; }; void MyFunc(vector& vecStructs) { MyStruct NewStruct = { 8, "Hello" }; vecStructs.push_back(std::move(NewStruct)); } int main() { …
Allgaeuer
  • 725
  • 1
  • 7
  • 12