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

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

问题描述

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

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

参考是此响应来自@Potatoswatter

Reference is this response from @Potatoswatter

给定代码片段,

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:别名,
它的glvalue评估不
需要对i的glvalue求值。
一般来说,评估
引用不需要手头有
原始对象。

没有理由应该是UB,因此使
有意义,应该有一个简单的漏洞
或转换到不是
UB的代码。

@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.


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

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.

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

推荐答案

想象以下

int &i = *new int;

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

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;

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

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.

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

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).

您需要对 i 情况执行相同操作。也就是说,你需要知道$ i 指向的值是什么对象。因此, ri 的glvalue评估不同于 i 的glvalue评估,尽管它们都产生相同的结果。

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天全站免登陆