c++标准库
右值引用
为了解决不必要的复制,并且允许perfect forwarding;
当赋值操作(=号)的右边是一个右值rvalue,那么左边的对象可以从右边的对象“偷”resources,即允许move语义;
Lvalue: 可以出现在operator= 左侧者
Rvalue: 只能出现在operator=右侧者
临时对象就是一种右值
| 12
 3
 4
 5
 
 | int foo() { return 5; }...
 int x = foo();
 int* p = &foo();
 foo() = 7;
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 
 | template <typename T>class Myclass{
 public:
 Myclass();
 Myclass(const T& t);
 
 Myclass(const T&& t) noexcept;
 }
 
 | 
右值使用示例
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | void process(int& i) { cout << "process(int&):" << i << endl; }void process(int&& i) { cout << "process(int&&):" << i << endl; }
 void forward(int&& i) {
 cout << "forward(int&&):" << i << ",";
 process(i);
 }
 
 int a=0;
 process(a);
 process(1);
 process(move(a));
 
 forward(2);
 
 forward(move(a));
 forward(a);
 
 const int& b = 1;
 process(b);
 process(move(b));
 
 int& x(5);
 process(move(x));
 
 |