C++ 11 Move Semantics and rvalue reference | Abhishek Kumar | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

C++ 11 Move Semantics and rvalue reference

teacher avatar Abhishek Kumar, Computer Scientist at Adobe

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

8 Lessons (40m)
    • 1. Introduction

      1:12
    • 2. Motivation

      5:03
    • 3. lvalue and rvalue

      3:53
    • 4. lvalue and rvalue reference

      9:32
    • 5. std::move

      8:56
    • 6. Move Constructor

      5:38
    • 7. Move Assignment Operator

      5:01
    • 8. Conclusion

      0:15
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

18

Students

--

Projects

About This Class

Move Semantics in C++ are a valuable addition to the language after C++ 11 standard. It helps in avoiding unnecessary copies created due to temporary objects.

In this class we explore the new features of C++ move semantics in detail. The course covers:

  • Introduction to C++ Move Semantics
  • lvalue and rvalue
  • lvalue and rvalue reference
  • std::move
  • Move Constructor, and
  • Move Assignment Operator

Meet Your Teacher

Teacher Profile Image

Abhishek Kumar

Computer Scientist at Adobe

Teacher

Computer Scientist @Adobe

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

2. Motivation : more semantics is a valuable Addison to the modern sleeplessness, and it was introduced to the language with deceit listless 11 standards. So this allows us to avoid unnecessary copies, which was importantly in earlier sleeplessness. Soc PLACELESS has always been known toe produced first programs, but this creasing off unnecessary temporary objects and deep copies resulted in some slowness and ordered. Let's see one example soon. B bus, any object, any argument with function by value. Then there is a corporate created in order for this songs and to use this object. And there's another border like that when we return temporary object. And when we consume this in our clan, Korb, like we're rating ts equal to full, and we're passing some argument we want, which is off the same tape. Then, when this temple objectives return, it's copied deco appeasement. So this object through all the effort that went in creating this object just got deluded. It's not used at all, so we created best temp object and here again we're copping back. This temp object to this and this is them are deleted once it's copied. So this effort was in vain. Hope can be used this temporary object so that we don't have to copy again. So this passing leave early, we can get rid by passing it by reference. But this we cannot avoid. So let's see one concrete example make we have if function if and it returns a vector, that's a vector of him and it picks a migrant. So first it'll create one victor, and by some complex logic, it will try toe put some elements to this victor. No, it's it. Create some object whose is back director. And it does this Maybe let's it 1000 times. So treat stallion projects and passes it back to this Victor Onda and it returns the V so this in could be some general object type also some gender class and then it returns the victor and the plant. Using this, Korb will initialize a vector off the same type and it will get the value from this food its function. And here again, all elements off distractible be corporate to this victor, and this director will be deleted. So again, we see that all this afford in creating Miss Victor just goes in when the only uses that it will be used to copy back again to some other vector. And this is your temporary object and its dilator. So but the move Semantics. We tried to address exactly this problem so that we can reuse this temporary off that instead off cropping it back. We can moving back to the clan called. So here we can We could have been movie. And here is this Ah, Somewhere in the memory. This doctor is there. We so know this I will refer to the same instead of creating a copy artifice. So this move Semantics. We tried to address that problem on forest understanding. Move semantics. First, we will see water l values and our values in the upcoming videos. Then we will see in value in our value. Your friends ALS didn't going on for that. We will see the newly introduced this two D move in si placeless 11. Then we will see some users off. Move semantics and we will see assignment operator. And we will also see who constructor. Okay. And we will see some more Gampel self This move. Semantics. End up coming. Reduce. So thanks for watching and see you in the next video. 3. lvalue and rvalue: understanding the distinction between their value and our values. Very important for understanding the new move. Semantics in seat listless. So let's see what real value and water our values. So an L value is a value that resides in the memory so we can find the address off that so it can be in the EU for a stack. But the thing is that it's addressable and we should be able to find its Andress and it cannot be moved. For example, earlier, we have a variable I this is holding the value. I so you So here I Eason in value because we can take that dress off way. It's really too create an in point which takes the value. Address it for you so we can get there just But we cannot clear the point of Let's sippy to and for in their dresses this constantly. So this really more well read. So decision l value and we can also modify it like we can reassign equal to 10 and it's valid. What we cannot review sane our values and this is true for even usually defined classes for a jump in class has been defined where the user, and then we could an instance off that class here season in values. So are basically anything that can appear on the left hand side of this assignment Operators is an l value. It can also be in the right, but it should be very toe. Put it on the left side off. Oh, those Hyneman corporate No, let's see how is different from our value. So our value is the value that's not L value for soon, and it resides only on the right side off an assignment expressing So there's a loo. Trim or temporary vis is intended to be known more the file or jump blue. In the earlier case, I was in value. So here Sigh is inaudible you because we cannot sign that dress off sleeve. Or we cannot keep five on the left side of the assignment operator, but do things that we can move our values in the modern c++. Similarly, here it's an express, um, I plus one, which is a temporary object, and here I plus one is our value. Similarly, a song send returning some value, like Florida function, which take some other girl and it returns alerts X, It's nor difference. It's just every new X foods. Oh, kept your value, is it? It's a temporary object that was returning. So this is an R value. So they're F. Wharton isn't our value. It's a temporary object, and once its contents are corporate weeks, it will be destroyed some earlier. It's the do the constructor of this A in order to create a the new This is our value. So this is a really and this party's our value. So I hope, or it's clear what our values and our values in sleeplessness. In the next week, we will see what our elderly references and water are. Value references Who seemed the next video? 4. lvalue and rvalue reference: I know that we know ever l value in our value and sleeplessness. We're readyto study what real value and our value references. So first, let's see what really reference such nothing good. The good old references that we have been using in C plus lists before c++ 11. So the name has been changed toe elderly reference in order to distinguish it from the newly introduced our Valley references. So our leader with no concept off really reference. So these elderly references can be bound on Li l values, but not our values, however, weaken bind and our value to a constant value. So this means that we are, Let's say I could been and then we have but a friend six. So it's super centered way this 1% single m percent symbol. So it's in an present x equal toe I. So here are reason in value. So began minded toe, this simple reference or really reference and its claim What if the right been then this is wrong because we saw that in is an r value in our previous will You and Eisen in value So it's okay toe a sane and l veloute when Israeli reference work here. We're assigning it our veloute, one Israeli reference. So it's not OK, but we can write Const in value difference. And then the ST been so explain. So this is an important point, remember? And this goes true for even the functions. So if we have a function which takes his argument one in really reference and while calling yes we do for been, then it will screw an error. Where is the Fibria? One in 10 ticks, Will you didn't and we dude, this. I then explain because I eat an elderly and expects an L earlier. And here we're assigning our value, so it's not correct. So how could be make this function to accept beutel valuable as well as our value. So we can overloaded and begin make it your cost elderly reference as resource you. So it was fine to a sane and our value to a constant really reference, so we can do the same. The dysfunction make it cost elderly reference, and then it's fine to give it in our value. Now let's see water are really reference, so it's introduced in the C plus plus 11 by received little 11 standard and it can bind only to our values and it's represented by double 1% l values represented way single m person. So here we have double in person to distinguish it from elderly reference an express image in our value, different regions in a temporary object, as we saw in the previous video. So we will write, like in X equal to 10 and we have this into 11% our value difference and we gonna sign it any our value similarly began. Also thing some function call like this function returns on and an integer then we can also directly assign it to this our value essence there we could not have done this to and in value, So this would have been wrong or desist. Correct? No, let's see some more gambles. So we have a function iss which is overloaded. So in one case it takes in value difference. And in the second case, it takes our value residents and we're just printing whether ah, the corn was made through this l value worsen or our valley was so here We have a very likable to 10 and we call this a fly. So I ease in in value. This is in value and they're passing ICT with, so it will see which off these matters this. So we have an in really reference here, so it will print and rebellious because it will call this function. Where is your were passing in our values in season or value, So it will try to find the matching version Office on it will print are valueless. It's a really good I have taken an L Value and called the newly introduced move function in sleeplessness. So what does dodgy? It takes an L value and converts to our values. So here again, nor could, will be our values. So we can or directly oh us in value and expect that it will call the R Value original function. So we use move and it will called are believers in on this. You know, we have a Quidditch to test your earlier understanding. So whatever we have seen so far so let's see here instead off getting to Williams off from San. If we have created 33 versions of this function, if so, one pixel really references argument Second Tex con still really reference and per picks are value reference and according Livia drinking it or to the control. And here is the main function. And here we have a very belie which tech civil you've been. So here I is an ill will you and distended them our value. So you're asking us to predict what will be dark report these four calls. So once we're calling with elderly reference second with l value second with our value then we are calling Move on this 10 So not been here to be move on I And then in the last case we're typecasting This I explicitly to one are very reference. Suppose the video for some time. And when you already the door Koopman resumed the video. So, Noah, for the first case, it should be obvious. Now we're calling with an l value. So this is the only matching function. So its footprint in value for the second kiss it's a bit tricky, Like Reese order Oh, we can assign and our value Do we corn still really reference? We cannot assign our value to an elderly reference, but we can assign it to coastal really reference. So here we see that Ah we have one our value reference. And when we have constant really reference boot and accept this 10 so will it print constant value, our village print or value? So the answer is that it's supposed we have commented, or this third function deafness, um, the function with our value reference. Then there will be just one matching function. Deafness. Um, this this one. So in that case, it will print going still, really, But it's be uncommon this so this is not defined in that case. Oh, this really good residents over the 2nd 1 because we have one explicitly defined off song sandwich Texas are went our value reference. So in that case, it will print our value and every comment or this. Obviously, it will print or value. So even in the prisons off, this constant really reference it will give presidents to this are very reference part. Similarly, in the third case we're using move to move this l value and it will retreated and are well , you know, and it will again print our value. And in this case, also, it will print our value, so this will not be called, but ive recommend or disturbance then Ah, this second notes will be printed, so I hope that our value and then value differences are clear to you know, we will. Or they deep into the move. Semantics. We already with the basic concepts so see in the next 1,000,000. 5. std::move: in this video we will study or is to remove invaluable. A reason to the C plus was language after seat, listless alone standards. Mr. De Move is a template function that was added to the secrets language. And it helps in custom, we are really reference. So after C plus list, living there were some every since to the language like introduction off move assignment off Richard for earlier there were just copy assignment operator. So now we have moved assignment operator and Real toe copy constructor, Or, rather, say move constructive in addition to copy Kustra. But these, uh, functions expect our value of friends are the argument. So both of these. So in order to pick up wanted or not you like Tokcan work you're really reference to are really references. So internally you can think off sisterly move as typecasting like you have a great started cost and take custody toward were a you have and then no objective type D. So its decision in value you can cost it back to our value reference, and you can in turn, use these newly added functions. So in that rail it helps in preventing redundant copies which were curator due to the recent off. Temporary objects will see some practical examples of his study move with someone in court . So let's create a few victors. But elephant Let's call it even. We need to include the victor. It's defined a function which will bring the contents of the victor print work, too, and it will take his are going toe one rector off same type and Air France Trennert. No, let's heard some elements to these directors so even contains elements and 0 to 4. Similarly, let's do the same. For will be two more this train crunk dental for being it's and then Brendan Victors, who even aan den bring Victor we to alert see efforts working correctly too long so it could slip Prince Move the victors. No, let's see what happens when we coffee that two or even three to And then after copy, Let's again printed. So we see that earlier there were two directors and no we to also contains the elements awful even what we even still exists. And it prints all elements off even no, instead of copy. Let's say we used the move on it and then we bring board directors? No. Let's see. So t layer, we are two victors. Where is after moving? They want to be too. The corporate did not have understand. There was no copy happening on all the contents of even basic earlier. No. One way we to. So we see there the corn to even print function. It does not print anything because even is no empty in order to see there, you can also print the sides off Victor bullets here and here, the north side. So we see that earlier Both had five elements. No, even has derailment because it has been moved to re to, and we two know contents, all elements off. Even so here the corporate did not happen. Just transfer off resources and it happened. You're rethinking the system. The move has moved the resources from the one Toby too. But there's not the case in this. What this movement is that it can works, even when are very reference. And when we're assigning our well reference to Erector, it will use the ah assignment operator that is defined inside the rector Close. So we will see more of this move assignment off return. Um, in May for the reduced. So this move can be very useful when we want to avoid unnecessary copy. And we want to get rid off multiple times Christian off temporary objects. This move can very useful, like in this case. So in this case, it was a pretty small director. But the specter can be very use, maybe, or millions off elements in that and copping them to another victor can be very time consuming process to you. You like to have widened their copy and use move instead. So let's see some more applications off. Move semantics so we can use this function. Using the move like we can define a template functions well. And here we move the contents off. Eight were temporary variable and then move be to a and then finally moved him to be. So let's see in X Corp. We have no move between and finally room them to me. No, let's say you want to nose even in veto. Oh, a knowledge to run the program. So obviously that earlier even our dear 1234 and we to have 10 11 12 13 14 and ah, no. The contents have been divorced, so the copy did not happen. They're just, ah, change off the slope off actual contended happened. So you see, that move can be very useful for define even other template functions. So that's all for now. We will see the other applications off, move semantics and we will see. Move, constructor and move assignment operators in other further reduced. So see you in the next video. Thank 6. Move Constructor: most imminent exit available ladies into the sleeplessness level on most of the template classes defined by sea Personal Slavery's Now have this move constructor defining them in order to pick advantage of this for your warm classes, you must define move constructive yourself. So how is it different from normal copy constructors? When normal Copy constructor, you can see there is so l really reference in Move constructor, we have the orbital reference so let's see some examples to understand it better. So let's define a class A In all its are defined all the constructors And here every we just printing or from Lord Constructor Remove which one is called so miserable to Fort Constructor. So no way will do the same for other constructors. So this one reader corporate constrictor andan We have the new move Constructor An old let's use this close. Let's recreate a for you. They're before constructively record andan we call B and if we run this then it took cold Copy constructor, You solar TSI So yes, for eight cord 34 constructor and and while creating be called copy constructor of close in . No, let's see, we call them one. This for? No, it will call them who construct trophy. Onda should be true for others in textiles. Like if the of see on we initially it would be. Then it will again call the copy constructor. So this, uh, here also we could've used has to be moved on. We could have forced it to call the move constructor off plus earlier Severe Some move variables in this class. Let's say turn one be earned. This was just pointed Win area frontiers. So in the constructor, we're doing equal to new internal. It's an utilizing with 100 been in the copy constructor you would have to again or listening you in with 100 and, uh then copy work everything from this it will be you to this Like we wouldn't have to run no from General 200 Onda and copy everything to this new era. You know, beaches be so we'll fit, you know, video You ve in order p and then I So we see that we have toe copy everything for, uh, look at some memory and then copy everything from the world Object to the new images So it's a very costly operates, but in the case of move constructor, we can simply assign the pointer off the point through off area frontiers off. Well, we do to point to the current B and ourself. So that's all for this video. We will see another important feature the move assignment operator in the next video. So see in the next redo. Thank you. 7. Move Assignment Operator: just like more constructor. We need to define our move Assignment operator in or take the fuller gone pays off the move semantics and seek listless so off for move Assignment of critter Oh, this is the same pics. This is the usual coffee assignment of Britain. So it takes an l value reference where the move assignment of Britain takes an are really reference. So let's see some of the, um, Berloff this in the court. So this is our earlier plus a where were defined the move constructor for no, let's be fine. Who assignment operators forced. Let's define the copy assignment operator. So we have a pretty cool too many takes head argument, a cost elderly reference off plus a and let's print or first copy assignment. And we will have to do the other stuff, like copy only values from on the world object and and copping it took current one. So this we need to do here, you know, or let's use this one. So let Sylvia hair. We have a B and C. Let's comment on these for no. Then we're assigning me to a so here it will be copping calling the copy assignment or greater. So let's run this. So here we also need to return this a Knoller, turn it for the other before constructor off is cornered, then be scored. And then here, the copious handling discourse. So what if we do is study, move on parts and are really reference What will happen? So we see that again copy assignment is happening because this constant really reference can pick and our value. But it's redefine our assignment operator. So first thing that truce. So the first thing is that they're the current. We have two or three art free the this whole city off this object. Because of this, the current object will be holding Oh, the new object. So we need to free it. Like for In this case, we have just 21 interior pointer which is pointing to an area of 20 years Solar It's or deleted first, so we really not be. And then ah, we don't need this. We will assign b two of Let's be andan. Finally we need toe set the beauty for their object TUNEL pointers Who their true Cinderella State and no is it on the same Corp you see their terror movie Sandman to score called because we have defined over Move assignment Operator. No. So these move, constructor and move assignment operators are very useful way for taking full advantage of the move. Semantics in C plus plus. So that's also no See you in the next video Think. 8. Conclusion: Congratulations on treating to the end of the course. Hope you inured. The secret was moved him Antics. Add mater's I Did So I hope you'll use the newly learned concept in your programming. Thank you.