Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ keep the mobile source object in a valid state

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how C++ keeps mobile source objects in an effective state". The explanation in this article is simple, clear and easy to learn and understand. let's study and learn "how C++ keeps mobile source objects in an effective state".

C.64: after the move operation is completed, the moving source object should remain in a valid state Reason (reason)

This is the semantics of the general assumption. When y=std::move (x) is executed, the value of y should become x, and x should be in a valid state.

Translator's Note

Removing the value of x is not the same thing as having an invalid state.

Example (sample) template

Class X {/ / OK: value semantics

Public:

X ()

X (X & a) noexcept; / / move X

Void modify (); / / change the value of X

/ /...

~ X () {delete [] p;}

Private:

T * p

Int sz

}

XRV (Xlux)

: p {a.p}, sz {a.sz} / / steal representation

{

A.P = nullptr; / / set to "empty"

A.sz = 0

}

Void use ()

{

X x {}

/ /...

X y = std::move (x)

X = X {}; / / OK

} / / OK: x can be destroyedNote (note)

Ideally, moving the source object should become the default value. This must be done unless there is a very good reason. However, not all types have default values, and some types have expensive code to build valid state. The standard requirement is that the object can be destroyed. In general, we can easily do better at little cost: the standard library assumes that values can be assigned to moving source objects. Ensure that the moved source object is in some (inevitably defined) valid state.

Note (Note)

Unless there is a particularly strong reason not to do so, it is important to make sure that after x=std::move (y) is executed, ybuttz can follow the usual semantics.

Enforcement (implementation recommendations)

(not executable) finds a situation in which a member in a move operation is assigned. If there is a default constructor, compare the assignment operation in the move operation with the assignment operation in the default constructor.

Thank you for your reading. the above is the content of "how C++ keeps the mobile source object in a valid state". After the study of this article, I believe you have a deeper understanding of the problem of how C++ keeps the mobile source object in a valid state, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report