EnDelt64
EnDelt64

Reputation: 1360

Clang-Tidy: Move constructor initializes class member by calling a copy constructor

class Vtx {
private:
    int indexPoint;
    int radius;
    std::string strName;

public:
    Vtx(const int p_indexPoint, const int p_radius, std::string p_strName)
            : indexPoint(p_indexPoint),
              radius(p_radius),
              strName(std::move(p_strName)) {
        std::cout << "Constructor\n";
    }

    Vtx(const Vtx& rhs)
            : indexPoint(rhs.indexPoint),
              radius(rhs.radius),
              strName(rhs.strName) {
        std::cout << "Copy constructor\n";
    }

    // (#)
    Vtx(const Vtx&& rhs)
            : indexPoint(rhs.indexPoint),
              radius(rhs.radius),
              strName(rhs.strName) {
        std::cout << "Move constructor\n";
    }

    ~Vtx() {
        std::cout << "Destructor\n";
    }
};

For this code, clang-tidy gives me following warning message.

I can't understand when the copy constructor is called inside of move constructor.(#) Although compilation works well, but I want to remove this warning. How to I fix it?

Upvotes: 0

Views: 871

Answers (1)

Jarod42
Jarod42

Reputation: 218138

You forget to move the members (can be omitted for built-in type where move is copy anyway)

and also moving from const object is generally a copy so you have to change to

Vtx(Vtx&& rhs)
            : indexPoint(rhs.indexPoint),
              radius(rhs.radius),
              strName(std::move(rhs.strName)) {
        std::cout << "Move constructor\n";
    }

Note that, in your case, except for message (you can move that code in dedicated class to ease Vtx implementation BTW), default implementation would be ok:

Vtx(Vtx&& rhs) = default;

Upvotes: 5

Related Questions