简体   繁体   English

临时终身延长

[英]Temporary lifetime extension

The 12.2.5 section of standard says: 标准的12.2.5部分说:

A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. 在函数调用(5.2.2)中与引用参数的临时绑定将持续存在,直到包含该调用的完整表达式完成为止。 A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. 函数返回语句(6.6.3)中返回值的临时绑定将一直存在,直到函数退出。 In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction. 在所有这些情况下,在初始化引用的表达式的求值期间创建的临时值,除了引用所绑定的临时值之外,在创建它们的完整表达式的末尾以及与它们相反的顺序被销毁。完成他们的建设。

The code I try to understand is: 我试着理解的代码是:

#include <iostream>

const int& foo(const int& fooRef)
{
    return fooRef;
}                                        // #0

int main (void)
{
    const int& numberRef = foo(5);     // #1
    std::cout << numberRef;            // #2
    return 0;
}

On line #1 a temporary object is created and bound to fooRef parameter of foo . #1行,创建一个临时对象并绑定到foo fooRef参数。 fooRef is destroyed on line #0 . fooRef#0行被销毁。 So I thought the temporary should be destroyed here since lifetime-extension is not transitive. 所以我认为临时应该在这里销毁,因为生命延长不是传递性的。

Questions: 问题:

  1. What does until the function exits mean? until the function exits意味着什么? Does it mean untill it finished executing ? 这是否意味着untill it finished executing

  2. Why do I get a 5 output. 为什么我得到5输出。 Does a temporary object still exist on line #2 ? #2行还存在临时对象吗?

  3. How can I interpret the standard quote to figure out how this example works? 如何解释标准引用以弄清楚此示例的工作原理?

Step-by-step atomic walk-through with references to the standard would be greatly appreciated. 将非常感谢参考标准的逐步原子演练。 Thank you! 谢谢!

PS An accepted answer here also told the the code is broken and I do not get, why I get such output of program. PS 这里接受的答案也告诉我代码被broken ,我不知道,为什么我得到这样的程序输出。

What does until the function exits mean? 直到函数退出意味着什么? Does it mean untill it finished executing? 这是否意味着直到它完成执行?

Yes. 是。

Why do I get a 5 output. 为什么我得到5输出。 Does a temporary object still exist on line #2? 第2行还存在临时对象吗?

Dereferencing a reference which is not bound to a living object is undefined behavior , so you may get 5 as well as 42 as well as anything else (including a crash). 取消引用未绑定到活动对象的引用是未定义的行为 ,因此您可以获得5以及42以及其他任何内容(包括崩溃)。 You simply cannot have any expectation on a program that has undefined behavior. 您根本无法对具有未定义行为的程序抱有任何期望。

How can I interpret the standard quote to figure out how this example works? 如何解释标准引用以弄清楚此示例的工作原理?

Pretty much like you did already.The temporary gets bound to the function parameter fooRef , which gets destroyed when returning from the function. 就像你已经做的那样。临时绑定到函数参数fooRef ,它从函数返回时被销毁。 Since that temporary is bound to the returned value, that object ceases to exist when the function returns. 由于该临时值与返回值绑定,因此该函数返回时该对象不再存在。 Later on, you are dereferencing a dangling reference, which gives you UB. 稍后,您将取消引用悬空引用,它会为您提供UB。

  1. It means until the closing brace, ie } . 这意味着直到结束括号,即}

  2. You invoked UB, you have a dangling reference. 你调用了UB,你有一个悬空引用。

Try the following modification of your code and see what it prints. 尝试以下修改代码并查看其打印内容。 It probably will print 6 because that is what was last on the stack. 它可能会打印6因为这是堆栈中的最后一个。 Or try passing a std::string instead, you might get a crash. 或者尝试传递std::string ,你可能会崩溃。

int main (void)
{
    const int& numberRef = foo(5);  
    foo(6);
    std::cout << numberRef;
    return 0;
}

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

相关问题 使用右值引用延长临时项的生存期 - Extension of the lifetime of a temporary with an rvalue reference 命名构造函数和临时生命周期扩展 - Named constructors and temporary lifetime extension 临时生命周期扩展和隐式转换为 const 引用 - Temporary lifetime extension and implicit conversion to a const reference C ++ 17和静态临时生命周期的引用扩展 - C++17 and reference extension of static temporary lifetime 为什么临时生命周期扩展会导致多次调用析构函数? - Why would temporary lifetime extension cause destructor to be called multiple times? 什么时候临时生命周期扩展在现代C ++中有用? - When is temporary lifetime extension useful in modern C++? I / O机械手错误或由const ref临时延长生命周期? - I/O manipulator bug or temporary lifetime extension by const ref? 临时生命周期延长与 clang 上的复制省略对象混合 - Temporary lifetime extension mixed with copy elision object on clang 临时对象的生命周期延长:包含函数调用的完整表达式是什么? - Lifetime extension of temporary objects: what is the full expression containing a function call? 我们是否应该在 C++17 及更高版本中使用临时延长寿命? - Should we use lifetime extension of a temporary in C++17 and later?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM