Add element at the end Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element.
Hi, the above is from http://www.cplusplus.com/reference/vector/vector/push_back/
I am a little bit confused by the word "move".I understand the rvalue reference and lvalue reference.
for example,
vector<int> source = {1,2,3};
vector<int> copy;
copy.push_back(source[0]);
cout<<source[0]; // why it doesn't throw error since source[0] is moved to copy ?
because source[0] is rvalue, so push_back actually moves source[0] rather than copy it. Then after this operator, access to source[0] is still valid? why?
source[0]
is not an rvalue. It is an lvalue. Even if it was, you are assuming that accessing a moved-from object "throws an error". The C++ standard doesn't require that, in any shape, matter, or form. It might throw an error, for some arbitrary class, but it may not. The C++ standard does not require either one or the other. – Sam Varshavchikint addInt(int i,inj);
. But someone told me below that the return value type is reference, then source[0] is lvalue type, which makes sense to me. Otherwise, it will be rvalue type. – Lin Paul