This question already has an answer here:
- Why is `std::move` named `std::move`? 2 answers
I am trying to understand the c++11's newly introduced std::move()
function. In the following code snippet:
typedef struct
{
int i, j;
} TwoNumbers;
void foo(TwoNumbers&& a)
{
}
int main ()
{
TwoNumbers A{0, 1};
cout << A.i << A.j << endl;
foo(move(A));
cout << A.i << A.j << endl;
return 0;
}
how is the output coming as the following ?
ayan@ayan-Aspire-E1-571:~/Desktop$ g++ main.cpp -o main -std=c++11
ayan@ayan-Aspire-E1-571:~/Desktop$ ./main
01
01
What I thought:
I thought that the std::move()
will convert my object A
to an rvalue and transfer the ownership to the function foo()
and I will no longer get the object A
in its initialised state. But that's not the case here. WHY?
Aucun commentaire:
Enregistrer un commentaire