dimanche 17 janvier 2021

Move semantic with std vector miss understanding

I try to understand the concept of move semantic and did some tests. I have the following function:

// testing the move semantic when passing 
// argument to be modified without copying

void process_copy( std::vector<int>&& aVec)
{
    std::cout << "Move semantic\n";
    aVec.push_back(42);
}

now in the main function, the following:

int main()
{
    std::vector<int> w_vec = {1,2,3,4,5};
    process_copy( std::move(w_vec));
}

I expect that the w_vec is now empty since I pass it with move cast (cast lvalue to rvalue). But the result is w_vec contains 6 elements now (42 has been added to the vector).

Something that I miss? Is std::vector is a moveable object?

Aucun commentaire:

Enregistrer un commentaire