138: Data Types: Lvalues And Rvalues.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

You’ll sometimes come across lvalues and rvalues maybe when trying to understand compiler error messages or when reading docs. A basic understanding will go a long way to making sense of them. The names look strange, lvalue and rvalue. They started out, as I always understood them, to identify what things could go on the left side of an assignment and what things could go on the right side. If you declare an int variable called height and later assign it the value 2, then your code will say height is assigned 2. In this example, height appears to the left of the assignment operator and the number 2 integer literal is on the right. So height is an lvalue and 2 is an rvalue. It turns out that there are actually two main properties that are important for variables and even other expression types. Does it have an identity? And is it moveable? What do these mean? Anytime you have two properties, you’ll have four possible ways they can be combined. One of the combinations in this case is just not needed though. That combination is whenever an object has no identity and cannot be moved. The language designers felt that this combination serves no purpose. So it’s gone. That leaves the following three combinations: If it’s identifiable and not moveable, then this is the traditional, in-depth meaning of an lvalue. If it’s not identifiable but is moveable, then this is what’s called a pure rvalue. Or a prvalue. If it’s both identifiable and moveable, then all I can say here is you need to be careful. This is called an xvalue. And the language designers left the meaning of the letter x unspecified. Think of it as an expert value. If you’re dealing with xvalues, then you need to know what you’re doing. What happened to rvalues? Well, rvalues are now defined to be more of a property. Rvalues map to the moveable property and include both xvalues and prvalues. And the other property, identifiable, is now called general lvalues or glvalues and includes both lvalues and xvalues. Listen to the full episode or read the full transcript below for more. Transcript You’ll sometimes come across lvalues and rvalues maybe when trying to understand compiler error messages or when reading docs. A basic understanding will go a long way to making sense of them. The names look strange, lvalue and rvalue. They started out, as I always understood them, to identify what things could go on the left side of an assignment and what things could go on the right side. If you declare an int variable called height and later assign it the value 2, then your code will say height is assigned 2. Notice I try to avoid saying height equals 2. Because that can get confused with an equality test to see if the current value of height is 2 or not. It’s a good habit for you to follow. In this example, height appears to the left of the assignment operator and the number 2 integer literal is on the right. So height is an lvalue and 2 is an rvalue. You can’t reverse these and try to say that the number 2 is assigned height. That doesn’t make any sense. And if you try to do something like this, your compiler is likely to give you an error message about how the number 2 cannot be an lvalue. Things get a little more complicated so don’t worry if the rest of this episode doesn’t make sense. You’ve already got the major understanding just knowing that some things can be assigned new values and some things can’t. In case you thing that any simple int variable is automatically an lvalue, well that’s not always the case. Some languages like C++ allow you to declare variables to be const. Then they can’t be changed. While researching this episode, I found reference to another possible meaning of lvalue and rvalue. I don’t know if this is correct but it does make sense and I hope will give you another way to think about this topic. Back in the days before C++ and likely b