Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions to the order of destruction for temporary objects?

Tags:

c++

c++11

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.

like image 590
Bo Persson Avatar asked Mar 21 '11 23:03

Bo Persson


2 Answers

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.

like image 172
Mark B Avatar answered Nov 02 '22 17:11

Mark B


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).

like image 3
Jerry Coffin Avatar answered Nov 02 '22 16:11

Jerry Coffin