Patrycjerz
Patrycjerz

Reputation: 23

Move concept in the C++ standard

I'm curious where description of moving objects is in the C++ ISO document. I've seen only informations about rvalue reference, move constructor, move assignment operator and their syntax. I couldn't find a official reason why "move" is described as moving resource between objects.

Upvotes: 2

Views: 286

Answers (1)

R Sahu
R Sahu

Reputation: 206567

From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm:

Move semantics is mostly about performance optimization: the ability to move an expensive object from one address in memory to another, while pilfering resources of the source in order to construct the target with minimum expense.

Move semantics already exists in the current language and library to a certain extent:

  • copy constructor elision in some contexts
  • auto_ptr "copy"
  • list::splice
  • swap on containers

All of these operations involve transferring resources from one object (location) to another (at least conceptually). What is lacking is uniform syntax and semantics to enable generic code to move arbitrary objects (just as generic code today can copy arbitrary objects). There are several places in the standard library that would greatly benefit from the ability to move objects instead of copy them (to be discussed in depth below).

Upvotes: 5

Related Questions