mardi 31 janvier 2017

Warn about calling copy ctor when std::move'ing

Given a program:

#include <iostream>
#include <vector>

using namespace std;

struct Vec {
    Vec() = default;

    Vec(const Vec& vec) 
        : v_(vec.v_)
    {
        cout << "copy Vec" << endl;
    }

    Vec(Vec&& vec)
        : v_(std::move(vec.v_))
    {
        cout << "move Vec" << endl;
    }

    vector<double> v_{};
};

struct T1 {
    Vec Value;
};

struct T2 {
    T2(const T1& t1) 
        : Value(std::move(t1.Value))
    {

    }

    Vec Value;
};

struct T3 {
    T3(T1&& t1)
        : Value(std::move(t1.Value))
    {

    }

    Vec Value;
};

int main() {
    T1 t1;

    T2 t2{ t1 };

    T3 t3{ std::move(t1) };

    return 0;
}

Output:

copy Vec
move Vec

In both cases I std::move'ing t1.Value to Vec ctor.

Is there any possibility to enable some warning in Clang (or another compiler) so it warns me that moving actually calls copy constructor?

(Clang already can warn about performance issues when, for example, I return a std::unique_ptr by std::move(result) from function.)

Aucun commentaire:

Enregistrer un commentaire