通常,解引用指针表达式结果是引用类型吗?

Generally, is dereference pointer expression results a reference type?

引用指针导致间接使用对象的值。但我从来没有真正理解 "using" 是什么意思。我开始思考这个问题,直到我的编译器对以下代码产生错误

int i = 0, *pi = &i;
decltype(*pi) c; // error: 'c' declared as reference but not initialized.

看了很久的错误,搜索了一些问题,只能给出如下论据。不知道对不对

参数 1:

1) *p是一个不是变量的表达式(或非变量表达式)

2) 解引用指针表达式产生引用,我们实际上是在使用引用来访问对象的值

参数 2:

解引用表达式仅针对 decltype returns 一个引用,这不是一般情况

以上论点如有不正确或不准确的描述,请指出。

Dereferencing a pointer yields an lvalue expression of the pointed-to type designating the object or function pointed to. 它不产生引用。* *pi 是类型 int 的左值。

decltype(此处不相关的例外情况)报告表达式的 类型 及其 值类别 the latter being encoded with reference types。由于*pi是一个左值,它被编码为左值引用类型,所以decltype(*pi)int &int为类型,&为值类别。

表达式从来没有引用类型,因为任何引用都被调整掉了 "prior to any further analysis"


* 这不仅仅是技术上的区别:根据 core issue 232 and core issue 453 的方向,您可以在绑定处编写有效的解引用表达式它对引用的结果会导致未定义的行为。

事实上,标准在 8.5.2.1 中表示如下:

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T”, the type of the result is “T”.

所以它不是引用,但是 decltype() 给你一些在 C++ 类型系统中实际上有表示的东西,引用是最接近它的东西。

回答你的标题问题:如T.C。也回答说没有。表达式从来没有引用类型。给定一个 int a; int &b = a;,表达式 a 和表达式 b 的类型都是 int,并且两个表达式都是左值。

1) *p is an expression that is not a variable (or non-variable expression)

正确。

2) dereferencing pointer expression yields a reference, we are in fact using a reference to access the value of the object

取消引用指针会产生左值,decltype 变为左值引用。

the dereferencing expression only for which decltype returns a reference, it is not a general case

我不完全确定你在这里的意思。如果你的意思是当 decltype 不产生引用时,有一个没有初始化器的声明 decltype(...) c; 是有效的,那么是的,确实如此。如果您的意思是除了取消引用的指针之外, decltype 永远不会产生引用类型,那么不会。例如,

int a;
decltype((a)) b; // not okay - decltype((a)) is int & because (a) is not the name of the
                 // variable, and the expression (a) is an lvalue
decltype(a) c;   // okay - decltype(a) is int because a is the name of the variable
decltype(+a) d;  // okay - decltype(+a) is int because the +a is a prvalue of type int