Language

C++ 11: Rvalue Reference — Move Semantics



Bo Qian

Purpose of move semantics: avoid costly and unnecessary deep copying.
1. Move semantics is particularly powerful where passing by reference and passing by value are both needed.
2. Move semantics give you finer control of which part of your object to be moved. .

Source

Similar Posts

48 thoughts on “C++ 11: Rvalue Reference — Move Semantics
  1. at 13:05, you say it is ok to call

    hoo(string s);

    no matter how big the string is. How? If I write

    string s = "askjo;sjedfselkajs;dlfijasdf";
    hoo(s);

    isn't a full copy being performed here?

  2. Hi. I have a question.
    I can use the lvalue reference and do the same (in constructor). Move the object. So why do we need special rvalue reference in your use case?

  3. A funny thing I just found is, if I add cout in both copy an move constructor for this code, in gcc 4.8, there's no move constructor message printed, but in MS VS2010 compiler, move constructor message printed

  4. when we use rhs.arr_ = nullptr; it doesn't delete the elements in heap? Dont we have to use delete rhs? and then assign nullptr to rhs.arr_?

  5. foo_by_ref(boVector& v) should be foo_by_ref(const boVector& v) if you want to be able to pass both rvalues and lvalues. Otherwise, you will get a compile error:

    foo_by_ref(boVector& v); //<– should be: const boVector& v
    //…
    foo_by_ref(createBoVector()); //<— compile error

  6. Hi Bo Qian, your explanation is very clear. However, how do I really know which constructor is called?
    I have tried to add a printing sentence in each constructor (i.e.,
    copy constructor and move constructor). However, when I am passing by a lval, the printing message (in the copy constructor) is printed, while if I passed a rval, no message is printed.

  7. oh my god, thank you so much… this makes so much more sense than the 250 lines of code my textbook tried to use to explain this with shoddy prose.

  8. Can someone explain to me how the foo function actually applies the constructor to the class object parameter? I don't see a connection between the foo function and the constructors he included.

  9. lvalue is simply locator value, i.e. an object that can be located in the memory. And, rvalue is register value which is computed and the result is placed in some register temporarily in CPU. So, rvalue is such an object which cannot be located in memory

  10. You, sir, have some of the clearest explanations of the complicated C++11+ examples I've seen – and I've read a lot of them. Thank you for taking your time to make these, I will recommend your channel to others learning C++.

  11. Bo, I'm not sure if it was mentioned in the history of comments, but should you ever redo this (very nice) video, you might want to discuss copy elision. Some viewers may be at a loss to explain why their move constructor does not get called because the compiler is allowed to elude copying (darn smart compilers). Nice work though… Love your videos!

  12. at 8:55 you write foo_by_ref(createBoVector()) is wrong because createBoVector() is not an lvalue. It would be correct to use rvalue reference in the signature of foo_by_ref.

  13. After doing some trials with gcc I think that the behaviour of this code is very compiler specific
    foo(createBoVector()) at 06:00 does not call the copy ctor
    and after adding the move ctor at 08:00 it does not call it either

  14. Excellent explanation:
    1) Explains what lvalue and rvalue are
    2) Explains what lvalue reference and rvalue reference are.
    3) Explains how they enable function overloading => they will also result in constructor overloading
    4) Introduces Move constructor (that will be called implicitly now instead of Copy constructor when dealing with rvalues)
    5)

Comments are closed.

WP2Social Auto Publish Powered By : XYZScripts.com