这个指针是r值吗?(Is this pointer an r-value? [duplicate])

这个问题在这里已有答案:

'this'指针 4的 类型 回答

我所知道的是:

L值是一个表达式(可以是单个变量表达式,即x),它具有一些地址(意味着它存储在存储器中并且可以被引用,超出当前表达式的范围)。 “this pointer”指向对象本身,这意味着它保存对象的地址,这反过来意味着它是L值(根据定义在第1点)。 我想知道的是: “这个指针”真的是L值吗? 因为在帖子“ 这个'指针的类型'中,人们说”这个指针“不是L值 如果“this pointer”不是L值,那么它是R值还是其他什么?

This question already has an answer here:

Type of 'this' pointer 4 answers

What I know is:

An L-Value is an expression (could be a single variable expression i.e x) which has some address (meaning it's stored in memory and can be referred to, beyond the scope of current expression). "this pointer" points to the object itself, which means it holds the address of the object which in turn means it's an L-Value (by definition in point 1). What I want to know is: Is "this pointer" really an L-Value ? Because in a post " Type of 'this' pointer " people are saying that "this pointer" is not an L-Value If "this pointer" is not an L-Value then is it an R-Value or something else ?

最满意答案

从幼稚的角度来看,你对 值的基本描述是一个相当好的一般规则,但是在存在用于表达式评估的 值 - 右值转换时它就会崩溃,并且它没有考虑到诸如this关键字之类的显着异常。

由于this本身不是一个你可以修改或认为真正“存储”在任何地方的指针 - 回想一下它的值是由编译器根据上下文自动确定的 - 委员会决定它最好作为一个右值表达式。

因此,你对内存地址的推理并不完全正确:作为指针,它当然包含一个内存地址作为其物理值(在典型系统上),但这并不意味着它本身就有一个。 尝试取其地址: &this不起作用。

Your basic description of lvalues is a fairly good general rule from a naive perspective, but it falls apart in the presence of lvalue-to-rvalue conversions for expression evaluation, and it doesn't account for notable exceptions such as the this keyword.

Since this itself is not a pointer that you can modify or deem to really be "stored" anywhere — recall that its value is automatically determined by the compiler based on context — the committee decided that it would be best off as an rvalue expression.

Accordingly, your reasoning about memory addresses is not quite right: this, as a pointer, certainly contains a memory address as its physical value (on typical systems) but that doesn't mean it has one for itself. Try taking its address: &this can't work.

更多推荐