函数参数的生命周期是什么(需要引用)? [英] What's the life-time of a function parameter (citation needed)?

查看:216
本文介绍了函数参数的生命周期是什么(需要引用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数参数的生命周期是否等于一个未命名的临时变量作为rvalue引用传递(等于表达式的函数)?我的'gcc'编译器显示它是。但我想看到一个实际的标准文档(可能是最新的C ++ 11或C ++ 14)。

Is the life-time of a function parameter equal of an unnamed temporary passed as an 'rvalue' reference (which is equal of the expression called the function)? My 'gcc' compiler shows that it is. But I want to see an actual standard document that states it too (possible the newest 'C++11' or 'C++14').

推荐答案

5.2.2 [expr.call] / 4 似乎在C ++ 11和C ++ 14中是相同的:

5.2.2 [expr.call]/4, seems to be the same in C++11 and C++14:


当一个函数被调用时,每个参数(8.3.5)都需要用相应的
参数初始化(8.5,12.8,12.1) 。 [注意:这种初始化相对于彼此是不确定的顺序(1.9) -
end note]如果函数是非静态成员函数,函数的这个参数(9.3.2)将
使用指向调用对象的指针来初始化,就像通过显式类型转换(5.4)进行转换。
[注意:此转换没有访问或模糊检查;访问检查和消除歧义
作为(可能隐式)类成员访问运算符的一部分来完成。见10.2,11.2和5.2.5。 -
end note]调用函数时,具有对象类型的参数应该具有完全定义的
对象类型。 [注意:这仍然允许一个参数是一个指针或一个不完整的类类型的引用。
然而,它阻止了一个传值的参数有一个不完整的类类型。 - end note]在
初始化参数期间,一个实现可以避免通过
构造额外的临时值,将相关联的参数的转换和/或临时值的构造与
初始化的参数(见12.2)。 当定义
的函数返回时,参数的生命周期结束。
每个参数的初始化和销毁​​发生在
调用函数的上下文中。 [示例:构造函数,转换函数或析构函数的访问在
处检查调用函数中的调用点。如果函数参数的构造函数或析构函数抛出
异常,则在调用函数的作用域中搜索处理程序;特别是如果函数
调用了一个带有可以处理异常的处理程序的函数try-block(第15条),这个处理程序是
。 -end example]函数调用的值是被调用的函数
返回的值,除非在虚函数调用中,如果最终重载的返回类型不同于
的返回类型静态选择函数,从最终覆盖函数返回的值将转换为
的静态选择函数的返回类型。

When a function is called, each parameter (8.3.5) shall be initialized (8.5, 12.8, 12.1) with its corresponding argument. [ Note: Such initializations are indeterminately sequenced with respect to each other (1.9) — end note ] If the function is a non-static member function, the this parameter of the function (9.3.2) shall be initialized with a pointer to the object of the call, converted as if by an explicit type conversion (5.4). [ Note: There is no access or ambiguity checking on this conversion; the access checking and disambiguation are done as part of the (possibly implicit) class member access operator. See 10.2, 11.2, and 5.2.5. — end note ] When a function is called, the parameters that have object type shall have completely-defined object type. [ Note: this still allows a parameter to be a pointer or reference to an incomplete class type. However, it prevents a passed-by-value parameter to have an incomplete class type. —end note ] During the initialization of a parameter, an implementation may avoid the construction of extra temporaries by combining the conversions on the associated argument and/or the construction of temporaries with the initialization of the parameter (see 12.2). The lifetime of a parameter ends when the function in which it is defined returns. The initialization and destruction of each parameter occurs within the context of the calling function. [ Example: the access of the constructor, conversion functions or destructor is checked at the point of call in the calling function. If a constructor or destructor for a function parameter throws an exception, the search for a handler starts in the scope of the calling function; in particular, if the function called has a function-try-block (Clause 15) with a handler that could handle the exception, this handler is not considered. —end example ] The value of a function call is the value returned by the called function except in a virtual function call if the return type of the final overrider is different from the return type of the statically chosen function, the value returned from the final overrider is converted to the return type of the statically chosen function.

你要的是什么?

这篇关于函数参数的生命周期是什么(需要引用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆