dimanche 8 novembre 2020

Why is copy assignment called but not move assignment?

Here are my test class. VTX has both copy assignment and move assignment. VTZ has a constructor that takes VTS&& to initialize its vtx memeber.

    struct VTX
    {
    int x;
    vector<int> vts;

    VTX & operator= (const VTX & other)
    {
        x = other.x;
        vts = other.vts;
        cout << "copy assignment\n";
        return *this;
    }

    VTX & operator= (VTX && other)
    {
        x = other.x;
        vts = std::move(other.vts);
        cout << "move assignment\n";
        return *this;
    }

    };

    struct VTZ
    {
    VTX vtx;
    VTZ(VTX && vvv)
    {
        vtx = vvv;
    }

    VTZ()
    {}
    };

Test code:

    int main(VOID)
    {
        VTX vtx;
        vtx.vts.resize(10);
    
        VTZ vtz(std::move(vtx));
        return 0;
    }

I think the move assignment of VTX will be called when VTZ is constructed. But

Output:

copy assignment

I'm really confused why is copy assignment called but not move assignment?

Aucun commentaire:

Enregistrer un commentaire