1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
Classes may very well allow move semantics without offering copy
semantics. Most stream classes belong to this category. Extending their
definition with move semantics greatly enhances their usability. Once move
semantics becomes available for such classes, so called
em(factory functions)hi(factory function)
(functions returning an object constructed by the function) can easily be
implemented. E.g.,
verb( // assume char *filename
ifstream inStream(openIstream(filename));)
For this example to work an tt(ifstream) constructor must offer a move
constructor. This ensures that only one object refers to the open tt(istream).
Once classes offer move semantics their objects can also safely be stored
in standard containers (cf. chapter ref(CONTAINERS)). When such containers
perform reallocations (e.g., when their sizes are enlarged) they use the
object's move constructors rather than their copy constructors. As move-only
classes suppress copy semantics containers storing objects of move-only
classes implement the correct behavior in that it is impossible to assign such
containers to each other.
|