What's the life-time of a function parameter (citation needed)? [closed]

拜拜、爱过 提交于 2020-01-02 07:18:32

问题


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').


回答1:


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

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.

Is this what you are asking for?




回答2:


Although the standard does definitely claim that the lifetime of a parameter ends when the function in which it is defined returns, as answered by Anton Savin, this is not what implementations do, and the standard will likely be changed to allow what implementations do:

WG decided to make it unspecified whether parameter objects are destroyed immediately following the call or at the end of the full-expression to which the call belongs.

This means the lifetime of a function parameter would become unspecified. It might end as soon as the function returns, or it might end later.



来源:https://stackoverflow.com/questions/27157204/whats-the-life-time-of-a-function-parameter-citation-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!