绑定到函数参数的引用是否会延长该临时的生命周期?

时间:2023-01-25 05:34:17

I have this code (simplified version):

我有这个代码(简化版):

const int& function( const int& param ){     return param;}const int& reference = function( 10 );//use reference

I can't quite decide to which extent C++03 Standard $12.2/5 wording

我不能完全决定C ++ 03标准在12.2 / 5美元的措辞

The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference...

引用绑定的临时对象或作为临时绑定的子对象的完整对象的临时对象在引用的生命周期内持续存在...

is applicable here.

适用于此处。

Is reference variable in the code above valid or dangling? Will the reference in the calling code prolong the lifetime of the temporary passed as the parameter?

上面的代码中的引用变量是有效还是悬空?调用代码中的引用是否会延长作为参数传递的临时生命周期?

4 个解决方案

#1


10  

A full-expression is an expression that is not a subexpression of another expression. In this case, the full-expression containing the call function( 10 ) is the assignment expression:

完整表达式是不是另一个表达式的子表达式的表达式。在这种情况下,包含调用函数(10)的完整表达式是赋值表达式:

const int& reference = function( 10 );

In order to call function with the argument 10, a temporary const-reference object is created to the temporary integer object 10. The lifetime of the temporary integer and the temporary const-reference extend through the assignment, so although the assignment expression is valid, attempting to use the integer referenced by reference is Undefined Behavior as reference no longer references a live object.

为了使用参数10调用函数,为临时整数对象10创建临时const引用对象。临时整数的生命周期和临时const-reference通过赋值扩展,因此尽管赋值表达式有效,尝试使用引用引用的整数是Undefined Behavior as reference不再引用活动对象。

The C++11 Standard, I think, clarifies the situation:

我认为,C ++ 11标准澄清了这种情况:

The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:

引用绑定的临时对象或绑定引用的子对象的完整对象的临时对象在引用的生命周期内持续存在,除了:

...

— 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)中的引用参数的临时绑定将持续存在,直到包含该调用的完整表达式完成。

"The temporary to which the reference is bound ... persists for the lifetime of the reference". In this case, the lifetime of the reference ends at the end of the assignment expression, as does the lifetime of the temporary integer.

“引用绑定的临时值......在引用的生命周期中持续存在”。在这种情况下,引用的生命周期在赋值表达式的末尾结束,临时整数的生存期也是如此。

#2


2  

This will compile, but you will end up with a dangling reference. param is freed after function returns.

这将编译,但你最终会有一个悬空参考。函数返回后释放param。

  1. function is called with a reference to the temporary, anonymous object
  2. 函数是通过引用临时的匿名对象来调用的

  3. function returns the reference
  4. 函数返回引用

  5. now that function has returned the temporary param is released
  6. 现在该函数已经返回临时参数被释放

  7. the reference is now dangling as the object was destroyed
  8. 当物体被摧毁时,参考物现在悬空

If you had made it non-const, then it wouldn't have compiled because you cannot pass a non-const reference to an anonymous object.

如果你使它成为非const,那么它就不会被编译,因为你不能将非const引用传递给匿名对象。

#3


2  

From the C++11 viepoint a reference returned by a function is not a temporary:

从C ++ 11 viepoint开始,函数返回的引用不是临时的:

12.12.1 Temporaries of class type are created in various contexts: binding a reference to a prvalue (8.5.3), returninga prvalue (6.6.3), a conversion that creates a prvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1),entering a handler (15.3), and in some initializations (8.5).

12.12.1类型的临时数在各种上下文中创建:绑定对prvalue的引用(8.5.3),返回prvalue(6.6.3),创建prvalue的转换(4.1,5.2.9,5.2.11, 5.4),抛出异常(15.1),进入处理程序(15.3),并进行一些初始化(8.5)。

A function returning a reference dosn't return prvalue ("pure rvalue"), so it's not a temporary.This seems quite natural: compiler can't manage lifetime of referenced objects, it is programmer's responsibility

返回引用的函数不会返回prvalue(“纯rvalue”),因此它不是临时的。这似乎很自然:编译器无法管理引用对象的生命周期,这是程序员的责任

Thus, the compiler doesn't provide any liftime guarantes for const int& reference since it is not bounded to temporary.

因此,编译器不为const int和reference提供任何生命周期保证,因为它不受临时限制。

#4


1  

It is this part that is important

这部分很重要

The temporary to which the reference is bound

引用绑定的临时值

In this case the parameter is bound to the temporary, and will be destroyed after the call.

在这种情况下,参数绑定到临时,并在调用后将被销毁。

You cannot extend the lifetime further by passing the reference on.

您无法通过传递引用来进一步延长生命周期。

#1


10  

A full-expression is an expression that is not a subexpression of another expression. In this case, the full-expression containing the call function( 10 ) is the assignment expression:

完整表达式是不是另一个表达式的子表达式的表达式。在这种情况下,包含调用函数(10)的完整表达式是赋值表达式:

const int& reference = function( 10 );

In order to call function with the argument 10, a temporary const-reference object is created to the temporary integer object 10. The lifetime of the temporary integer and the temporary const-reference extend through the assignment, so although the assignment expression is valid, attempting to use the integer referenced by reference is Undefined Behavior as reference no longer references a live object.

为了使用参数10调用函数,为临时整数对象10创建临时const引用对象。临时整数的生命周期和临时const-reference通过赋值扩展,因此尽管赋值表达式有效,尝试使用引用引用的整数是Undefined Behavior as reference不再引用活动对象。

The C++11 Standard, I think, clarifies the situation:

我认为,C ++ 11标准澄清了这种情况:

The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:

引用绑定的临时对象或绑定引用的子对象的完整对象的临时对象在引用的生命周期内持续存在,除了:

...

— 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)中的引用参数的临时绑定将持续存在,直到包含该调用的完整表达式完成。

"The temporary to which the reference is bound ... persists for the lifetime of the reference". In this case, the lifetime of the reference ends at the end of the assignment expression, as does the lifetime of the temporary integer.

“引用绑定的临时值......在引用的生命周期中持续存在”。在这种情况下,引用的生命周期在赋值表达式的末尾结束,临时整数的生存期也是如此。

#2


2  

This will compile, but you will end up with a dangling reference. param is freed after function returns.

这将编译,但你最终会有一个悬空参考。函数返回后释放param。

  1. function is called with a reference to the temporary, anonymous object
  2. 函数是通过引用临时的匿名对象来调用的

  3. function returns the reference
  4. 函数返回引用

  5. now that function has returned the temporary param is released
  6. 现在该函数已经返回临时参数被释放

  7. the reference is now dangling as the object was destroyed
  8. 当物体被摧毁时,参考物现在悬空

If you had made it non-const, then it wouldn't have compiled because you cannot pass a non-const reference to an anonymous object.

如果你使它成为非const,那么它就不会被编译,因为你不能将非const引用传递给匿名对象。

#3


2  

From the C++11 viepoint a reference returned by a function is not a temporary:

从C ++ 11 viepoint开始,函数返回的引用不是临时的:

12.12.1 Temporaries of class type are created in various contexts: binding a reference to a prvalue (8.5.3), returninga prvalue (6.6.3), a conversion that creates a prvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1),entering a handler (15.3), and in some initializations (8.5).

12.12.1类型的临时数在各种上下文中创建:绑定对prvalue的引用(8.5.3),返回prvalue(6.6.3),创建prvalue的转换(4.1,5.2.9,5.2.11, 5.4),抛出异常(15.1),进入处理程序(15.3),并进行一些初始化(8.5)。

A function returning a reference dosn't return prvalue ("pure rvalue"), so it's not a temporary.This seems quite natural: compiler can't manage lifetime of referenced objects, it is programmer's responsibility

返回引用的函数不会返回prvalue(“纯rvalue”),因此它不是临时的。这似乎很自然:编译器无法管理引用对象的生命周期,这是程序员的责任

Thus, the compiler doesn't provide any liftime guarantes for const int& reference since it is not bounded to temporary.

因此,编译器不为const int和reference提供任何生命周期保证,因为它不受临时限制。

#4


1  

It is this part that is important

这部分很重要

The temporary to which the reference is bound

引用绑定的临时值

In this case the parameter is bound to the temporary, and will be destroyed after the call.

在这种情况下,参数绑定到临时,并在调用后将被销毁。

You cannot extend the lifetime further by passing the reference on.

您无法通过传递引用来进一步延长生命周期。