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

Why should copy assignment be safe for self-assignment in C++?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of why copy assignment is safe for self-assignment in C++. Many people will encounter this dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

C.62: secure copy assignment to self-assignment Reason (reason)

If xchangx changes the value of x, people will find it strange, and at the same time, very bad errors will occur. (usually contains leaks)

Example (sample)

The standard library container handles self-assignment in an elegant and efficient way:

Std::vector v = {3,1,4,1,5,9}

V = v

/ / the value of v is still {3,1,4,1,5,9} Note (attention)

The default assignment operation that arises from a member who correctly handles self-assignment will deal with the self-assignment problem.

Struct Bar {

Vector v

Map m

String s

}

Bar b

/ /...

B = b; / / correct and efficientNote (note)

You can prevent self-assignment by explicitly checking for self-assignment, but it is usually faster and more elegant not to use the above-mentioned checking methods (such as using swap).

Class Foo {

String s

Int i

Public:

Foo& operator= (const Foo& a)

/ /...

}

Foo& Foo::operator= (const Foo& a) / / OK, but there is a cost

{

If (this = & a) return * this

S = a.s

I = a.i

Return * this

}

This seems safe and efficient. But what if there is only one self-assignment in a million assignments? There are about a million extra checks (but since the results are always the same in nature, the computer's branch predictions are right every time). Consider the following code:

Foo& Foo::operator= (const Foo& a) / / simpler, and probably much better

{

S = a.s

I = a.i

Return * this

}

Std::string is safe for self-assignment, and so is int. All the costs come from (rarely) self-assignment.

Enforcement (implementation recommendations)

The (simple) assignment operator should not contain the following check: if (this = = & a) return * this

This is the end of the content of "Why should you ensure that copy assignment is safe for self-assignment in C++?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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