C ++中的所有临时值是否都是临时值? [英] Are all temporaries rvalues in C++?

查看:189
本文介绍了C ++中的所有临时值是否都是临时值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几年我一直在用C ++编程。但有一个问题,我没有能够弄清楚。



如果没有,任何人都可以提供一个例子,其中代码中临时产生的是 lvalue



C ++语言规范从来没有像你所问的那样做出这样直截了当的断言。它在语言标准中没有说所有临时对象都是右值。此外,问题本身有点不当,因为在C ++语言中作为右值的属性不是对象的属性,而是表达式的属性(即其结果的属性)。这实际上是如何在语言规范中定义的:对于不同种类的表达式,当结果是左值时以及当它是右值时它表示。其中,这实际上意味着临时对象可以作为右值和左值访问,这取决于用于执行访问的表达式的具体形式。



例如,文字 2 + 3 表达式的结果显然是一个右值,是 int 。我们不能应用一元& ,因为一元& 需要一个左值作为其操作数

 &(2 + 3); // ERROR,lvalue required 

但是,我们都知道,常量引用可以附加到临时对象,如

  const int& ri = 2 + 3; 

在这种情况下,引用将附加到临时文件,延长后者的生命周期。显然,一旦完成,我们就可以访问和$ ri 一样的临时变量,因为引用总是左值。例如,我们可以轻松地并合法地将一元& 应用于引用,并获取指向临时

的指针

  const int * pi =& ri; 

只要临时文件仍然存在,该指针就完全有效。



对临时对象进行左值访问的另一个明显的例子是当我们通过其 this 指针访问类类型的临时对象时。 * this 的结果是一个左值(与将一元 * 的结果应用于数据的情况指针),但它不改变事实,实际的对象可能很容易是一个临时的。对于给定类类型 T ,表达式 T()是语言标准中明确说明的右值,通过 * T()。get_this()表达式访问临时对象(显式实现 T :: get_this())是一个左值。与前面的例子不同,这个方法允许你立即获得一个非const限定的左值,它引用一个临时对象。



所以,临时对象可能很容易被看见为右值或左值,这取决于您用来查看该对象的什么类型的表达式(什么样的访问路径)。


I have been coding in C++ for past few years. But there is one question that I have not been able to figure out. I want to ask, are all temporaries in C++, rvalues?

If no, can anyone provide me an example where temporary produced in the code is an lvalue?

解决方案

No.

The C++ language specification never makes such a straightforward assertion as the one you are asking about. It doesn't say anywhere in the language standard that "all temporary objects are rvalues". Moreover, the question itself is a bit of misnomer, since the property of being an rvalue in the C++ language is not a property of an object, but rather a property of an expression (i.e. a property of its result). This is actually how it is defined in the language specification: for different kinds of expressions it says when the result is an lvalue and when it is an rvalue. Among other things, this actually means that a temporary object can be accessed as an rvalue as well as an lvalue, depending on the specific form of expression that is used to perform the access.

For example, the result of literal 2 + 3 expression is obviously an rvalue, a temporary of type int. We cannot apply the unary & to it since unary & requires an lvalue as its operand

&(2 + 3); // ERROR, lvalue required

However, as we all know, a constant reference can be attached to a temporary object, as in

const int &ri = 2 + 3;

In this case the reference is attached to the temporary, extending the lifetime of the latter. Obviously, once it is done, we have access to that very same temporary as an lvalue ri, since references are always lvalues. For example, we can easily and legally apply the unary & to the reference and obtain a pointer to the temporary

const int *pi = &ri;

with that pointer remaining perfectly valid as long as the temporary persists.

Another obvious example of lvalue access to a temporary object is when we access a temporary object of class type through its this pointer. The result of *this is an lvalue (as is always the case with the result of unary * applied to a data pointer), yet it doesn't change the fact that the actual object might easily be a temporary. For a given class type T, expression T() is an rvalue, as explicitly stated in the language standard, yet the temporary object accessed through *T().get_this() expression (with the obvious implementation of T::get_this()) is an lvalue. Unlike the previous example, this method allows you to immediately obtain a non-const-qualified lvalue, which refers to a temporary object.

So, once again, the very same temporary object might easily be "seen" as an rvalue or as an lvalue depending on what kind of expression (what kind of access path) you use to "look" at that object.

这篇关于C ++中的所有临时值是否都是临时值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆