Home >Backend Development >C++ >How Do I Handle Moved-From Objects in C 11?
Handling Moved-From Objects in C 11
Understanding the concept of "moved from" objects in C 11 can be challenging. When an object is moved from, it enters an unspecified state. However, the type definition should specify what operations are valid on moved-from objects.
For standard library types, moved-from objects are placed in a valid but unspecified state. This means that the preconditions for any operation should be checked before using it. For instance, a moved-from string can't be popped back until the empty condition is verified.
In custom types, the developer is responsible for defining the state of moved-from objects. For example, a pimpl object may specify that moving from it invalidates the do_stuff operation.
The language design prevents unexpected moved-from objects. Moving occurs either when no further actions are possible on the moved-from object or when the user explicitly requests a move operation.
It's important to note that moved-from objects don't qualify for standard library concepts. If a type doesn't remain in a valid state (as defined by the concept), it can't be used with the standard library.
The above is the detailed content of How Do I Handle Moved-From Objects in C 11?. For more information, please follow other related articles on the PHP Chinese website!