c++ value category

2年前 (2022) 程序员胖胖胖虎阿
110 0 0

微软官方解释


示例

c++ value category

glvalue

g是generalize的意思,glvalue是个抽象的概念,是lvalue和xvalue的并集

lvalue

左值字面意思是可以放在左边的值,包括成员变量和能返回一个左值的函数

int main()
{
    int i, j, *p;

    // Correct usage: the variable i is an lvalue and the literal 7 is a prvalue.
    i = 7;

    // Incorrect usage: The left operand must be an lvalue (C2106).`j * 4` is a prvalue.
    7 = i; // C2106
    j * 4 = 7; // C2106

    // Correct usage: the dereferenced pointer is an lvalue.
    *p = i;

    // Correct usage: the conditional operator returns an lvalue.
    ((i < 3) ? i : j) = 7;

    // Incorrect usage: the constant ci is a non-modifiable lvalue (C3892).
    const int ci = 7;
    ci = 9; // C3892
}

xvalue

x是expiring,xvalue意思是即将走完生命周期的值,xvalue有地址但程序无法进入,例如函数返回右值引用的表达式,或者使用move语句这类传入的参数,这种参数快死掉了,就叫做xvalue,例如1 + 1, move(a), static_cast<>({xvalue})

rvalue

右值可以是纯右值,也可以是xvalue

prvalue

不是xvalue的右值,即没有地址且程序无法进入,例如字符,函数返回的非引用类型

版权声明:程序员胖胖胖虎阿 发表于 2022年10月10日 上午8:08。
转载请注明:c++ value category | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...