且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么未命名的C ++对象在范围块结束之前被销毁?

更新时间:2022-11-16 23:00:12

这是在12.2 / 3:

This is in 12.2/3:


临时对象作为评估(词法)包含创建点的完整表达式(1.9)的最后一步被销毁。

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created.

您的行为得到保证。

有两个条件,如果满足,将延长临时生命周期。第一个是当它是一个对象的初始化时。第二个是当引用绑定到临时。

There are two conditions that, if met, will extend the lifetime of a temporary. The first is when it's an initializer for an object. The second is when a reference binds to a temporary.