传统的C语言:左值可能放在等号左边,右值只能放在等号右边
在 C++ 中,左值也不一定能放在等号左边;右值也可能放在等号左边
#include <iostream>int main(){const int val = 7;//val = 9;int val1 = val;} //显然这里val不可以作为左值使用,也就是左值也不一定能放在等号左边
#include <iostream>struct MyStruct{};int main(){MyStruct x = MyStruct{}; //这里MyStruct{}作为prvalueMyStruct{} = MyStruct{} ; //作为临时对象,并不能找到相应的内存}//没有报错
所有的划分都是针对表达式的,不是针对对象或数值
glvalue :标识一个对象、位或函数
prvalue :用于初始化对象或作为操作数
xvalue :表示其资源可以被重新使用 
#include <iostream>#include <vector>void fun(std::vector<int>&& param){}int main(){std::vector<int> x;fun(std::move(x));}//lvalue --> xvalue
左值与右值的转换
左值转化为右值:
#include <iostream>int main(){int val = 6;int val1 = val;}
临时具体化(temporary intialization)
decltype
语法:
decltype ( entity ) (1) (since C++11)decltype ( expression ) (2) (since C++11)
用法(针对表达式):
a) if the value category of expression is xvalue, then decltype yields T&&;b) if the value category of expression is lvalue, then decltype yields T&;c) if the value category of expression is prvalue, then decltype yields T.
a)
Source:#include <iostream>#include <utility>int main(){int x;decltype(std::move(x)) y = std::move(x);}Insight:#include <iostream>#include <utility>int main(){int x;int && y = std::move(x);}
b)
Source:#include <iostream>int main(){int x = 6;decltype((x)) y = x; // 注意这里不能写成 decltype((x)) y;因为此时已经转化为引用类型} // error: declaration of reference variable 'y' requires an initializerInsight:#include <iostream>int main(){int x = 6;int & y = x;}
c)
Source:#include <iostream>int main(){decltype(4) x; //显然4是prvalue}Insight:#include <iostream>int main(){int x;}
