GURU Of THE WEEK Terms 02: Temporary object

zhaozj2021-02-08  530

Gotw # 02 Temporary Objects

Author: Herb Sutter

Translation: Kingofark

[Declaration]: This article takes the Guru of The Week column on www.gotw.ca website, and its copyright belongs to the original person. Translator Kingofark translated this article without the consent of the original person. This translation is only for self-study and reference, please read this article, do not reprint, spread this translation; people download this translation content, please delete its backup immediately after reading. Translator Kingofark is not responsible for people who violate the above two principles. This declaration.

Revision 1.0

GURU Of THE WEEK Terms 02: Temporary object

Difficulty: 5/10

(For sinners who have made your heart (including your program performance) as garbage throws out of the window, often is an unexpected temporary object.)

[problem]

Imagine that you are reading another programmer write a good function code (below), but this function is used in at least three places to use unnecessary temporary objects. So, can you find a few of them? How do the programmer modify the code?

String Findaddr (List L, String Name)

{

For (List :: item i = l.begin ();

I! = l.end ();

i )

{

IF (* i == name)

{

Return (* i) .addr;

}

}

""; "

}

[answer]

Letter do not believe in you, this short a few lines of code, there is three places to obviously use unnecessary temporary objects, two of which are subtle, and the third is a red herring.

* String Findaddr (List L, String Name)

---- 1st place ---- ---- Level 2 ----

1 and 2: Two parameters should use constant references. Using the pass-by-value method will cause functions to copy List and String, with high performance costs.

[Rules]: Please use const & instead of the pass value copy.

* For (list :: item i = L.begin ();

I! = l.end ();

i )

- Articles 3 -

3: This is really subtle. PREINCREMENT operation is higher than the increasing operation efficiency, because the object not only increases itself, but also returns a temporary object that contains the value before increment. To know, even the built-in type of INT is true!

[Learning Guidance]: Please use the preincrement to avoid the postIncreent operation.

* If (* i == name)

- 4th place -

4: This is not embodied here, but if you want it to travel, it is either to convert to String, either to get a string through a conversion constructor (constructor). However, both methods produce temporary objects, resulting in the call to String or EMPLOYEE's Operator =. () [Learning Guidance]: Take careful hidden temporary objects due to parameter conversion operations. A good way to avoid it is to explicitly use the use constructor (constructor) as possible.

* Return "";

- Articles 5 -

5: An temporary (empty) String object is generated here. Better approach is to declare a local string object to store the return value, then return this String with a separate returnite statement. This allows the compiler to enable the "return value optimization" processing in some cases (such as "String a = FindAddr (L," Harold ") to enable" Return Value Optimization "processing to omit the local object.

[Rule]: Please follow the so-called "Single-Entry / Single-Exit rule. Never write multiple Return statements in a function.

[Author ": When further performance testing, I no longer agree with the suggestion above. I have modified this in "Exceptional C ". ]

* String Findaddr (List L, String Name)

- 第 *

*: This is a crowded method. Look, it seems that you can quickly create temporary objects in all possible return issues by declaring the return type as String & instead of String. right? wrong! If your program just crashes when the code tries to use reference (Reference) (because the local object points to the local object have not yet existed), then you are lucky enough! If you don't walk, your code will seem to work normally, but the cold does not have a few times a few times, so that you have to spend another long night in the process of debugging procedures.

[Rules]: Absolute absolute (!) Do not return to the reference to the local object (Reference).

[Author: There are some posts correctly point out that you can declare a static object returned when an error is encountered, so that it is realized to return a reference (Reference) without changing the function semantic. At the same time, this also means that when returning reference (Reference), you must pay attention to the living cycle of the object. ]

In fact, there are many places that can be optimized, such as "avoid excessive calls for End ()" and so on. Programmers can use a const_iterator (should also). Throwing these don't talk, we can still get the following correct code:

String Findaddr (Const List & l, Const String & Name)

{

String addr;

For (list :: const_iterator i = l.begin ();

I! = l.end ();

i) {

IF ((* i) .Name == name)

{

AddR = (* i) .addr;

Break;

}

}

Return addr;

}

[Kingofark Note:

Red Herring: Something That Draws Attention Away from the center issue.

a red merring: eye-catching method; transfer attention

转载请注明原文地址:https://www.9cbs.com/read-348.html

New Post(0)