如何理解"Temporary objs are destroyed as the last step in evaluating the full-expression"?谁能举个简单的例子说清楚?
How to comprehend "Temporary objs are destroyed as the last step in evaluating the full-expression"?Could anyone make it clear by some simple example?
根据文档(),它说:
When an implementation introduces a temporary object of a class that
has a non-trivial constructor ([class.default.ctor],
[class.copy.ctor]), it shall ensure that a constructor is called for
the temporary object. Similarly, the destructor shall be called for a
temporary with a non-trivial destructor ([class.dtor]). Temporary
objects are destroyed as the last step in evaluating the
full-expression ([intro.execution]) that (lexically) contains the
point where they were created. This is true even if that evaluation
ends in throwing an exception. The value computations and side effects
of destroying a temporary object are associated only with the
full-expression, not with any specific subexpression.
如何理解"Temporary objects are destroyed as the last step in evaluating the full-expression ([intro.execution]) that (lexically) contains the point where they were created."?谁能举个简单的例子说清楚?
简单的例子。此表达式生成一个临时对象:
std::string("test")
此处,该表达式用作子表达式:
function(std::string("test"));
// point A
在 A 点,临时对象已被销毁,因为该点位于创建临时对象的完整表达式之后。
这里有一个例子,说明如果不理解这条规则,如何编写错误:
const std::string& function(const std::string& arg) {
return arg;
}
const std::string& ref = function("test");
std::cout << ref;
在这里,作为参数创建的临时对象在完整表达式后被销毁,因此 ref
变得无效 - 悬空引用。将无效引用插入输出流时,行为未定义。
一种在很多情况下都适用的解释是,当执行到语句末尾的分号时,临时对象被销毁。某些语言结构(例如 for
循环)未包含在本说明中,因此请不要勉强解释。有关异常的更好解释,请参阅 .
举个例子:
i = foo1(foo2(std::string("test")));
临时字符串在赋值之后一直保持活动状态,因为赋值发生在语句结束之前。 (在这种情况下,完整的表达式就是语句。)
根据文档(),它说:
When an implementation introduces a temporary object of a class that has a non-trivial constructor ([class.default.ctor], [class.copy.ctor]), it shall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called for a temporary with a non-trivial destructor ([class.dtor]). Temporary objects are destroyed as the last step in evaluating the full-expression ([intro.execution]) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception. The value computations and side effects of destroying a temporary object are associated only with the full-expression, not with any specific subexpression.
如何理解"Temporary objects are destroyed as the last step in evaluating the full-expression ([intro.execution]) that (lexically) contains the point where they were created."?谁能举个简单的例子说清楚?
简单的例子。此表达式生成一个临时对象:
std::string("test")
此处,该表达式用作子表达式:
function(std::string("test"));
// point A
在 A 点,临时对象已被销毁,因为该点位于创建临时对象的完整表达式之后。
这里有一个例子,说明如果不理解这条规则,如何编写错误:
const std::string& function(const std::string& arg) {
return arg;
}
const std::string& ref = function("test");
std::cout << ref;
在这里,作为参数创建的临时对象在完整表达式后被销毁,因此 ref
变得无效 - 悬空引用。将无效引用插入输出流时,行为未定义。
一种在很多情况下都适用的解释是,当执行到语句末尾的分号时,临时对象被销毁。某些语言结构(例如 for
循环)未包含在本说明中,因此请不要勉强解释。有关异常的更好解释,请参阅
举个例子:
i = foo1(foo2(std::string("test")));
临时字符串在赋值之后一直保持活动状态,因为赋值发生在语句结束之前。 (在这种情况下,完整的表达式就是语句。)