Herb Sutter Dialogue # 30: It's an Object-Ful Lifetime

zhaozj2021-02-16  33

Dialogue # 30: It's an object-fullifetime (wq translation)

That is a few days before the holiday. A rare, there is no compression of the deadline - I have done it on time.

I often hang around in the source code library as a pastime. When studying other programmers, I often learn new techniques - and the skills that should be avoided. I accidentally discovered an interesting thing, it was concentrated in the applet:

Class T

{

PUBLIC:

T & REF () {Return * this;}

}

Void f (t &);

int main ()

{

f (t (). REF ());

}

At first, I didn't understand the intent of the ref () function, so I removed the call to it - I expected it to work:

int main ()

{

f (t ());

}

However, when I compile it, the compiler prompts an error that binds a non-Const based reference on the temporary object. I took a head - this is of course not allowed! I recall GURU's explanation, just when I first encountered this problem.

"One reason for this ban is to avoid some subtle bugs," She once said. "My child, consider the following:"

Class U

{

// ... whatver ...

}

Void TakesAndModifieSu (U)

{

// performs actions what modify the state of u

}

Class V

{

PUBLIC:

Operator u ();

}

Void g ()

{

V V;

// ...

TakesAndModifieSu; (V)

// ...

}

"If the bind is allowed, the compiler calls the type conversion operation, constructs a temporary U object. Then this unnamed temporary object will be passed to the TakesAndModifiesu and modified, and then dropped after the function call is completed. The original object V There is no passive at all - this will give the person to write this function to greatly trouble. "

However, I was still plagued. I don't understand the initial f (t (). Ref ()); how can statements can be compiled - it is still binding a non-Const from a temporary object.

"You can't limit it in the invariance, my child," Guru voice shocked me - from the real world, not in memory. " "Think of Lvalue and RValue. Sacred standard tells us that a explicit type conversion in T () forms generates a rvalue. RValue can only be bound to a CONST reference, while Lvalue does not have this limit. On the other hand, return The result of the reference is to get a Lvalue [1]. Therefore, the compiler can bind non-Const's reference to the result of REF (). "

"So, as long as I call a function that returns a reference, I have no problem," I replied. "Hey - assignment operator returns a reference, so I can write f (t () = t ()) ;." "I am coming." "I can think of this skill has a lot of use."

"Be careful, my child. So unusual skill is dangerous, should not be used easy to use. Indeed, I think there is at least one problem, regarding the object life, will lead to undefined behavior."

"That is ..." I urged.

"First think about it, after lunch," GURU slowly represented. In the distance, I saw some colleagues have prepared a holiday dinner to eat sectors. I grabbed the coat and joined them and went to a nearby hotel.

I don't know why, we actually try to avoid discussing shopping at eating. Discuss in our favorite holiday movie, Comparing The Various Remakes of Miracle On 34th Street, and WHether Alistair Sim or Patrick Stewart Made A Better Scrooge. (My Vote Was for Stewart.) Bob Surprised Me, Though - I Figure WAXED POTIC ON IT'S A WONDERFUL LIFE. (WQ Note: Sorry, "Due to the technical independent of technology, Not good, so I didn't translate.) When we finished eating, I feel very good. I sat down on the table and began to think about what I'm referred to in Pensive Guru. Finally, when I tried to keep my eyes opened, I understood:

Class U

{

T & T_;

PUBLIC:

U (T & T): T_ (t) {}

}

{/ ... Some Block Scope ...

U u ((t () = t ()));

// ...

}

Once the u object completes the construction, the life of the temporary object is over, and the u object will be left with a suspension reference.

Just as the T object has never existed, I am in meditation.

"Okay, George" I heard the voice of Guru, "You have got your wish. You have never born."

"Well? Who is George?" I turned to GURU.

"Of course you," Guru replied. I saw her, I realized that I am doing a dream - that kind of you know that it is a dream, but I'm going to follow your dream. "You are George Bailey. You constructed objects have never existed. My job is to explain what happens when it is involved in undefined behavior."

In the distance, I saw an item: "Welcome to Bobville."

"Bobville?" I asked, fear this name.

"The town is controlled by your death bob," Guru said. "He is also the owner of the bank, and the head of the software development department. Let us walk along the main street."

When we walked forward, I saw a horrible throwing - there was no separated whole code, named I and J variables, and "NULL pointer anti-use for this" commercial billboard. On a street corner, I saw some programmers who didn't have a copy-paste code.

I saw Kerry driving is driving a taxi. I showed that he stopped.

"Take me away, Kerry," I shouted when I drilling into the carriage. "Bring me back to the office, the place we write rational code".

"Kerry? My name is Ernie (a lottery machine). I don't know how to write code; I just opened a taxi." We wore it from Wendy, she worched. Kerry waved her hand to her. Know that it is a dream, and I am not set to pay attention to his actions, I ignore him.

We stopped in front of an abandoned office building. I jumped out of the car. I found a whiteboard, it has some uncomfortable code above, so I rewrote it:

{/ ... Some Block Scope ...

T TMP;

U u u (tmp);

}

I was awakened when I wrote a pen.

"Wow!" I spend my words, and I went to the restaurant to take the most concentrated coffee I found. Note:

[1] ISO / IEC 14882: 1998 (E), "International Standard, Programming Languages ​​- C ", Clauses 5.2.3, 8.5.3, and 3.10 Respectively.

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

New Post(0)