简体   繁体   English

为什么使用临时 object 访问时引用类型是左值?

[英]Why is a reference type an lvalue when accessed with a temporary object?

Why does assigning a value to a reference variable being accessed using a temporary object work, but not for a non-reference type?为什么将值分配给使用临时 object 访问的引用变量有效,但不适用于非引用类型?

class a
{
    public:
        int m;
        int &n;
        a():m(2),n(m)
        {
            cout<< "A's constructor"<<endl;
        }
};

int main()
{
    // a().m = 6; // this gives an error that a temporary object is being used 
                  // as an lvalue
    a().n = 20;   // But this line works

    return 0;
}

But is a().n truely a temporary?但是a().n真的是暂时的吗? Consider this code:考虑这段代码:

class a
{
    public:
        int m;
        int &n;
        a():m(2),n(m)
        {
            cout<< "A's constructor"<<endl;
        }

        a(int& _n):m(2),n(_n)
        {
            cout<< "A's constructor"<<endl;
        }
};

int main()
{
    a().n = 20;   // (1)

    int n = 0;
    a(n).n        // (2)

    return 0;
}

The line (2) clearly shows that .n is not a temporary.(2)行清楚地表明.n不是临时的。 It must not be, since it's a reference to the local n variable.它一定不是,因为它是对局部n变量的引用。

But then, the compiler cannot know what n will refer.但是,编译器无法知道n将引用什么。 One could even do n(rand_bool()? m: _n) and it must work.甚至可以做n(rand_bool()? m: _n)并且它必须工作。

The compile instead uses the type system to know what should be assigned or not.相反,编译使用类型系统来知道应该分配什么。

For example, the literal 9 is a pr-value of type int .例如,文字9int类型的 pr 值。 You can't assign to it:你不能分配给它:

9 = 8; // nope

In your code, a() is a prvalue or type a .在您的代码中, a()是纯右值或类型a All of its value member also are.它的所有价值成员也都是。 This is why a().m won't work.这就是a().m不起作用的原因。 m is a prvalue. m是纯右值。

But, a().n is an lvalue because n is a lvalue reference.但是, a().n是一个左值,因为n是一个左值引用。 No matter to which variable it points to.不管它指向哪个变量。

a().n = 20;

works since n is a lvalue reference type.因为n是左值引用类型,所以有效。 The compiler does not know that n is a reference to m in the implementation.编译器不知道n在实现中是对m的引用。 It assumes that n is a valid lvalue reference and hence accepts that line.它假定n是一个有效的左值引用,因此接受该行。

In theory, when you assign to a().n , you could be assigning to a variable that lives independent of the life of a() .理论上,当您分配给a().n时,您可以分配给一个独立于a()生命周期的变量。 The compiler has no way of assessing that and will be in the way of the programmer if it didn't accept that line.编译器无法评估这一点,如果它不接受该行,它将妨碍程序员。 Imagine the use case below:想象一下下面的用例:

// Global variable.
int gv;

class a
{
    public:
        int m;
        int &n;
        a():m(2), n(gv)  // n is a reference to the global variable.
        {
            cout<< "A's constructor"<<endl;
        }
};

int main()
{
    a().n = 20;   // Changes gv. It is a valid operation.
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将左值分配给右值引用时会发生什么? 没有破坏临时对象? - What happen when a lvalue assigned to a rvalue reference? No destruction of the temporary object? 从临时对象返回左值引用 - Return lvalue reference from temporary object 将临时绑定到左值引用 - Binding temporary to a lvalue reference 左值引用返回临时对象的成员函数的悬空引用吗? - Is the lvalue reference return of a member function of a temporary object a dangling reference? 类型“”的非常量左值引用不能绑定到类型“ *”的临时对象 - non-const lvalue reference to type '' cannot bind to a temporary of type ' *' 构造的临时对象是左值吗? - Constructed temporary object are lvalue? 返回常量左值引用右值临时? 为什么这样做? - Returning const lvalue reference to rvalue temporary? Why does this work? 为什么右值引用类型的模板参数可以绑定到左值类型? - Why a template argument of a rvalue reference type can be bound to a lvalue type? 对“ int”类型的非常量左值引用不能绑定到“ int”类型的临时对象 - non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int' 无法解决错误:类型为&#39;bool&#39;的非常量左值引用无法绑定至类型为&#39;bool&#39;的临时目录 - Can't resolve Error: non-const lvalue reference to type 'bool' cannot bind to a temporary of type 'bool'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM