Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

c++ - 什么是rvalue,lvalue,xvalue,glvalue和prvalue?(What are rvalues, lvalues, xvalues, glvalues, and prvalues?)

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

(在C ++ 03中,表达式是rvaluelvalue 。)

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

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

  1. rvalue

    (右值)

  2. lvalue

    (左值)

  3. xvalue

    ()

  4. glvalue

    (总值)

  5. prvalue

    (前值)

Two categories have become five categories.

(两个类别已变成五个类别。)

  • 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?

    (C ++ 0x中的rvalue和lvalue类别与C ++ 03中的类别相同吗?)

  • Why are these new categories needed?

    (为什么需要这些新类别?)

    Are the WG21 gods just trying to confuse us mere mortals?

    (WG21众神只是在试图使我们仅仅是凡人吗?)

  ask by James McNellis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(我猜这个文档可能只是一个简短的介绍: 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.

(一旦我们有了可以移动且不能复制的表达式,突然容易掌握的规则就要求在可以移动的表达式之间以及在哪个方向上进行区分。)

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.

(根据我对草案的猜测,r / l值的区别保持不变,只是在移动事物变得混乱的情况下。)

Are they needed?

(他们需要吗?)

Probably not if we wish to forfeit the new features.

(如果我们希望放弃这些新功能,可能不会。)

But to allow better optimization we should probably embrace them.

(但是为了更好的优化,我们可能应该接受它们。)

Quoting n3055 :

(引用n3055)

  • 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.

    ([示例:如果E是指针类型的表达式,那么*E是一个左值表达式,它引用E指向的对象或函数。)

    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).

    (xvalue (“ eXpiring”值)也指对象,通常在其生命周期快要结束时(例如,可以移动其资源)。)

    An xvalue is the result of certain kinds of expressions involving rvalue references.

    (xvalue是某些包含rvalue引用的表达式的结果。)

    [Example: The result of calling a function whose return type is an rvalue reference is an xvalue.]

    ([示例:调用返回类型为rvalue引用的函数的结果为xvalue。)

  • A glvalue (“generalized” lvalue) is an lvalue or an xvalue .

    (甲glvalue(“广义”左值)是左值x值 。)

  • 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.

    (一个右值 (所谓的,历史上,因为右值可能出现在赋值表达式的右侧)是一个x值,其临时对象或子对象或与对象不相关的值。)

  • A prvalue (“pure” rvalue) is an rvalue that is not an xvalue.

    (prvalue (“纯” rvalue)是不是xvalue的rvalue。)

    [Example: The result of calling a function whose return type is not a reference is a prvalue]

    ([示例:调用返回类型不是引用的函数的结果是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.

(所讨论的文档对该问题提供了很好的参考,因为它显示了由于引入了新术语而导致的标准的确切变化。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...