0

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()           = default;
    A(A && other) = default;
    int a;
};

struct B : public A
{
    int b;
    int * c;
};

Would the implementation look like this:

B(B && other) : A(std::move(other)), b(std::move(other.b)), c(std::move(other.c)) {}
michalt38
  • 1,047
  • 6
  • 13

1 Answers1

2

From cppreference.com:

For union types, the implicitly-defined move constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument. If this satisfies the requirements of a constexpr constructor, the generated move constructor is constexpr.

The base class constructors runs before the derived one.

Eric
  • 815
  • 2
  • 11
  • 1
    In other words, given the data member types, just a copy. ;) – Asteroids With Wings May 20 '20 at 15:21
  • also, here's the standard. http://eel.is/c++draft/class.copy.ctor#14 – John Ding May 20 '20 at 15:23
  • Could you write in your answer how the implementation of the implicit move constructor should look like then? – michalt38 May 20 '20 at 15:25
  • 1
    @michalt38: That's not really how the language works. As implicitly-defined constructor is generated directly by the compiler, not by writing "the" explicit implementation and then compiling it. Maybe you mean to ask for an example of *an* explicit move constructor that *would behave equivalently* to the implicitly-defined one? – ruakh May 20 '20 at 15:33
  • @michalt38 the c++ standard just defines the behaviour of the default move constructor. The compiler won't write down a move constructor in normal c++ syntax as you would and then compile that. All i could do is write down a move constructor for you that behaves the same as the default one. But what's the point of that? – Eric May 20 '20 at 15:36
  • @michalt38 Also note that there are multiple ways how to write such an equally-effective hand-written move constructor, since for fundamental types, move is just the same as copy. – Daniel Langr May 20 '20 at 15:38
  • I just wanted this for a better understanding of what the implicit move constructor is doing what what the member-wise move means – michalt38 May 20 '20 at 15:38
  • So the implicit move will do the "shallow move" so the members of the temporary object will just not be assigned to their default values? – michalt38 May 20 '20 at 16:10
  • @michalt38 you can think of member wise move as calling std::move on all members like you did. The common misconception about std::move is that it actually doesn‘t move anything. All std::move does is a cast to an rvalue. So for basic types like integers or pointers moving is the same as copying. – Eric May 20 '20 at 16:45
  • @michalt38 You can also study the generated assembly. Basically it's not possible for implicitly-defined constructors (which are inline), but for defaulted ones, which should effectively do the same. See, e.g., this [live demo](https://godbolt.org/z/nLc7wJ). Note that both copy/move constructors for both classes consist of exactly the same instructions. – Daniel Langr May 20 '20 at 17:02