Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When move constructor are called

I would like to know when move constructor are called in C++ code.

It means, I know that when I call Foo a(b) it is copy constructor, so what have I to code to call move constructor.

like image 995
jlw Avatar asked Dec 21 '14 21:12

jlw


People also ask

What is a move constructor?

A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying. For more information about move semantics, see Rvalue Reference Declarator: &&. This topic builds upon the following C++ class, MemoryBlock , which manages a memory buffer.

When move assignment is called?

A move assignment is less, not more restrictively defined than ordinary assignment; where ordinary assignment must leave two copies of data at completion, move assignment is required to leave only one.

What is a constructor when is the constructor called?

Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as constructors. Constructor does not have a return value, hence they do not have a return type.

Are move constructor automatically generated?

If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.


2 Answers

A move constructor is just another constructor. If you have overloaded constructors, just like having any other functions overloaded, the choice of which constructor is called comes down to the rules of overload resolution. That is, when you construct an object with Foo a(<some-expression>);, there might be multiple possible constructors and one needs to be chosen.

The copy constructor takes one argument of type const Foo&. This lvalue reference type will bind to any expression denoting a Foo object. The move constructor takes one argument of type Foo&&. This rvalue reference will only bind to modifiable rvalues. In fact, this overload will be preferred in the case of passing a modifiable rvalue.

This means that in Foo a(<some-expression>); if the expression <some-expression> is a modifiable rvalue, the move constructor will be chosen. Otherwise the copy constructor is chosen. Modifiable rvalues usually appear when denoting temporary objects (for example, an object returned from a function). It is also possible to force an expression to an rvalue expression by using std::move, such as Foo a(std::move(b));.

like image 68
Joseph Mansfield Avatar answered Oct 07 '22 20:10

Joseph Mansfield


In general we have to tell it by giving an rvalue reference argument. For example:

template<class T>
void swap(T& a, T& b)
{
  T tmp = std::move(a);
  a = std::move(b);
  b = std::move(tmp);
}

The move() is a standard-library function returning an rvalue reference to its argument move(x) means "give me an rvalue reference to x". That is, std::move(x) does not move anything, instead, it allows a user to move x.

Source: The C++ Programming Language (B. Stroustrup)

like image 28
Alex Avatar answered Oct 07 '22 19:10

Alex