1

I have following code:

struct Base
{
    std::vector<int> a;
}

struct Derived : public Base
{
    Derived(Base && rhs):
        Base( std::forward<Base>(rhs))
    {
    }
    //some more fields
}
//...
Base a;
Derived b(std::move(a));

Does calling of Derived constructor will lead calling of move constructor of std::vector that contained in Base class?

2
  • Yes, construction process of Base part of Derived object will invoke implicitly defined move c'tor of Base which will invoke move constructors of all the data members of Base. Commented Nov 1, 2017 at 6:30
  • You should use std::move here. std::forward is for forwarding arguments passed to templated functions. Commented Nov 1, 2017 at 10:17

1 Answer 1

3

Yes, the implicitly-defined move constructor of Base is called, it will perform move on its data member a.

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.

LIVE for confirming

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

Comments

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.