Is a constructor a move constructor if the parameter is not an object of the class?

145 Views Asked by At

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.

1

There are 1 best solutions below

0
wohlstad On

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.