0

Does the && and in the parameters mean that this is a move constructor?

Vertex(int&& val, float&& dis)
            : value_(std::move(val)), distance_(std::move(dis)),
            known_(false), previous_in_path_(nullptr)
{}

Do all move constructors have to have a parameter that is an object of the same class as the constructor is in? Like this?

Vertex(Vertex&& rhs)
            : value_(std::move(rhs.value_)), distance_(std::move(rhs.distance_)),
            known_(false), previous_in_path_(nullptr)
{}

I just need clarification as to what is and what isn't a move constructor.

3
  • 1
    A move constructor has the form class_name(class_name&&) Commented Dec 6, 2022 at 18:16
  • The answer to the headline is 'No.' That first constructor is garbage. So is the second one. Commented Dec 6, 2022 at 18:16
  • The parameter Blah&& blah means that the blah object is capable & allowed to be moved. The argument passed to the parameter has not been moved yet. That occurs with a move-constructor or move-assignment — which is not required to happen, which in that case will leave the caller's argument not-moved. Commented Dec 6, 2022 at 19:21

1 Answer 1

1

From the Move constructors documentation:

A move constructor of class T is a non-template constructor whose first parameter is T&&, const T&&, volatile T&&, or const volatile T&&, and either there are no other parameters, or the rest of the parameters all have default values.

(emphasis is mine)

This means that in order to be a move constructor, the first parameter must be a rvalue reference (potentially const/volatile) to the same type.
You can add additional parameters, but they must have default values.

Therefore your 1st constructor is not a move constructor, but the 2nd one is.

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.