简体   繁体   中英

Exceptions to the order of destruction for temporary objects?

Reading clause 1.9/14 of the C++0x draft. There I find:

Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.8)

And the footnote 8 says

8) As specified in 12.2, after a full-expression is evaluated, a sequence of zero or more invocations of destructor functions for temporary objects takes place, usually in reverse order of the construction of each temporary object.

What does it mean by "usually"? I thought the reverse order of destruction was the rule.

I assume they're referring to any temporaries bound to references. The lifetime of the temporary is extended to the lifetime of the reference, while other temporaries may still be destroyed.

Along with Mark B's answer (which is quite good) there's one other situation: if you create a temporary object of a type that's accessed via a forward iterator, the objects (obviously enough) created in the order supported by the iterator and also destroyed in the same order (not the reverse -- because the iterator doesn't support that). I don't remember for sure, but I seem to recall the same happening (or at least being allowed) even if the container in question would support reverse iteration (so the code in question can ignore the container/iterator type, presumably).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM