参考表达式的评估 [英] Evaluation of a reference expression

查看:134
本文介绍了参考表达式的评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据@ Potatoswatter的建议,我创造了一个新的讨论。



参考是



鉴于代码片段,

  int i = 3,& j = i; 
j = ++ i;

我要澄清的评论是这样的。 (这似乎是我对未定序评估aka序列点的理解中的重要缺失):


@Chubsdad:即使它是别名,
它的glvalue评估不
需要我的glvalue评估。
一般来说,评估
引用不需要
原始对象在手。

没有理由应该是UB,所以它使
感觉应该有一个容易的漏洞
或转换为不是
UB的代码。



引用不告诉
编译器去查看引用的
变量并获取它的左值,因为
它可能不知道什么变量是
引用。编译器计算引用的
lvalue,
lvalue标识一个对象。如果您要
想进一步辩论,请
打开一个新问题。


这个问题不清楚是我在C ++ 0x中理解'无序评估','序列点'等''未定义行为'的一部分。

解决方案

想象下面的

  int& i = * new INT; 

如果你说是一个别名另一个名字 - 什么名字?引用引用一个对象或函数。当你说glvalue时,你引用特定表达式的属性,而不是对象的属性。

  int i = 0; 
int& ri = i;

现在,是一个左值表达式 ri 也是一个左值表达式(两个句法类别 id-expression )。他们名称(通过名称查找找到)引用和 int 变量。



如果现在确定 ri case的对象标识,则需要使用引用,并使表达式引用它被初始化的对象。这被称为左值评估,因为您确定左值的属性(即指示物)。



您需要为 i 案例做同样的事情。我想知道左值表达式 i 是指什么对象。因此,对于 ri 的glvalue评估与 i 的glvalue评估不同,尽管它们都产生相同的结果。



Rvalue评估意味着采取左值,并将左值应用于右值转换。换句话说,读一个值。


As per @Potatoswatter's suggestion, I have created a new discussion.

Reference is this response from @Potatoswatter

Given the code snippet,

int i = 3, &j = i; 
j = ++ i;

The comment which I seek clarity on, is this. (which seems to be an important missing piece in my understanding of the unsequenced evaluation a.k.a sequence point):

@Chubsdad: Even though it's an alias, its glvalue evaluation does not require a glvalue evaluation of i. Generally speaking, evaluating a reference does not require the original object to be on hand. There's no reason it should be UB, so it makes sense there should be an easy loophole or transformation to code which is not UB.

and

The reference doesn't tell the compiler to go look at the referenced variable and get its lvalue, because it might not know what variable is referenced. The compiler computes the lvalue of the reference and that lvalue identifies an object. If you want to debate this further, please open a new question.

Any possible lack of clarity in the question is part of the 'undefined behavior' I am going through trying to understand 'unsequenced evaluation', 'sequence point' etc in C++0x.

解决方案

Imagine the following

int &i = *new int;

If you say that i is an alias for another name - what name? A reference either references an object or function. When you say "glvalue", you refer to a property of a particular expression, not to a property of an object.

int i = 0;
int &ri = i;

Now, i is an lvalue expression and ri is an lvalue expression too (both of the syntactic category id-expression). They name (as found by name-lookup) a reference and an int variable.

If you now determine the object identity for the ri case, you need to take the reference and make the expression refer to the object it was initialized with. This is called an lvalue evaluation because you determine the property of an lvalue (i.e the referent).

You need to do the same for the i case. I.e you figure out to what object the lvalue expression i refers to. A glvalue evaluation of ri thus is different than glvalue evaluation of i, despite both of them yielding the same result.

Rvalue evaluation means to take an lvalue and apply the lvalue to rvalue conversion. In other words, to read a value.

这篇关于参考表达式的评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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