-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreference.cpp
More file actions
47 lines (39 loc) · 1.29 KB
/
reference.cpp
File metadata and controls
47 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Created by light on 19-12-14.
//
#include <functional>
#include <iostream>
using namespace std;
// xvalue
int&& f() {
return 3;
}
struct As {
int i;
};
As&& ff() {
return As();
}
int main() {
// lvalue
int x = 0;
cout << "(x).addr = " << &x << endl;
cout << "(x = 1).addr = " << &(x = 1) << endl;
cout << "(++x).addr = " << &++x << endl;
cout << "(cout << ' ').addr=" << &(cout << ' ') << endl;
cout << "(\"hello\").addr=" << &("hello") << endl;
// rvalue
cout << true << endl;
// xvalue
f(); // The expression f() belongs to the xvalue category, because f() return
// type is an rvalue reference to object type.
cout << "static_cast<int&&>(7):" << static_cast<int&&>(7)
<< endl; // The expression static_cast<int&&>(7) belongs to the
// xvalue category, because it is a cast to an rvalue
// reference to object type.
std::move(7); // std::move(7) is equivalent to static_cast<int&&>(7).
cout << "ff().i = " << ff().i; // The expression f().i belongs to the xvalue category, because
// As::i is a non-static data member of non-reference type, and
// the subexpression f() belongs to the xvlaue category.
return 0;
}