为了账号安全,请及时绑定邮箱和手机立即绑定

为什么非Const引用不能绑定到临时对象?

为什么非Const引用不能绑定到临时对象?

C++ C
哆啦的时光机 2019-05-30 13:37:47
为什么非Const引用不能绑定到临时对象?为什么不允许获取对临时对象的非Const引用,哪个函数getx()退货?很明显,这是C+标准所禁止的,但我对这种限制的目的感兴趣,不是参考达到标准。struct X{     X& ref() { return *this; }};X getx() { return X();}void g(X & x) {}    int f(){     const X& x = getx(); // OK     X& x = getx(); // error     X& x = getx().ref(); // OK     g(getx()); //error     g(getx().ref()); //OK     return 0;}很明显,对象的生存期不能成为原因,因为对象的持续引用是不受禁止C+标准。显然,在上面的示例中,临时对象不是常量,因为允许调用非常量函数。例如,ref()可以修改临时对象。此外,ref()允许您欺骗编译器,并获得到这个临时对象的链接,从而解决我们的问题。此外:他们说“将一个临时对象分配给Const引用延长了该对象的生存期”,并且“尽管没有提到非Const引用”。我的附加问题。之后的赋值是否延长了临时对象的生存期?X& x = getx().ref(); // OK
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

在你的代码中getx()返回一个临时对象,称为“rvalue”。您可以将rvalue复制到对象(也就是。或者将它们绑定到Const引用(这将延长它们的生存期,直到引用的生命周期结束)。不能将rvalue绑定到非Const引用。

这是一个刻意的设计决定,目的是防止用户意外修改将在表达式末尾死亡的对象:

g(getx()); // g() would modify an object without anyone being able to observe

如果要这样做,则必须首先创建一个本地副本或对象的本地副本,或者将其绑定到Const引用:

X x1 = getx();const X& x2 = getx(); // extend lifetime of temporary to lifetime of const referenceg(x1); // fineg(x2); 
// can't bind a const reference to a non-const reference

注意,下一个C+标准将包括rvalue引用。因此,你所知道的“引用”正在被称为“lvalue引用”。您将被允许将rvalue绑定到rvalue引用,并且可以在“rvalue-ness”上重载函数:

void g(X&);   // #1, takes an ordinary (lvalue) referencevoid g(X&&);  // #2, takes an rvalue referenceX x; g(x);      
// calls #1g(getx()); // calls #2g(X());    // calls #2, too

rvalue引用背后的思想是,由于这些对象无论如何都会死掉,所以您可以利用这些知识并实现所谓的“移动语义”,这是一种特定的优化:

class X {
  X(X&& rhs)
    : pimpl( rhs.pimpl ) // steal rhs' data...
  {
    rhs.pimpl = NULL; // ...and leave it empty, but deconstructible
  }

  data* pimpl; // you would use a smart ptr, of course};X x(getx()); // x will steal the rvalue's data, leaving the temporary object empty


查看完整回答
反对 回复 2019-05-30
  • 3 回答
  • 0 关注
  • 909 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信