Anders Schau Knatten: lvalues, rvalues, glvalues, prvalues, xvalues, help!

OsloCpp2,149 words

Full Transcript

L values or values to jail values PR values X values help so who knows what all of these value categories mean okay I saw one hand it's a bit dark but I don't think they were so I think the question you would be asking is you know how far down this line you get when you give up and have to look it up yeah okay so like yeah most people don't know what all these are but if I do like this like who has at least some idea what what this mean right so it used to be simple back in the good old days with like L values and R values um before C2 plus 11 we only had two value categories we had L values and we had R values um and it basically meant an L value is anything that has an identity and an R value is anything that doesn't so an L value is an expression that is an actual object it has a name it's it's like in there in memory you can refer to it others can like refer to this name yeah and then and our value doesn't have an identity like you could still there could still be some memory like there it could be a temporary in in memory Zoomer but so this is a like big uh so far what do you think are the good old days so if you have a stud string s here then you do like this like s well that has an identity that's this thing here so it has a name uh you can like talk about this thing as it has an identity but this is the string bar here that doesn't have an identity it's just like a temporary kind of thing so you can't really name it so it's it's yeah it's uh it doesn't have an identity and like these L values are so named because they typically appear on the left hand side of the expression so an L value and you have the R value here uh because this wouldn't make oh wait that does make sense you can actually put them in the other the other order so so this is this is now an R value but it's on the left hand side and this is an L value on the right hand side this doesn't really it doesn't happen in practice a lot but it's perfectly fine um but like the L values typically are the things on the left hand side that you can assign to our values are typically on the right hand side and you don't typically assign that as obviously before 17 at least what it does is that actually I'll get back to it after that but I I'll it's a it has to do with temporary materialization and and discarded value Expressions so again we talk about it later um uh yes but then um simple plus 11 uh introduced move semantics and we want to be able to overload on uh L value versus r value so if we have function s f that takes a string if we take an L value here we we don't want to move from this s here because like someone else could be using this right but this one we really want to move from we don't want to make a copy of a temporary that's pointless right so if we want to be able to to overload based on based on this we have to be able to distinguish between these um so then well if you have an L value it has an identity someone else might be referring to it somewhere so you can't really move from it if you have an R value it doesn't have an identity you can move from it just fine but what happens if you try to do stood move here and this slid move s that's still that expression still valid to actually the value as the object as it's in memory it has on that entity but you can still move from it so you can't just say that well uh if it has an identity you can't move because you have to have some concept for this thing here that you you're able to move from something that that has identity so what they figured out is that identity and movability are two orthogonal concerns they're related but they're not necessarily the same thing so you can say any expression either it has an identity or it doesn't and either you can't move from it or you can move from it so so far we've seen the L value it has an identity and you can't this was hard to move horizontally you can't hear from it R values they have no identity they're Temporaries basically and you can move from them um so then we need a concept for this the move thing that's the x value if we need a name for something that you can move from even though it has an identity so the S here that's an L value the this one is still an R value like it used to be like very simple understood move is an x value and the X stands for um expiring L value so it is an L value which is kind of no longer and a value it has expired you can't really use it any longer after you move from it concept for that thing that actually it has identity but you can actually move from it um I like if you are like a completionist you might ask like hey there's a square missing here should we have something here and we should not because there is nothing that there are there's no really there's not uh and nothing has no identity and can't be moved from it's not a useful concept so yeah there is there is nothing up here um yeah yes so they also renamed the this concept of no identity uh can move to PR value so these are the three fundamental value types in civil verse 11 and later uh L value for the like normal normal variables PR value for the pure R values that's the P here the pure r value which is like basically just a temporary and an x value for anything that both has an identity and can be moved from and I already said that there is nothing up in the corner here so where do we put GL value and the old name or value where do those go like is there another axis here or what's going on and luckily we have already discussed both of these we just haven't given them names yet because GL value is basically just anything that has an identity so it's like if it has an identity you don't care if it can be moved from or not it's just like anything that actually refers to a real object that's a GL value so both L values and X values the union of these two is a GL value so s here is an is an L value because it's just a regular object and any L value is also a GL value this is the pr value like we saw on the previous slide and then if you move from the L value it's an x value but this still has an identity so it's also a GL value and the GL value stands for generalized L value and then maybe no big surprise that we have like a Union here and this direction as well anything you can move from is an R value so um you can see this one is hasn't changed but here this the string here this PR value that's just one type of r value you can move from it and if you still move the s uh it's an x value you can move from it so it's an R value so this is both an x value a GL value and an R value and that's uh the simplest way I could think of of uh yeah discussing these five value categories in simple bus yeah thank you and you had a question about what happens yeah so this is a PR value and if you didn't do anything like if you if you had assigned this to something it will actually never exist if you if you assign this to something um then you will have a copy Edition and nothing it would never actually exist but since this sub-expression here is a discarded value expression you never you discard this value you don't use it for anything then you're forcing the compiler to do the temporary materialization I think that was that's the name so it has to actually actually make the temporary like this doesn't have to even be a temporary you can just do copulation and just like stick it right in here uh at least conceptually I'm not sure if it happens yeah I'm not sure if you have guaranteed copulation here but you have that for like returning from functions like I think a function call expression is an x value uh so then and then so then you get a temporary and then you assign then you copy assign into this temporary and then everything just gets discarded so so it would be a way in effect of causing it to you know I could I'm just unsure of what the consequences of doing that would be yeah yeah so you only the side effects right nothing nothing would come out of this anywhere so it's just for the side effects you would have The Constructor of this of the string the string Constructor and then the copy assignments thing and then you then you destroy uh this one after you destroyed or before you destroy s oh yeah do you have any idea why they reuse the R value name for a different concept because I guess stick hero called it like generalized I think the number of places they would have to replace the word r value in the standard was a motivating factor I'm not I'm not because of the work to actually go on like the regex or the thing but like all the documentation everyone knows what an R value is like you can and that's that's why they wanted to be able to like this concept of our value is apparently very similar to what it was before whereas this concept of L value is not so similar something about that I think was the reason um [Music] how to say is an assignable state but you shouldn't trust in the body that is holding is that any or any new whatever value yeah so then the question is when you have when you have moved from s you don't really know uh anything about it so uh um usually you can't just you can't do anything but like this is just the value category is about the expression it's not about the object so this expression has a value category the object itself doesn't really have a value category it's just the expression so the expression s here is an L value like s Theory doesn't that's just an object but the expression s here which happens to be s it's it's that's what has the value category so the stud move doesn't change anything for S like that but once you have moved from it so stood move doesn't actually move anything but like once you actually call F and something has been moved from it you can't make any assumptions about it complete her face after a move is yeah don't assume anything Yes except for the standard Library which says that any types in the standard Library unless otherwise specified if you move from it it's in a valid but unspecified state so that means you can still destroy it and you can still do these things but the standard Library kind of imposes on itself but that the language pretty sure doesn't impose on you uh probably but I'm not sure if the instructors too around like yeah yeah but I'm not sure what the what the um what actually the standard like impulses on you know yeah but I'm not sure if the language should probably do that but like tell you as an implementer of the type you have to satisfy that it does that to the standard Library so you can but yeah I'm not sure about the guarantees for like any other types yeah okay thank you

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Anders Schau Knatten: lvalues, rvalues, glvalues, prvalue...