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

标签 c++ c++11

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/

    相关文章:

    c++ - 如何使用 clang 在 Mac OS X 上获得新的 C++ 线程支持?

    c++ - const decltype(*std::begin(container))& val 不会使 val const?

    multithreading - 为什么在 GCC 和 Clang 中使用 std::thread 需要 -pthread?

    C++11 构造函数

    c++ - 将数字表示为两个三角数之和

    c++ - 构建与 Dreamweaver 兼容的 C DLL

    c++ - 该函数不会向我的 bst 树添加任何内容

    c++ - 为什么 std::move 使用 std::remove_reference?

    c++ - UnitTest++创建cmd窗口,无法关闭

    c++ - 如何将std::get用作boost-multi-index容器键的global_fun?