Does '&' operator return r-value?

int num = 10;
int& ref = num;
int* ptr = #
int num2 = &ptr;

In this code, ref and *ptr are l-values.
Then what about '&num'? Is it l-value or r-value? I'm very confused about this.
It's easy enough to test, put in in an l-value context and see what happens.
1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat foo.cpp
#include <iostream>
using namespace std;

int main ( ) {
  int num = 10;
  &num = 20;
}
$ g++ foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:6:8: error: lvalue required as left operand of assignment
   &num = 20;
        ^
&num is a temporary value; an address, a pointer-to-int. It's an r-value.
A prvalue (a "pure" rvalue).

For the expression &num:
operand: lvalue of type int
result: prvalue of type 'pointer to int' (int*)

For the expression &ptr:
operand: lvalue of type 'pointer to int'
result: prvalue of type 'pointer to pointer to int' (int**)
1
2
// int num2 = &ptr ; // *** error   
int** pptr = &ptr ; // fine 
Topic archived. No new replies allowed.