patterncppCritical
What is std::move(), and when should it be used?
Viewed 0 times
moveandshouldusedstdwhenwhat
Problem
- What is it?
- What does it do?
- When should it be used?
Good links are appreciated.
Solution
Wikipedia Page on C++11 R-value references and move constructors
(And in addition to copy assignment operators, they have move assignment operators.)
It's a new C++ way to avoid copies. For example, using a move constructor, a
Try googling for move semantics, rvalue, perfect forwarding.
- In C++11, in addition to copy constructors, objects can have move constructors.
(And in addition to copy assignment operators, they have move assignment operators.)
- The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" (
Type &&).
std::move()is a cast that produces an rvalue-reference to an object, to enable moving from it.
It's a new C++ way to avoid copies. For example, using a move constructor, a
std::vector could just copy its internal pointer to data to the new object, leaving the moved object in an moved from state, therefore not copying all the data. This would be C++-valid.Try googling for move semantics, rvalue, perfect forwarding.
Context
Stack Overflow Q#3413470, score: 439
Revisions (0)
No revisions yet.