[Cs3] std::move
Mikhail Nesterenko
mikhail at cs.kent.edu
Thu Mar 13 22:19:05 EDT 2014
CS3 students,
this is regarding the discussion we had during Wednesday's lecture
about placing an element into its container without copying overhead.
It turns out, the proper way to do it (with C++11 standard) is to use
std::move(). For example, here is a mechanism to add another string
to a vector of strings:
string s="Hello";
vector <string> vs;
vs.push_back(move(s)); // string "s" is moved to "vs" without
// copying
Now, the standard says that the source element is left in "valid but
"unspecified" state. So it is safe to empty and reuse it. Or just
re-assign to it.
std::move is supported by MSVS 2012 and g++ compilers a the department
(with -std=c++0x option). I recoded the "roster" example to demonstrate
the use of std::move (see course website).
Thanks,
--
Mikhail
More information about the cs3
mailing list