Home > Backend Development > C++ > What Operations Are Permissible on Moved-From Standard Library Objects in C ?

What Operations Are Permissible on Moved-From Standard Library Objects in C ?

DDD
Release: 2024-12-25 11:50:40
Original
307 people have browsed it

What Operations Are Permissible on Moved-From Standard Library Objects in C  ?

Operations on Moved-From Objects

Despite the common misconception that moved-from objects can only be destroyed, the C standard specifies that moved-from objects of standard library types are placed in an unspecified state, allowing for a limited scope of operations. This state arises after an object is moved from, enabling operations that typically do not have preconditions.

Unconditional Operations

The following operations can generally be applied to moved-from objects without encountering preconditions:

  • Destruction: Deallocating the object's memory.
  • Assignment: Setting a moved-from object as the value of another object.
  • Constant observations: Accessing information such as getter functions, array sizes, and emptiness booleans.

Conditional Operations

In contrast, the following operations may require the object not to be in an unspecified state and hence are generally not permissible on moved-from objects:

  • Dereference: Accessing the object's internals via pointers or references.
  • Mutation: Making changes to the object's fields.

Regarding std::swap

In the example swap function template, the assignment operations (lines 2 and 3) are valid even though they involve moved-from objects because assignment generally does not have preconditions.

Finally

The standard detailing unspecified-state objects for standard library types is located at 17.6.5.15 [lib.types.movedfrom].

The above is the detailed content of What Operations Are Permissible on Moved-From Standard Library Objects in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template