什么是右值、左值、xvalues、glvalues 和 prvalues? [英] What are rvalues, lvalues, xvalues, glvalues, and prvalues?

查看:27
本文介绍了什么是右值、左值、xvalues、glvalues 和 prvalues?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++03 中,表达式是右值左值.

In C++03, an expression is either an rvalue or an lvalue.

在 C++11 中,表达式可以是:

In C++11, an expression can be an:

  1. 右值
  2. 左值
  3. xvalue
  4. glvalue
  5. 右值

两个类别变成了五个类别.

Two categories have become five categories.

  • 这些新的表达类别是什么?
  • 这些新类别与现有的右值和左值类别有何关联?
  • C++0x 中的右值和左值类别是否与 C++03 中的相同?
  • 为什么需要这些新类别?WG21 众神只是想迷惑我们只是凡人吗?
  • What are these new categories of expressions?
  • How do these new categories relate to the existing rvalue and lvalue categories?
  • Are the rvalue and lvalue categories in C++0x the same as they are in C++03?
  • Why are these new categories needed? Are the WG21 gods just trying to confuse us mere mortals?

推荐答案

我想这个文档可以作为一个不那么简短的介绍:n3055

I guess this document might serve as a not so short introduction : n3055

整个屠杀从移动语义开始.一旦我们有了可以移动不可复制的表达式,突然容易掌握的规则要求区分可以移动的表达式,以及向哪个方向移动.

The whole massacre began with the move semantics. Once we have expressions that can be moved and not copied, suddenly easy to grasp rules demanded distinction between expressions that can be moved, and in which direction.

我根据草稿推测,r/l 值区别保持不变,仅在移动事物变得混乱的情况下.

From what I guess based on the draft, the r/l value distinction stays the same, only in the context of moving things get messy.

他们需要吗?如果我们希望放弃新功能,可能不会.但是为了实现更好的优化,我们可能应该接受它们.

Are they needed? Probably not if we wish to forfeit the new features. But to allow better optimization we should probably embrace them.

引用 n3055:

  • 一个左值(所谓的,历史上,因为左值可能出现在作业的左侧表达式)指定一个函数或一个东西.[示例:如果 E 是一个指针类型的表达式,然后*E是一个左值表达式,指的是E 的对象或函数点.再举一个例子,调用一个函数的结果返回类型是左值引用是一个左值.]
  • 一个xvalue(一个到期"值)也指对象,通常接近其末尾生命周期(这样它的资源可以移动,例如).一个 xvalue 是某些类型的结果涉及右值的表达式参考.[示例:调用一个函数的结果返回类型是一个右值引用是一个 xvalue.]
  • glvalue(广义"左值)是一个左值xvalue.
  • 一个右值(所谓的,从历史上看,因为右值可以出现在右侧赋值表达式)是一个 xvalue,一个临时对象或其子对象,或者是一个值不与对象相关联.
  • Aprvalue(纯"右值)是一个右值这不是 xvalue.[示例:调用一个函数的结果返回类型不是引用是一个右值]
  • An lvalue (so-called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [Example: If E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue.]
  • An xvalue (an "eXpiring" value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references. [Example: The result of calling a function whose return type is an rvalue reference is an xvalue.]
  • A glvalue ("generalized" lvalue) is an lvalue or an xvalue.
  • An rvalue (so-called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object.
  • A prvalue ("pure" rvalue) is an rvalue that is not an xvalue. [Example: The result of calling a function whose return type is not a reference is a prvalue]

有问题的文档是这个问题的重要参考,因为它显示了由于引入新命名法而导致标准发生的确切变化.

The document in question is a great reference for this question, because it shows the exact changes in the standard that have happened as a result of the introduction of the new nomenclature.

这篇关于什么是右值、左值、xvalues、glvalues 和 prvalues?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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