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
1
vote
3 answers

why use a move constructor?

I'm a little confused as to why you would use/need a move constructor. If I have the following: vector fill(istream& is) { vector res; for(double x; is >> x; res.push_back(x)); return res; } void bar() { vector vec = fill(cin); …
Strahinja Ajvaz
  • 2,521
  • 5
  • 23
  • 38
1
vote
3 answers

Moving ctor and moving dtor

As I've asked in Move constructor/operator= and after a while I've agreed and accepted right answer to that question I was just thinking, if would it be useful to have something like "moving destructor" which would be invoked on moved object…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
1
vote
2 answers

"Defaulted" move constructor and assignment - weird behaviour

So I have a simple example of a class which has two member variables. And I've declared a copy and a move constructor which are = default, which would force the compiler to generate them for me. Now my question is this. When the move constructor (or…
DeiDei
  • 10,205
  • 6
  • 55
  • 80
1
vote
2 answers

Move semantics when calling a C++11 library from pre-C++11 code

Say that you create a library with a class MyClass that has proper move assignment operators and move constructors. Furthermore, this library has properly defined and implemented MyClass MyClass::operator + (const MyClass& other). This class is…
NoseKnowsAll
  • 4,593
  • 2
  • 23
  • 44
1
vote
1 answer

Move constructor doesn't get fired

Why in this class move constructor doesn't get fired? #include #include using std::cout; template class Movable { using value_type = T; using pointer_type = T*; const std::size_t init_size_{ 1 }; …
Artur
  • 181
  • 1
  • 9
1
vote
2 answers

Moving semantics in detail

In the code below, line with pm, will that line move entire memory and assign it to pm or it will only move the memory p is pointing to but not the whole array? int main() { int* p{ new int[10]{0} }; int* pm{ move(p) };//WILL…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
1
vote
2 answers

Does move constructor have to call std::move()?

I ran into some code in our project like this: class A { public: A(A&& obj): valid_(false), data_(obj.data_) {} //... void print() { for (auto x : data_) cout< data_; }; Is…
Deqing
  • 14,098
  • 15
  • 84
  • 131
1
vote
1 answer

How to implement perfect forwarding on a non-generic type?

say I have the following code: class Element; typedef shared_ptr ElementPtr; class Element { public: void add_child(const ElementPtr& elem); private: vector children; } inline void Element::add_child(const ElementPtr&…
C. Finegan
  • 83
  • 6
1
vote
0 answers

Performant way to write lambda in std accumulate when init is string.

I was implementing some code that required something like this const auto concat = std::accumulate(ints.begin(), ints.end(), string{}, [](string& acc, const int& val) { return string(std::move(acc))+to_string(val);}); 2 questions: 1) is it safe…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

Is a move constructor created by default?

I know that a copy constructor is always created by default even if I don't explicitly create one. Is the same true for a move constructor? Let's assume a have a very simple class: class SimpleClass { public: SimpleClass(int value) : …
Thomas Sparber
  • 2,827
  • 2
  • 18
  • 34
1
vote
2 answers

Difference between move operator= and copy operator=

Consider this code: #include using namespace std; class A { int x; public: A () {x=5;} A (const A & a) {x=a.x;} A (A && a) {x=move(a.x);} A & operator= (const A & a) {x=a.x;} A & operator = (A && a) {x=move(a.x);} void func () {A a;…
Tony
  • 35
  • 1
  • 4
1
vote
2 answers

Is this a correct usage of rvalue references?

Consider the following function: vector get_vector() { vector xs; // Do some stuff to fill the vector with numbers... return xs; } Would it make sense to write the following instead? The main goal would be to avoid copying the…
aochagavia
  • 5,887
  • 5
  • 34
  • 53
1
vote
1 answer

How move semantic works in vector relocation?

To my understanding, when a vector increases its capacity, it allocates a new memory, copy(move?) all contents to the new array, and then destroy the old one: vector v; v.emplace_back(1); v.emplace_back(2); cout<<&v[0]<
Deqing
  • 14,098
  • 15
  • 84
  • 131
1
vote
1 answer

A const data member prevents automatic generation of both copy and move ctors?

Consider the following class: struct A { int const x; A(int x) : x(x) { } } Will this class get automatically generated move and copy ctors? Or is this prevented by the presence of the const field member x? From my experience the answer…
a06e
  • 18,594
  • 33
  • 93
  • 169
1
vote
1 answer

Move assignable class containing vector>

The class Foo has an rvalue reference constructor that moves the contained vector of unique_ptrs so why does the following code give the following error, both with or without the std::move on the Foo() in main? error C2280:…
Edward J Brown
  • 333
  • 1
  • 5
  • 17
1 2 3
99
100