为什么可以const lvalue引用参考可变的右值引用? [英] Why Can const lvalue References Refer to Mutable rvalue References?

查看:407
本文介绍了为什么可以const lvalue引用参考可变的右值引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,const Lvalue引用可以使用可变的右值引用初始化。然后,右值引用的值可以改变,产生一个可见的突变,常量值指的是什么。这里是一个例子:

In C++11 a const lvalue reference can be initialized with a mutable rvalue reference. The value referred to by the rvalue can then change producing a visible mutation to what the const lvalue is referring to. Here is an example:

int && rval = 3;
const int & lval = rval;

cout << "lval = " << lval << endl;
cout << "rval = " << rval << endl;

rval ++;

cout << "lval = " << lval << endl;

输出(来自clang 3.2和gcc 4.8.2都使用-std = c ++ 11):

Output (from clang 3.2 and gcc 4.8.2 both with -std=c++11):

lval = 3
rval = 3
lval = 4

我会猜测的原因是指示不能修改通过左值引用,但它可以通过右值引用修改。但是,我不明白为什么const lvalue被允许引用一个可变对象。

I would guess the reason for this is that the referent cannot be modified through the lvalue reference but it can be modified through the rvalue reference. But, I don't understand why a const lvalue is allowed to refer to a mutable object.

有人可以解释这个的理由,并给出最好的做法,当处理这种情况。

Can someone explain the rationale for this and give best practices for when dealing with such a situation. Also, are there other similar examples where constness can be subverted?

推荐答案


但是,我不' t理解为什么const lvalue被允许引用一个可变对象

But, I don't understand why a const lvalue is allowed to refer to a mutable object

有一个 const 引用的东西只意味着你不能通过该引用修改对象。

Having a const reference to something only means that you can't modify the object through that reference. It doesn't mean that nobody is allowed to change the object ever.

我们假设你有一个函数:

Let's say you have a function:

void f(Foo const& bar);

函数声明调用者不会修改 bar 。而已。没有什么比这更多,没有什么比。它不说什么发生在 bar ,而 f 正在执行(例如在另一个线程);

The function is declaring to the caller that it will not modify bar. That's it. Nothing more than that, nothing less than that. It says nothing about what happens to bar while f is executing (e.g. in another thread); the language doesn't have a way to express constraints like that.

最后一位:

int && rval = 3;

rval / strong>。它有一个名称,可以在作业的左侧,因为您的代码清楚地演示。右值引用和左值引用之间的区别是,右值引用可以将绑定到右值 - 而不是它们本身是右值。

rval is an lvalue. It has a name and can be on the left side of an assignment, as your code clearly demonstrates. The difference between an rvalue reference and an lvalue reference is that rvalue references can bind to rvalues -- not that they themselves are rvalues.

是为什么要像

void foo(unique_ptr<X>&& x)
{
    aVector.emplace_back(x);
}

x 被声明为右值引用,但在 foo 中,它有一个名称,可以在赋值,是一个左值。将它移动到其他东西需要使用 move 或类似的:

does not compile. x is declared as an rvalue reference, but inside foo it has a name and can be on the left side of an assignment, and is an lvalue. Moving it into something else requires using move or similar:

void foo(unique_ptr<X>&& x)
{
    aVector.emplace_back(move(x)); // Ok
}

这篇关于为什么可以const lvalue引用参考可变的右值引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆