#include <iostream>using namespace std;int main(){ int a = 5; int&& b = 3; int& c = a; b++; c++; cout << " Value for b " << b << " Value for c " << c << endl;}
其次,当为临时对象分配引用时,引用将使用该对象的数据.但是根据临时对象的定义,它们会在使用它们的表达式结束时被删除.如果该临时对象内存不足,该引用如何作为该临时对象的别名?
解决方法 数字文字不能绑定到任何引用,既不是右值引用也不是左值引用.从概念上讲,数字文字创建一个从文字值初始化的临时对象,这个临时对象可以绑定到右值引用或const左值引用(int const& r = 17;).文字的相关引用似乎是5.1.1 [expr.prim.general]第1段:A literal is a primary Expression. Its type depends on its form (2.14). A string literal is an lvalue; all other literals are prvalues.
将引用直接绑定到临时引用时,它的生命周期会延长,直到引用超出范围.终身问题的相关部分是12.2 [class.temporary]第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
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.
for the lifetime of the reference except:
以上是内存溢出为你收集整理的C:右值参考存储器全部内容,希望文章能够帮你解决C:右值参考存储器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)