const引用会延长临时变量的寿命与自己一样?

看到机器就疯狂 2013-08-06 08:42:21
如果一个函数返回一个临时变量,const引用绑定这个临时变量,那么它会延长这个临时变量的寿命与自己一样?
...全文
579 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lx111000lx0 2016-03-22
  • 打赏
  • 举报
回复
楼主如果是个菇凉,我可以告诉你去看看 http://itlab.idcquan.com/c/vc/200906/785943.html 写的很详细,左值和右值的问题。
shenxiaolong1976 2014-01-06
  • 打赏
  • 举报
回复
引用 3 楼 u010828523 的回复:
[quote=引用 1 楼 starytx 的回复:] 返回的是临时变量而不是临时变量的地址,所以会复制生成一个临时对象,引用绑定的实际是那个临时对象,和函数内的临时变量没有关系了
为什么去掉const,G++报告错误?[/quote] 这是G++编译器的问题,不是C++语法的问题。关于const,其涉及到internal linkage/external linkage问题,和楼主所说的问题无关。 我专门测试了一下,在MS编译器上OK。
FeelTouch Labs 2013-08-06
  • 打赏
  • 举报
回复
引用 3 楼 u010828523 的回复:
[quote=引用 1 楼 starytx 的回复:] 返回的是临时变量而不是临时变量的地址,所以会复制生成一个临时对象,引用绑定的实际是那个临时对象,和函数内的临时变量没有关系了
为什么去掉const,G++报告错误?[/quote] 虽然没有看到你的代码,大致感觉到,去掉const后一定是更改了某个不可更改的操作 参考:http://blog.chinaunix.net/uid-26748613-id-3378023.html
rocktyt 2013-08-06
  • 打赏
  • 举报
回复
引用 13 楼 rocktyt2 的回复:
[quote=引用 12 楼 u010828523 的回复:] [quote=引用 11 楼 rocktyt2 的回复:] [quote=引用 10 楼 u010828523 的回复:] [quote=引用 7 楼 rocktyt2 的回复:] [quote=引用 6 楼 mujiok2003 的回复:] 取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote] 没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用 讲引用的地方有

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
[/quote] 用const引用来引用临时变量,是标准提倡的,还是标准规定必须这么做的?[/quote]必须的[/quote]这个应该属于英语范畴 如果不确定shall的意思,可以去查词典 (通常指按照规则或法律规定)必须,一定,将
rocktyt 2013-08-06
  • 打赏
  • 举报
回复
引用 12 楼 u010828523 的回复:
[quote=引用 11 楼 rocktyt2 的回复:] [quote=引用 10 楼 u010828523 的回复:] [quote=引用 7 楼 rocktyt2 的回复:] [quote=引用 6 楼 mujiok2003 的回复:] 取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote] 没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用 讲引用的地方有

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
[/quote] 用const引用来引用临时变量,是标准提倡的,还是标准规定必须这么做的?[/quote]必须的
  • 打赏
  • 举报
回复
引用 11 楼 rocktyt2 的回复:
[quote=引用 10 楼 u010828523 的回复:] [quote=引用 7 楼 rocktyt2 的回复:] [quote=引用 6 楼 mujiok2003 的回复:] 取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote] 没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用 讲引用的地方有

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
[/quote] 用const引用来引用临时变量,是标准提倡的,还是标准规定必须这么做的?
rocktyt 2013-08-06
  • 打赏
  • 举报
回复
引用 10 楼 u010828523 的回复:
[quote=引用 7 楼 rocktyt2 的回复:] [quote=引用 6 楼 mujiok2003 的回复:] 取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote] 没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。[/quote]这段是在讲临时变量的生命周期,不是在讲引用 讲引用的地方有

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
— If the reference is an lvalue reference and the initializer expression
(略)
— Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const), or the reference shall be an rvalue reference.
  • 打赏
  • 举报
回复
引用 7 楼 rocktyt2 的回复:
[quote=引用 6 楼 mujiok2003 的回复:] 取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote] 没看到标准严格规定必须是const引用,怎么G++要求必须是const引用才能绑定临时变量。
mujiok2003 2013-08-06
  • 打赏
  • 举报
回复
引用 7 楼 rocktyt2 的回复:
[quote=引用 6 楼 mujiok2003 的回复:] 取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
[/quote] ++
mujiok2003 2013-08-06
  • 打赏
  • 举报
回复
#include <string>

std::string foo(){ return "bar";}

 int main(int argc,char* argv[])
 {  
	 //case one
	 {
		 std::string const& f = foo();
	 }
	 //case two
	 {
		foo();
	 }
 }
vc 2012 debug:

 //case one
	 {
		 std::string const& f = foo();
010B44B8  lea         eax,[ebp-30h]  
010B44BB  push        eax  
010B44BC  call        foo (010B11EFh)  
010B44C1  add         esp,4  
010B44C4  lea         ecx,[ebp-30h]  
010B44C7  mov         dword ptr [ebp-0Ch],ecx  
	 }
010B44CA  lea         ecx,[ebp-30h]  
010B44CD  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (010B12B7h) 
//延长到}  
	 //case two
	 {
		foo();
010B44D2  lea         eax,[ebp-114h]  
010B44D8  push        eax  
010B44D9  call        foo (010B11EFh)  
010B44DE  add         esp,4  
010B44E1  lea         ecx,[ebp-114h]  
010B44E7  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (010B12B7h)
//立即析构  
	 }
 }
rocktyt 2013-08-06
  • 打赏
  • 举报
回复
引用 6 楼 mujiok2003 的回复:
取决于实现,没有强制要求。 VC好像会延长生命。
标准有明确规定声明周期要延长到和引用的一样
5 The second context is when a reference is bound to a temporary. 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 member in a constructor’s ctor-initializer (12.6.2) persists until the
constructor exits.
— 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.
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not
extended; the temporary is destroyed at the end of the full-expression in the return statement.
— A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the
full-expression containing the new-initializer.
mujiok2003 2013-08-06
  • 打赏
  • 举报
回复
取决于实现,没有强制要求。 VC好像会延长生命。
rocktyt 2013-08-06
  • 打赏
  • 举报
回复
引用 3 楼 u010828523 的回复:
[quote=引用 1 楼 starytx 的回复:] 返回的是临时变量而不是临时变量的地址,所以会复制生成一个临时对象,引用绑定的实际是那个临时对象,和函数内的临时变量没有关系了
为什么去掉const,G++报告错误?[/quote]规定就是这样,右值不能被绑定到左值引用,但可以绑定到常量左值引用和右值引用
rocktyt 2013-08-06
  • 打赏
  • 举报
回复
一般来说是的,但是要注意当const引用绑定到值返回的函数返回值时,生命周期没有被延长,函数调用那行语句一结束对象就会被释放,对引用的操作将会是不可预料的
  • 打赏
  • 举报
回复
引用 1 楼 starytx 的回复:
返回的是临时变量而不是临时变量的地址,所以会复制生成一个临时对象,引用绑定的实际是那个临时对象,和函数内的临时变量没有关系了
为什么去掉const,G++报告错误?
max_min_ 2013-08-06
  • 打赏
  • 举报
回复


const 修饰表示只读数据段, 与生命周期无关的
只读数据段仅是实现的其中一种方式,目的是在运行时令试图修改const变量的行为产生错误。
由于C标准并没有禁止对const变量的修改,而是规定属于未定义行为,
因此一个实现对于试图修改const的行为如何处理都没有违反标准,也就是说,
无论把const变量放在只读段也好,放在可被修改的地方也好(例如你说的栈),都是允许的行为。

starytx 2013-08-06
  • 打赏
  • 举报
回复
返回的是临时变量而不是临时变量的地址,所以会复制生成一个临时对象,引用绑定的实际是那个临时对象,和函数内的临时变量没有关系了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧