Forcing a Move
Move constructors and move assignment operators work with rvalues. What if you want to use them with lvalues? For instance, a program could analyze an array of some sort of candidate objects, select one object for further use, and discard the array. It would be convenient if you could use a move constructor or a move assignment operator to preserve the selected object. However, suppose you tried the following:
Useless choices[10];Useless best;int pick;... // select one object, set pick to indexbest = choices[pick];
The choices[pick] object is an lvalue, so the assignment statement will use the copy assignment operator, not the move assignment operator. But if you could make choices[pick] look like an rvalue, then the move assignment ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access