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
0
votes
1 answer

Tour of C++ copy elision in Section 4.2.3

I'm reading Section 4.2.3 Initializing Containers, Tour of C++ Second Edition. It says: Vector read(istream& is) { Vector v; for (double d; is>>d;) v.push_back(d); return v; } ... The way to provide Vector with a move…
Jay Lee
  • 1,065
  • 1
  • 9
  • 20
0
votes
2 answers

should I call void constructor to reset object in move semantic?

As I know, the purpose of void constructor is to reset all the element from choas state to proper new-born state. and this statment is quite accruate for me to do it after steal data from rvalue referance. but it seems to be a invild use for direct…
AiDSl
  • 25
  • 6
0
votes
1 answer

What is the proper way to move a vector (or anything else) from outside to a class member?

struct Something { ... }; class MyClass { public: MyClass(std::vector&& vec) { myVec = vec; } private: std::vector myVec; }; void main() { ... MyClass…
M. P.
  • 73
  • 5
0
votes
1 answer

Why does copy assignment cause GCC to give a -Wvirtual-move-assign warning?

The following class hierarchy will cause a [-Wvirtual-move-assign] warning in GCC if a move assign is invoked for an instance of B. struct A { std::vector v; }; struct B : public virtual A {}; The following scenarios indeed cause the…
Adam Sperry
  • 143
  • 8
0
votes
1 answer

Is it bad practice to reference class member object from within another member of the same class?

I'm rather new to C++ (with Java background) and I'm trying to understand the principles and practices for class designing. From what I've already read it seems to me that one should prefer having class members as objects to having them as pointers…
0
votes
0 answers

Use std::move on named return value to guarantee no copying?

UPDATE: To be even more explicit, and avoid misunderstandings: What I am asking is, in case of returning a named value, does the C++17 standard GUARANTEE that the move constructor will be invoked if I do std::move on the return value?. I understand…
Erik Alapää
  • 2,335
  • 10
  • 23
0
votes
1 answer

Move sematics to move data from one vector to another

If I have a vector std::vector oldData, can I use move semantics to move the data into another vector std::vector newData. Instead of doing: std::vector newData(oldData.begin(),oldData.end()); Could I do…
0
votes
2 answers

How I can implement move constructor and operator for unique_ptr as a private member of class

I am trying to write Matrix class. I am using unique_ptr as a storage pointer array. But this unique_ptr is private member of class. I want to add move constructor to my Matrix class. But I need getter function for private unique_ptr but when I…
0
votes
1 answer

Mechanism of Move Semantic

I've started to learn C++ but I have some doubts about the move semantics. When the book says that the move constructor of A "steals" the attributes of an rvalue B, is it means that the pointer to a value V switch from B to A? So in this way, A…
Lorenzoi
  • 21
  • 7
0
votes
1 answer

Why behavior changes as order of a emplace_back / push_back changes ? Why number of Constructor increases?

class Buffer { unsigned char* ptr{nullptr}; size_t length{0}; public: Buffer() : ptr(nullptr), length(0) {std::cout<<"\n In Buffer()\n";} explicit Buffer(size_t const size): ptr(new unsigned char[size] {0}), …
0
votes
2 answers

C++ - What is wrong with my move/copy constructors and move/copy assign operators?

So I have my own matrix class and I have tried to overload the + and += operators for the class like this: template void Matrix::operator+=(Matrix& mat) { if (this->m_rows != mat.m_rows || this->m_cols != mat.m_cols) { cerr <<…
0
votes
2 answers

c++ copy (reference) constructor and move constructor of class cohabitation

Here code showing the intent: template class B { public: // this should indeed set t_ as a reference to t B(T& t):t_(t){} // this should instead set t_ as a copy of t B(T&& t):t_(t){} T& t_; // maybe type should be something…
Vince
  • 3,251
  • 8
  • 28
  • 57
0
votes
0 answers

When to std::move from *this in ref qualified method?

I am playing around with immutable objects after reading the excellent Functional Programming in C++ book. Assume I have a class for an immutable vector like this: template class immutable_vector { public: immutable_vector()…
geo
  • 361
  • 1
  • 9
0
votes
2 answers

Using std::move() vs. assign in constructor member initializer list

What is the advantage of using std::move() in parametrized constructor (with initializer list) instead of regular member initialization, i.e., assign? e.g., #include #include template class myVec{ private: …
0
votes
1 answer

Should I implement rvalue push function and should I use std::move?

As an exercise, I'm implementing a stack class in C++11. This is the relevant code: template class stack { private: T * elements; std::size_t capacity; std::size_t count; public: explicit stack(std::size_t capacity) :…
HBv6
  • 3,389
  • 4
  • 26
  • 41
1 2 3
99
100