c++ - Are there any undefined behavior issues when moving data into a function and then back out to where it came from? -
consider following function:
std::vector<int> pushit(std::vector<int> v){ v.push_back(10); return v; } int main(){ std::vector<int> vec; vec = pushit(std::move(vec)); } my assumption vector moved function, modified , moved original spot. should result in simmilar behavior passing in non const reference. seems quite valid behavior colleague fears undefined behavior. there missing here?
i because current function
void currentpushit(std::vector<int>& v){ v.push_back(10); } has led lot of problems in code review because people overlook fact innocent call currentpushit(v) can invalidate iterators. making them write v=pushit(std::move(v)) should wake them enough don't make same mistakes.
per 1.9p15, value computations , side effects associated argument sequenced before execution of body of function. time enter pushit source vec has been moved from. assignment sequenced after execution of pushit, since calling user-defined operator vector::operator=:
vec.operator=( // sequenced after pushit( // evaluation of this, sequenced after std::move( // evaluation of vec))) so code fine.
Comments
Post a Comment