简体   繁体   English

临时对象销毁顺序的例外情况?

[英]Exceptions to the order of destruction for temporary objects?

Reading clause 1.9/14 of the C++0x draft. 阅读C ++ 0x草案的第1.9 / 14条。 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) 在与要评估的下一个全表达式相关联的每个值计算和副作用之前,对与全表达式相关联的每个值计算和副作用进行排序.8)

And the footnote 8 says 脚注8说

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. 8)如12.2中所述,在评估完整表达式之后,临时对象的析构函数的零次或多次调用的序列发生, 通常以每个临时对象的构造的相反顺序发生。

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). 除了Mark B的答案(这是非常好的)还有另外一种情况:如果你创建一个通过前向迭代器访问的类型的临时对象,那么对象(显然就足够了)按照迭代器支持的顺序创建,并且在相同的顺序(不是相反的 - 因为迭代器不支持)。 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). 我不记得是肯定的,但我似乎记得同样的事情发生(或至少是被允许),即使存在问题的容器支持反向迭代(所以有问题的代码可以忽略容器/迭代器类型,大概)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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