Language

Move Semantics Explained – Learn Modern C++



Code Blacksmith

C++11 move semantics and rvalue references – ever wondered how they work? In this lesson I will teach you what they are and how to write move constructors and move assignment operators.

Source

Similar Posts

33 thoughts on “Move Semantics Explained – Learn Modern C++
  1. Great job man, thanks. I was asked most of these things in an interview I did a couple of days ago, and I didn't know the answers. I never had much opportunity to deal with it since c++ 11. After watching your video, I was able to get back to the interviewer with my new found knowledge, and answer his questions, and he was very impressed. Good work!

  2. It's a nice video, but you should mention that raw pointers are used only as example to show internals and in general smart pointers should be used instead.

  3. I think I'm in love. Seriously, the level of emotion conveys the importance of avoiding all the wasteful construction and copies and is rather motivating. We already knew how to do these things in pre-C++ 11, now we have to learn new stuff, right? This video really makes one feel the importance of making the change, like moving out of a bad neighborhood or leaving a bad job for a much better one.

    There's many great presenters on C++ here on YouTube, but this presentation style really gets me excited about un-learning the things that have been re-worked in modern C++.

  4. This is a weird thing, not specific to C++ but to English, however, I see you also use the (English) language a different way than I do. I first noticed this, by the way, in Bruce Eckel's classic (now a bit stale in light of changes in modern C++) "Thinking in C++" and it jumped out at me about 5:30 in this video that your usage is similar to his and different to mine.

    This is related to the ambiguity in English where when someone asks you to feed the dog, you ask "Are you nuts? What do you want me to feed the dog to? An alligator?"

    a = 3;
    In English, for my whole life that I can remember, I would describe the operation above as either "a equals 3" (only if I was being very sloppy, better really I would say "a becomes equal to 3" or "set a to 3" or "a is set to 3".–people like Wirth and Dijkstra always hated using the = sign for assignment, for this very reason, and would go with := )
    If I would use the word assign, I would definitely always have said "Assign the value 3 to a", or if in a rush, "Assign 3 to a". However, I think of all of these choices, I would most likely say "assign the value 3 to a", like "attach the leash to the dog collar".

    In that example the lack of a case system in English doesn't really matter, "man bites dog" because of course we obviously aren't doing the option "3 = a".

    However, in:
    a = b;

    I would always say "Assign b TO a" short for "assign the value of b TO a". If I heard this over the phone or over my shoulder when I was discussing a fix to some shared code,, I would for sure type "a = b;"

    The verb here matters, if you had said "set a to b" I would interpret that as "set the variable a to contain the value of b" just as if you had said "set a to 3" would clearly mean "change the value of a to 3".

    It is my association of "assign to" that is so strong here. You assign <values> to <variables | array element | container element>'s in my English usage, but you set <variables | array elements | container elements> to values.

    This seems really OCD maybe, but I throw a micro-exception every time someone says "Assign x to y" and look up in a cache "Are these people who say "Assign b to a" like me or say "Assign a to 3" like those other guys.

    This is not specific to C++ at all, but the fact that we have two different default ways of saying this drives me a bit nuts. Of course, if one drags out "Assign x the value of 3" it is clear, but…I think you see where the ambiguity comes in.

    I hope you don't mind that this incredibly generic comment comes on one of your many videos that cogently explains current topics in Modern C++ that are rather urgent to understand well, but I am sure at some point in your life you have ended a one day or even two day debugging session with a one-character fix in one line somewhere, and it is in this spirit I wish everyone used English / whichever natural language we communicate about the pseudocode or code in an always consistent fashion.

    Now back to my second watch-thru of this video to ensure I fully understand the content.

  5. I now totally get why some people gripe that move is a weird name to use for this, the contents themselves never move, it is simply about a new or different object taking ownership of very large things without either copying or moving the very large things. If the widget actually contained the huge array in itself instead of a pointer to the array outside the body of the object, it wouldn't buy us anything here. But all that really gets "moved" here is a pointer, physically, and ownership, logically. If I have that right now, this video was truly awesome.

  6. GCC7 running in C++17 mode must optimize a lot of this away. When I ran the program (sort and all), all I got was one empty instantiation ctor call for each elem in the vector.

  7. I'm also curious why you used memcpy instead of std::copy(other.mPtr, other.mPtr + numElems, mPtr) or std::copy_n(other.mPtr, numElems, mPtr) ?

  8. Note: I forgot to add delete to move operator. If you're using owning raw pointers (not recommended), like it the example, please remember to free the memory. And then never use owning raw pointers again 😉

  9. Thanks! This was something that I needed to learn, but reading about it was so dry and dull. With your examples, I now understand it.
    Very much appreciated!

  10. For anyone watching, when implementing the rule of 5/6, for non-copyable/moveable types you can explicitly delete the move/copy constructor and or the move/copy assignment operator when it don’t be implicitly deleted by the compiler or to make your intentions clear.

Comments are closed.

WP2Social Auto Publish Powered By : XYZScripts.com