参考 https://en.cppreference.com/w... 下面,这一行:
xvalue
a[n], the built-in subscript expression, where one operand is an array rvalue;
那么什么是array rvalue呢?
参考 https://en.cppreference.com/w... 下面说到
对于 expr1[expr2]
When applied to an array, the subscript expression is an lvalue if the array is an lvalue, and an xvalue if it isn't (since C++11).
什么时候数组不是一个左值呢?
参考下面代码
struct S
{
int iarr[10];
int i;
std::string sarr[10];
std::string s;
};
S get() { return S{}; }
template<class T>
void foo(T&& v) {}
int main()
{
foo(get().iarr[0]);//int&
foo(get().i); //int&&
foo(get().sarr[0]);//std::string&
foo(get().s); //std::string&&
return 0;
}
根据cppreference的说法:
-
get()
返回的是prvalue -
get().m
应该是xvalue -
get().m[n]
是xvalue吗?
简单的,(int[3]){1,2,3}此时这个就是一个右值数组,下标访问返回的为右值(xvalue),但通过偏移间接寻址后得到的恒为左值(以上写法在gcc和clang下正确),还有种写法为using arryTY = int[3];arryTY{1,2,3}