在C ++中常量为右值(11) [英] Constant as rvalue in C++(11)

查看:287
本文介绍了在C ++中常量为右值(11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 const int C ++中的R值(11)?我认为R值是任何东西,不能在左手边,常数满足这一点。此代码失败:

  int f(int& x){return 100; } 

void g(){
const int x = 1;
f(x);
}

错误:类型'int&&'从引用的初始化
类型'const int'

p>

解决方案

好的,有三类表达式 1


  1. 表示具有身份且不能从其中移动的对象的那些对象;

  2. 表示具有身份的对象并可以从;

  3. 代表那些没有身份并可以从中移动的对象;

第一个叫做左值,第二个是xvalue,第三个是prvalues。如果我们把左值和x值放在一起,我们有glvalues。 Glvalues是表示具有身份的对象的所有表达式。如果我们把xvalues和prvalues放在一起我们有右值。 Rvalues是表示可以移动的对象的所有表达式。



问题中的表达式 x 是glvalue :可以写& x ,所以对象显然有一个标识。



我们可以从这个表达式?这个对象是否即将过期?不它不是。它只会在当前表达式后的某个时间到期。这意味着它不能移动。



所有这些名称都有点令人困惑,因为C ++中的左值和右值不再意味着它们在C语言中的意思。 C ++的意义与在 2 的左边或右边完全无关。



我个人喜欢使用<一个href =http://stroustrup.com/terminology.pdf>本文由Bjarne:iM值(而不是lvalues),im值(而不是xvalues),Im值(而不是prvalues),i值(而不是glvalues)和m值(而不是rvalues)。






1 这里有一个身份是指其地址可以; 可以从意味着它即将到期,或者是由于它的临时性,或者因为程序员在类型系统中通过调用 std :: move 或类似的东西。



2 你可以在赋值左侧有右值: std :: vector< int>(17)= std :: vector< int>(42)是有效的表达式,即使它是无用的。


Why const int is not an R-value in C++(11)? I thought that R-value was 'anything' which cannot be on the left hand side and constants fulfil that. This code fails:

int f(int && x) { return 100; }

void g() {
  const int x = 1;
  f(x);
}

error: invalid initialization of reference of type ‘int&&’ from expression
of type ‘const int’

解决方案

Ok, there are three categories of expressions1:

  1. those that represent objects that have an identity and cannot be moved from;
  2. those that represent objects that have an identity and can be moved from;
  3. those that represent objects that do not have an identity and can be moved from;

The first ones are called lvalues, the second ones are xvalues, and the third ones are prvalues. If we put lvalues and xvalues together, we have glvalues. Glvalues are all expressions that represent objects with an identity. If we put xvalues and prvalues together we have rvalues. Rvalues are all expressions that represent objects that can be moved.

The expression in question, x, is a glvalue: one can write &x, so the object clearly has an identity.

Can we move from this expression? Is this object about to expire? No, it is not. It only expires sometime after the current expression. That means it cannot be moved from. That makes it an lvalue.

All these names can be a bit confusing because lvalue and rvalue in C++ no longer mean what they meant in their C origins. The C++ meaning is completely unrelated to being on the left or right side of assignment2.

Personally, I prefer to use the terminology from this paper by Bjarne: iM-values (instead of lvalues), im-values (instead of xvalues), Im-values (instead of prvalues), i-values (instead of glvalues), and m-values (instead of rvalues). That is not the terminology that the standard uses, unfortunately.


1 Here "have an identity" means "its address can be taken"; "can be moved from" means that it is about to expire, either due to its temporary nature, or because the programmer made that explicit in the type system by calling std::move or something similar.

2 You can have rvalues on the left side of assignment: std::vector<int>(17) = std::vector<int>(42) is a valid expression, even if it is useless.

这篇关于在C ++中常量为右值(11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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