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
2 answers

Move constructor implementation in linked list

I have the following List class that's supposed to hold a linked list. I'm experimenting and trying to learn more about move semantics. I was wondering if I have implemented the move constructor and assignment operator properly. What I want to…
SegFaulter
  • 61
  • 5
0
votes
4 answers

Shrink a vector from N1 down to N2 items without triggering the default constructor and using only move semantics

Could you help me with an efficient way to shrink an std::vector from N1 down to N2 (N2 <= N1) items in it without requiring T to have a default constructor (i.e. resize() is not an option because it requires a default constructor because it can…
Serge Rogatch
  • 11,119
  • 4
  • 58
  • 117
0
votes
0 answers

How to copy an r-value but not an l-value

I would like to create a class that keeps a reference to a container and lets the user loop on it in special ways. Here is a stripped-down example: template class DummyIterable1 { const Container& container; public: …
Erel Segal-Halevi
  • 26,318
  • 26
  • 92
  • 153
0
votes
1 answer

How does the compiler decide between const reference and rvalue reference?

This C++ code: void f(int& i) { cout << "by reference" << endl; } void f(const int& i) { cout << "by const reference" << endl; } void f(int&& i) { cout << "by rvalue reference" << endl; } int main() { int x; const int y =…
Erel Segal-Halevi
  • 26,318
  • 26
  • 92
  • 153
0
votes
1 answer

Is it possible use an rvalue to initialize a data member?

I'm very new to r-value references and move semantics but the tutorials I've read have the attitude "since the temporary is a perfectly well constructed object that's usually copied and destroyed, why not extend its lifetime and move it around?" So…
0
votes
1 answer

Why is movement constructor invoked twice while std::emplace_back is called only once?

I understand that std::emplace_back uses placement-new to construct the element in-place at the location provided by the container. Why is movement constructor invoked twice while std::emplace_back is called only once? Here is the related…
John
  • 1,029
  • 2
  • 14
0
votes
2 answers

Are these two expresions all the same:"CTest cTest(t);" "CTest cTest=t;" in C++? Only different in efficiency?

As the subject, the related code is listed below.You could check it on https://godbolt.org/z/bcf8js. There is no doubt that EntityId_t c_SEDSubscribe(ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER); calls the user defined constructor EntityId_t(int id)…
John
  • 1,029
  • 2
  • 14
0
votes
0 answers

Why does the copy constructor and copy assignment operator call `memcpy` whereas the movement ctor and movement assignment operator call `memmove`

As the subject, the related code is listed below.You could check it on https://godbolt.org/z/mAbmwJ. I completely understand the differences between memcpy(3) and memmove(3), but I don't understand the reasons lie behind it.The code is quoted from…
John
  • 1,029
  • 2
  • 14
0
votes
0 answers

I cannot see my user-defined copy constructor called when I return the class by value

I have the following class in C++ class.h typedef std::complex fcomp; class wf { public: int nx, ny, nf; //dimensions size_t wfSize; fcomp * val; //data //constructors wf(int nx, int ny, int…
0
votes
0 answers

Should I use const reference, or std::move

I am wondering what performance/code quality bearing differences come from the use of move semantics, or passing something by a const reference, and copying it into a field member. (For the use of initializing class members.) Here's an…
user10047010
0
votes
0 answers

Find a unique_ptr to an inherited class object emplaced_back in a vector

I am trying to implement an Entity Component System (ECS) for my game. I have a base class "Component" (referred here as A) which is inherited by child class like HealthComponent(referred here as B), DamageComponent (referred here C) #include…
0
votes
1 answer

Implicit move constructor

What is exactly the implicit move constructor doing? For example how would the implicit move constructor look like for the following class (could you provide some example implementation of this implicit constructor): struct A { A() =…
michalt38
  • 1,047
  • 6
  • 13
0
votes
1 answer

How to return a Vec of structs that have a String type field from a function?

I'm working on a lexer that has the function lex that should move a vector of scanned tokens to a main program that would then generate a parser to parse the tokens, defined as follows: /// ### lex /// Pushes the tokens generated by ///…
SeSodesa
  • 1,155
  • 8
  • 17
0
votes
1 answer

std::move on boost::asio::ip::tcp::socket

I'm reading the book Functional programming in C++ and the source code of Chapter 12. https://gitlab.com/manning-fpcpp-book/code-examples/-/blob/master/chapter-12/bookmark-service/service.cpp#L19 In this line, std::move is applied on m_socket. After…
chaosink
  • 1,110
  • 10
  • 23
0
votes
0 answers

std::move is passing by reference instead of calling move constructor

I'm trying to wrap my head around move semantics in c++. I created a small class to help me understand what is going on, I deleted the copy and move assignment operators to keep things simple. using namespace std; struct Bar { Bar(std::string…
FRR
  • 10,772
  • 13
  • 43
  • 80
1 2 3
99
100