gpt4 book ai didi

c++ - "xvalue has identity",为什么我不能计算它的地址

转载 作者:行者123 更新时间:2023-12-01 14:23:23 26 4
gpt4 key购买 nike

here弄清楚

  • “具有身份”:地址,一个指针,用户可以判断两个拷贝是否相同。
  • xvalue:具有标识并且可以从中移动(例如将左值转换为右值引用的结果。

  • here弄清楚

    以下表达式是 xvalue 表达式:
    3. a.m,对象表达式的成员,其中a为右值,m为非引用类型的非静态数据成员;

    here举个例子
    struct X { int n; };

    X{4}; // prvalue: does not have an identity

    X{4}.n; // xvalue: does have an identity and denotes resources
    // that can be reused

    但是当我尝试使用 &cout X{4}.n的地址,编译器拒绝编译。
    cout << &X{4}.n << endl;    // error C2102:  '&' requires l-value

    这是否意味着我永远无法获得 xvalue 的地址?

    最佳答案

    这是标准所说的:[expr.unary.op]/3

    The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id.



    所以不,你不能使用 & 来获取右值的地址。运算符(operator)。

    关于c++ - "xvalue has identity",为什么我不能计算它的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59332607/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com