mardi 17 septembre 2019

std::move Not Working on RValue Reference Function

While trying to learn std::move and rvalue reference , i just came across the following:

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> vecNumbers;
    vecNumbers.push_back(10);
    vecNumbers.push_back(20);

    foo(std::move(vecNumbers));

    std::cout<<"After Move \n";
    std::cout<<"size:"<<vecNumbers.size()<<"\n";

    return 0;
}

void foo(  std::vector<int> &&value)
{
    std::cout<<"size in Function:"<<value.size()<<"\n";
}

The Output

size in Function:2
After Move
size:2

I was expecting the size to be 0 after calling move on vector but here it only moved as reference. Could someone please explain what is happening here.

Aucun commentaire:

Enregistrer un commentaire