lvalues and rvalues in C++

The Cherno2,986 words

Full Transcript

one of the kitchen today because I'm working from harm so you know sometimes I just have to be here hey what's up guys my name is Anna welcome back to my staples last series today we're gonna be talking about Elle values and our values so is it just a zoom lens I don't usually shoot one a zoom lens Pam today so I had to I had to do that this is gonna be the first video in a series of videos which essentially is gonna be talking about some more advanced staples plus features including move semantics I don't know if this will just cover move semantics because there's obviously a lot more that this information will help you with and just learning about basically move semantics but it is definitely a huge prerequisite for that if you don't know what an l-value is if you don't know what an r-value is especially then it's gonna be a little bit difficult for you to kind of understand how the more advanced c++ features such as move semantics look this is definitely one of those things that is very difficult to just explain by waving my hands like I tend to do for most of my video we're definitely gonna jump into some code I'm gonna show you guys how this stuff works and what it is and more importantly why you care but first I want to thank Skillshare for sponsoring this video if you guys have not heard of Skillshare yet then you clearly haven't been watching most of my videos Skillshare is an online learning community where millions of creatives and curious people all around the world come together to deepen an existing passion or hobby or learn something new and unlike other kind of online communities Skillshare is specifically curated to teach you something new and with videos that are like super short super easy to understand super OnPoint it's a really efficient place for you to learn something new I know that a lot of people are anxious with what's going on in the world right now I know that I definitely am and I think being creative and learning something is a really good way to kind of put your mind out of that and also deepen something that you might have always wanted to do you'll share have so many amazing classes that will teach you just so many things photography videography how to start a YouTube channel and market that and start a successful business I mean seriously they've even got classes on how you can like rearrange the furniture in your room to make it better as I mentioned before I particularly love their illustration classes because I think that artwork is something that's really hard to teach well and I guess I just really enjoyed the way that Skillshare manages to communicate all of those ideas across and a lesson $10 a month for an annual subscription I think that is a really useful asset to have in your library of learning and information and skill sure have been nice enough to offer the first 1,000 people who sign up using my link in the description below two months of free Skillshare premium so especially if you're sitting at home now in this period two months for free try it out and see what new stuff you could learn huge thank you again to Skillshare for sponsoring not just me but other fellow youtubers in this difficult time I have to say I really like working with companies who are actually nice and Skillshare is definitely one of them Before we jump into some card and take a look at what L values and our values actually I just kind of want to explain to you roughly what they are and why it's important for you to kind of recognize what they are and we're not just going to be talking about L values and our values in this video we're going to also be touching l value references and our value of references these are kind of words I guess that a lot of samples loss program is here but they don't quite understand and especially if you're unusual language you might just get compiler error as saying words like L value R value must be an L value must be a modifiable R value known constant value and you might be like okay why is why is the compiler giving me such cryptic language why can't this language be more straightforward language being simple plus why can't it be more straightforward why do they have to have all these fancy words and the words are only fancy if you turn on what they mean if you know what the words mean they kind of lose their fanciness and they actually make sense and this video is hopefully gonna clear a lot of that up for you guys now a lot of people call L values locator values because they have some kind of location as we'll see in a minute but there are many different ways to kind of define L values and our values and I don't think that you should stick to like trying to basically you know find a definition for the letter L of letter R you just need to know what an L value isn't an R value is don't try and kind of define it for yourself because I feel like that's a little a bit of a trap okay so let's start with a super simple definition of what an L value is and what an R value is suppose I have a variable let's just call it I will set it to ten we have two parts to this expression we have a left side and a right side and this is also another good way to think about what an L value is in what an R value is an L value is a lot of the times something that is on the left side of the equal sign and then an R value is something that is on the right side of the equal sign now this does not always apply so I'm not gonna like say this is just a complete fact to always think of it that way because it's not necessarily true but in this case it is we have a variable called I which is of course an actual variable with a location in memory and then we have simply a value just a numeric literal it's just ten it has no storage it has no location until it is assigned to an L value which is what I is this makes sense because you can't assign something to an R value so otherwise I can't say 10 equals I or something like that that would be weird because 10 is not something that has a location we can't store data in ten however this is an L value now obviously we could just make another variable called a and set it equal to I in this case we're setting an L value equal to something it is also an L value which is why I was saying that whole like right side of the equal sign doesn't always make sense and our value doesn't just have to be a literal like this it can also be the result of a function maybe we have a function called get value which returns 10 and then we call this function here now in this case get value returns an R value it returns a temporary value it's temporary because even though it does return an int it has no location it has no storage it's just returning the value 10 but what we're doing here is taking that R value that temporary value and storing it into an L value now because this is an R value if we try and assign something to that R value it's not gonna work this expression is not gonna work why because this is an R value and you can see that our compiler is helping us as saying that expression must be a modifiable L value modifiable meaning it has to be non Const an L value meaning it has to be an L value however this is where it gets interesting if this was to return an L value which we could do by returning int reference this is called an l-value reference and I would need to obviously provide some kind of storage for my value maybe by using a static int like this and then returning it if this is the case since this is now an L value is pretending an L value reference I can assign to it and this expression works fine so that's what an L value reference is to expand on this a little bit if I had a function that took in a value maybe we'll call it set value I can call this function a number of ways I can call it with an L value or an R value I'll set I back to ten and then I'll call set value with that variable so here I'm calling set value with an L value and then here I'm calling set value with an R value so this is a temporary value and R value I keep repeating myself just so you guys really get the point here so in this case what will happen is this hour value will be used to create an L value when the function is actually called now we can instantly see which one of these is temporary and which is not because another rule is that you cannot take an L value reference from an R value so you can only have an L value reference of an L value and we can easily check this if I stick an ampersand over here so now I'm taking an interest is an L value reference you can see that this immediately gives me an error it says that the initial value of reference to non Const be an L value now you've probably noticed so far there's a lot of mention of like Const non constant what's the deal here well there's actually a special rule and that is that whilst I can't have an L value reference of an R value so in other words I can't have something like this if it's a Const L value reference I can so if you're right Const here you can see this works this is just a special rule it's kind of a little bit of a workaround realistically what happens is that the compiler will probably create like a temporary variable with your actual storage and then assign it to that reference like that so really it's just they're not to kind of avoid creating an L value but rather to just kind of support both our values and L values because what I can do here is make this an our const reference and you can see that it accepts both types of data it accepts an l value and an R value because a Const l value reference can in fact cept both of them okay let's scratch this example and take a look at another one this time using strings I'll have a first name which I'll set to my first name and then I'll have a last name which I will set to my last name then what I'll do is I'll create a third string which is gonna be my full name and I'll set this equal to first name plus last name ignoring the space that I'm not adding between the two strings so take a good look at this and try and identify what you think an l-value is and what the our values are so in this case everything on the left side here is an L value and everything on the right side here isn't our value so in other words this is an R value this is an L value this expression here is an R value it's a temporary object what happens is a temporary string gets constructed from these two and then it gets assigned into an L value so with that same kind of rule in mind if I had a function called print name which took in an S City string name here and then let's just say we print it here then if I try and call this function print name with full name it's gonna work fine but if I try and call it with this expression that we had here that's not going to work because it's an R value which is why you'll see a lot of conferences being written in C++ because as you can see they're compatible with both temporaries R values and also actual kind of real existing variables L values so we clearly have a way to detect whether or not something is an actual L value we can do that by just writing an L value reference that's non Const an L value reference can only accept L values this will not compile because this of course is an R value do we have a way to write a function that only accepts temporary objects yes and for that we need to use something called an r-value reference and our value reference looks the same as an L value reference except we use two ampersands instead of one so you can see what's happened here is the errors of switch now we can't pass in an L value but we can pass in an R value and if we hover our mouse over here and look at the error message it says an R value reference cannot be bound to an L value so that makes sense this is pretty cool because it means that we can actually write an overload of this function that accepts only temporary Jax and one that accepts L values and even if we make this Const meaning that it's also going to be compatible with our values this overload will still be chosen if it exists so if we take a look at our code here I might just write L value and then I'll do the same thing over here except I'll write our value if we now call all of these and I'll just do a little STD seems I guess that the console doesn't close you can see that the first overload that was chosen was the L value 1 and then the second one was the R value 1 so this is all really cool because with our value references we now have a way to actually detect temporary values and do something special to them so the question now might be well ok but how is this useful and the answer is well it's very useful especially with move semantics but that's going to be a topic for another video basically the main advantage here is to do with optimization if we know that we're taking in a temporary object then we don't have to worry about making sure that we keep it alive and making sure we keep it intact making sure we copy it we can simply steal the resources that might be attached to that specific object and use them somewhere else because we know that it's temporary it's not going to exist for very long whereas if you take in something like this then you can't I mean apart from the fact that it's cost you can't really steal anything from this name string because it might be used in a number of functions whereas this is clearly something that is temporary only going to be used with this particular call of print name and that's where the power really comes from so remember L values are basically variables that have some kind of storage back in them our values are temporary values l value references can only take in l values unless they're Const and our value references only take in these temporary R values you know I have this video Syrus playing with the paprika for this video makes sense for you guys as I say I'm definitely one of those topics that a lot of people are very confused about so hopefully this video helped to kind of clear the air a little bit as to what l values and our values are as I kind of progress with this series and specifically this kind of sum of semantics situation it will definitely become a lot more clear as to why it's important to know if you're dealing with an R value reference and if you can in fact steal the resources from that temporary value because it is temporary it really does help a lot with optimization and it's also very useful it's very important information to know in general if you want to know how sales class actually works and when you're kind of killing with the code like that because again a lot of the time you might be looking through some code you might see that double ampersand that R value reference and you might be like I have no idea what this means or why I care but hopefully with this you now nor never have you guys enjoyed this video if you did please hit that like button also don't forget to check out Skillshare first thousand people who use my link in description blur get free access to two months of skill shaped premium go and learn something new I will see you next time goodbye [Music]

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

lvalues and rvalues in C++ - YouTube Transcript | YouTube...