Deferencing pointer leads to using the value of the object indirectly. But I've never really understood what does the "using" means. I started to think the question until my compiler yield an error for the following code
int i = 0, *pi = &i;
decltype(*pi) c; // error: 'c' declared as reference but not initialized.
I looked at the error for a very long time and searched some questions I can only give out the following arguments. I don't know if they are correct or not.
Arguments 1:
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
Arguments 2:
the dereferencing expression only for which decltype
returns a reference, it is not a general case
Please points out any incorrectness or inaccurate descriptions of the above arguments.
Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.
I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing is accessing the value of the variable that the pointer points to.
Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.
The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer.
Dereferencing a pointer yields an lvalue expression of the pointed-to type designating the object or function pointed to. It does not yield a reference.**pi
is an lvalue of type int
.
decltype
(with an exception not relevant here) reports both an expression's type and its value category, the latter being encoded with reference types. Since *pi
is an lvalue, it's encoded as an lvalue reference type, so decltype(*pi)
is int &
: int
for the type, &
for the value category.
Expressions never have reference type because any referenceness is adjusted away "prior to any further analysis".
* This isn't a merely technical distinction: per the direction of core issue 232 and core issue 453, there are valid dereference expressions you can write where binding its result to a reference would cause undefined behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With