1

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)) {}
5
  • 1
    Please update your question with your best guess of the implementations you are after. Commented May 20, 2020 at 15:08
  • stackoverflow.com/questions/61914086/… Commented May 20, 2020 at 15:10
  • 1
    Your A class doesn't define constructors. I assume those Bs should be A? Commented May 20, 2020 at 15:20
  • Yes, of course, my mistake. I've changes this Commented May 20, 2020 at 15:21
  • Your guess is almost correct. (there is the except part which is missing). Commented May 20, 2020 at 15:33

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

10 Comments

In other words, given the data member types, just a copy. ;)
also, here's the standard. eel.is/c++draft/class.copy.ctor#14
Could you write in your answer how the implementation of the implicit move constructor should look like then?
@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?
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.