Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, March 11, 2016

What are rvalues, lvalues, xvalues, glvalues, and prvalues?

What are rvalues, lvalues, xvalues, glvalues, and prvalues?


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

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

  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?
  • Why are these new categories needed? Are the WG21 gods just trying to confuse us mere mortals?

Answer by Kornel Kisielewicz for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


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.

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.

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

Answer by dirkgently for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


What are these new categories of expressions?

The FCD (n3092) has an excellent description:

? 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. ?end example ]

? 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 (8.3.2). [ Example: The result of calling a function whose return type is an rvalue reference is an xvalue. ?end example ]

? 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 expressions) is an xvalue, a temporary object (12.2) 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 value of a literal such as 12, 7.3e5, or true is also a prvalue. ?end example ]

Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category. [ Note: The discussion of each built-in operator in Clause 5 indicates the category of the value it yields and the value categories of the operands it expects. For example, the built-in assignment operators expect that the left operand is an lvalue and that the right operand is a prvalue and yield an lvalue as the result. User-defined operators are functions, and the categories of values they expect and yield are determined by their parameter and return types. ?end note

I suggest you read the entire section 3.10 Lvalues and rvalues though.

How do these new categories relate to the existing rvalue and lvalue categories?

Again:

Taxonomy

Are the rvalue and lvalue categories in C++0x the same as they are in C++03?

The semantics of rvalues has evolved particularly with the introduction of move semantics.

Why are these new categories needed?

So that move construction/assignment could be defined and supported.

Answer by fredoverflow for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


How do these new categories relate to the existing rvalue and lvalue categories?

A C++03 lvalue is still a C++0x lvalue, whereas a C++03 rvalue is called a prvalue in C++0x.

Answer by Johannes Schaub - litb for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


C++03's categories are too restricted to capture the introduction of rvalue references correctly into expression attributes.

With the introduction of them, it was said that an unnamed rvalue reference evaluates to an rvalue, such that overload resolution would prefer rvalue reference bindings, which would make it select move constructors over copy constructors. But it was found that this causes problems all around, for example with Dynamic Types and with qualifications.

To show this, consider

int const&& f();    int main() {    int &&i = f(); // disgusting!  }  

On pre-xvalue drafts, this was allowed, because in C++03, rvalues of non-class types are never cv-qualified. But it is intended that const applies in the rvalue-reference case, because here we do refer to objects (= memory!), and dropping const from non-class rvalues is mainly for the reason that there is no object around.

The issue for dynamic types is of similar nature. In C++03, rvalues of class type have a known dynamic type - it's the static type of that expression. Because to have it another way, you need references or dereferences, which evaluate to an lvalue. That isn't true with unnamed rvalue references, yet they can show polymorphic behavior. So to solve it,

  • unnamed rvalue references become xvalues. They can be qualified and potentially have their dynamic type different. They do, like intended, prefer rvalue references during overloading, and won't bind to non-const lvalue references.

  • What previously was an rvalue (literals, objects created by casts to non-reference types) now becomes an prvalue. They have the same preference as xvalues during overloading.

  • What previously was an lvalue stays an lvalue.

And two groupings are done to capture those that can be qualified and can have different dynamic types (glvalues) and those where overloading prefers rvalue reference binding (rvalues).

Answer by sellibitze for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


I'll start with your last question:

Why are these new categories needed?

The C++ standard contains many rules that deal with the value category of an expression. Some rules make a distinction between lvalue and rvalue. For example, when it comes to overload resolution. Other rules make a distinction between glvalue and prvalue. For example, you can have a glvalue with an incomplete or abstract type but there is no prvalue with an incomplete or abstract type. Before we had this terminology the rules that actually need to distinguish between glvalue/prvalue referred to lvalue/rvalue and they were either unintentionally wrong or contained lots of explaining and exceptions to the rule a la "...unless the rvalue is due to unnamed rvalue reference...". So, it seems like a good idea to just give the concepts of glvalues and prvalues their own name.

What are these new categories of expressions? How do these new categories relate to the existing rvalue and lvalue categories?

We still have the terms lvalue and rvalue that are compatible with C++98. We just divided the rvalues into two subgroups, xvalues and prvalues, and we refer to lvalues and xvalues as glvalues. Xvalues are a new kind of value category for unnamed rvalue references. Every expression is one of these three: lvalue, xvalue, prvalue. A Venn diagram would look like this:

    ______ ______     /      X      \    /      / \      \   |   l  | x |  pr  |    \      \ /      /     \______X______/         gl    r  

Examples with functions:

int   prvalue();  int&  lvalue();  int&& xvalue();  

But also don't forget that named rvalue references are lvalues:

void foo(int&& t) {    // t is initialized with an rvalue expression    // but is actually an lvalue expression itself  }  

Answer by Nicol Bolas for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


Why are these new categories needed? Are the WG21 gods just trying to confuse us mere mortals?

I don't feel that the other answers (good though many of them are) really capture the answer to this particular question. Yes, these categories and such exist to allow move semantics, but the complexity exists for one reason. This is the one inviolate rule of moving stuff in C++11:

Thou shalt move only when it is unquestionably safe to do so.

That is why these categories exist: to be able to talk about values where it is safe to move from them, and to talk about values where it is not.

In the earliest version of r-value references, movement happened easily. Too easily. Easily enough that there was a lot of potential for implicitly moving things when the user didn't really mean to.

Here are the circumstances under which it is safe to move something:

  1. When it's a temporary or subobject thereof. (prvalue)
  2. When the user has explicitly said to move it.

If you do this:

SomeType &&Func() { ... }    SomeType &&val = Func();  SomeType otherVal{val};  

What does this do? In older versions of the spec, before the 5 values came in, this would provoke a move. Of course it does. You passed an rvalue reference to the constructor, and thus it binds to the constructor that takes an rvalue reference. That's obvious.

There's just one problem with this; you didn't ask to move it. Oh, you might say that the && should have been a clue, but that doesn't change the fact that it broke the rule. val isn't a temporary because temporaries don't have names. You may have extended the lifetime of the temporary, but that means it isn't temporary; it's just like any other stack variable.

If it's not a temporary, and you didn't ask to move it, then moving is wrong.

The obvious solution is to make val an lvalue. This means that you can't move from it. OK, fine; it's named, so its an lvalue.

Once you do that, you can no longer say that SomeType&& means the same thing everwhere. You've now made a distinction between named rvalue references and unnamed rvalue references. Well, named rvalue references are lvalues; that was our solution above. So what do we call unnamed rvalue references (the return value from Func above)?

It's not an lvalue, because you can't move from an lvalue. And we need to be able to move by returning a &&; how else could you explicitly say to move something? That is what std::move returns, after all. It's not an rvalue (old-style), because it can be on the left side of an equation. It is neither an lvalue nor an rvalue; it's a new kind of thing.

What we have is a value that you can treat as an lvalue, except that it is implicitly moveable from. We call it an xvalue.

Note that xvalues are what makes us gain the other two categories of values:

  • A prvalue is really just the new name for the previous type of rvalue, i.e. they're the rvalues that aren't xvalues.

  • The glvalues is the union of xvalues and lvalues in one group, because they do share a lot of properties in common.

So really, it all comes down to xvalues and the need to restrict movement to exactly and only certain places. Those places are defined by the rvalue category; prvalues are the implicit moves, and xvalues are the explicit moves (std::move returns an xvalue).

Answer by bruziuz for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


lvalue -- from practical point of view, something from left hand side of the equal operator. You can think that it is something from which you can take the address (variables, or lvalue reference). But it is not rigorous definition, e.g. as mentioned in comments bit field is also lvalue, but can not be addressable.

rvalue -- from practical point of view it is something from right hand side of the equal operator. You can think that it is something from which you can not take the address (unnamed temporary variables). Temporary objects can be pass to functions only by const reference. A good counter example what is rvalue, but has a name - this

By the way -- constructor of copy for auto_ptr has some strange signature in sense that it is not allowable to pass temporary auto_ptr to it. But you can. It is feature for most compilers, so you can safely use it. But with C++2003 you can not do it legaly!

xvalue -- temporary object will soon be destroyed. You should take data from it, because it will die very soon. Such reference in C++11 has && signature.

Overload function with xvalue Reference (A&&) has priority under constant reference (const A&)

prvalue -- pure rvalue. Real object.

glvalue -- it is lvalue or xvalue.

Answer by Dániel Sándor for What are rvalues, lvalues, xvalues, glvalues, and prvalues?


INTRODUCTION

ISOC++11 (officially ISO/IEC 14882:2011) is the most recent version of the standard of the C++ programming language. It contains some new features, and concepts, for example:

  • rvalue references
  • xvalue, glvalue, prvalue expression value categories
  • move semantics

If we would like to understand the concepts of the new expression value categories we have to be aware of that there are rvalue and lvalue references. It is better to know rvalues can be passed to non-const rvalue references.

int& r_i=7; // compile error  int&& rr_i=7; // OK  

We can gain some intuition of the concepts of value categories if we quote the subsection titled Lvalues and rvalues from the working draft N3337 (the most similar draft to the published ISOC++11 standard).

3.10 Lvalues and rvalues [basic.lval]

1 Expressions are categorized according to the taxonomy in Figure 1.

  • 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. ?end example ]
  • 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 (8.3.2). [ Example: The result of calling a function whose return type is an rvalue reference is an xvalue. ?end example ]
  • 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 (12.2) 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 value of a literal such as 12, 7.3e5, or
    true is also a prvalue. ?end example ]

Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category.

But I am not quite sure about that this subsection is enough to understand the concepts clearly, because "usually" is not really general, "near the end of its lifetime" is not really concrete, "involving rvalue references" is not really clear, and "Example: The result of calling a function whose return type is an rvalue reference is an xvalue." sounds like a snake is biting its tail.

PRIMARY VALUE CATEGORIES

Every expression belongs to exactly one primary value category. These value categories are lvalue, xvalue and prvalue categories.

lvalues

The expression E belongs to the lvalue category if and only if E refers to an entity that ALREADY has had an identity (address, name or alias) that makes it accessible outside of E.

#include     int i=7;    const int& f(){      return i;  }    int main()  {      std::cout<<&"www"<

xvalues

The expression E belongs to the xvalue category if and only if it is

? the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to the type of object being returned, or

int&& f(){      return 3;  }    int main()  {      f(); // The expression f() belongs to the xvalue category, because f() return type is an rvalue reference to object type.        return 0;  }  

? a cast to an rvalue reference to object type, or

int main()  {      static_cast(7); // The expression static_cast(7) belongs to the xvalue category, because it is a cast to an rvalue reference to object type.      std::move(7); // std::move(7) is equivalent to static_cast(7).        return 0;  }  

? a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or

struct As  {      int i;  };    As&& f(){      return As();  }    int main()  {      f().i; // The expression f().i belongs to the xvalue category, because As::i is a non-static data member of non-reference type, and the subexpression f() belongs to the xvlaue category.        return 0;  }  

? a pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

Note that the effect of the rules above is that named rvalue references to objects are treated as lvalues and unnamed rvalue references to objects are treated as xvalues; rvalue references to functions are treated as lvalues whether named or not.

#include     struct As  {      int i;  };    As&& f(){      return As();  }    int main()  {      f(); // The expression f() belongs to the xvalue category, because it refers to an unnamed rvalue reference to object.      As&& rr_a=As();      rr_a; // The expression rr_a belongs to the lvalue category, because it refers to a named rvalue reference to object.      std::ref(f); // The expression std::ref(f) belongs to the lvalue category, because it refers to an rvalue reference to function.        return 0;  }  

prvalues

The expression E belongs to the prvalue category if and only if E belongs neither to the lvalue nor to the xvalue category.

struct As  {      void f(){          this; // The expression this is a prvalue expression. Note, that the expression this is not a variable.      }  };    As f(){      return As();  }    int main()  {      f(); // The expression f() belongs to the prvalue category, because it belongs neither to the lvalue nor to the xvalue category.        return 0;  }  

MIXED VALUE CATEGORIES

There are two further important mixed value categories. These value categories are rvalue and glvalue categories.

rvalues

The expression E belongs to the rvalue category if and only if E belongs to the xvalue category, or to the prvalue category.

Note that this definition means that the expression E belongs to the rvalue category if and only if E refers to an entity that has not had any identity that makes it accessible outside of E YET.

glvalues

The expression E belongs to the glvalue category if and only if E belongs to the lvalue category, or to the xvalue category.

A PRACTICAL RULE

Scott Meyer has published a very useful rule of thumb to distinguish rvalues from lvalues.

  • If you can take the address of an expression, the expression is an lvalue.
  • If the type of an expression is an lvalue reference (e.g., T& or const T&, etc.), that expression is an lvalue.
  • Otherwise, the expression is an rvalue. Conceptually (and typically also in fact), rvalues correspond to temporary objects, such as those returned from functions or created through implicit type conversions. Most literal values (e.g., 10 and 5.3) are also rvalues.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.