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. .
Similar Posts
48 thoughts on “C++ 11: Rvalue Reference — Move Semantics”
Comments are closed.
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?
Very nice video. Thanks !!
am… destructor should be delete [ ] arr_; right?
Simple and clear explanation about move semantics! Thanks Bo! 🙂
Nicely done.
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?
binding rvalue to lvalue reference will cause compile error. should use const lvalue reference.
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
Excellent
Thank you very much!!
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_?
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
In boVector size is a private variable…how can you do rhs.size in the copy constructor
the destructor of boVector should call 'delete[] arr_', not 'delete arr_'.
When you assign 'nullptr' to rhs.arr_, doesn't that mean that the rhs object will remain in the memory?
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.
Very Nice tutoriar.
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.
Thank you. This helps tremendously. 10/10
lost me after 8:00
Good explanation of move semantics.
2:16
std::move() converts lvalue to rvalue.
12:53
bo you are an amazing teacher keep up the good work!
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.
Excellent explanation of this not so easy to understand topic.
great ,thanks !
7:40 how can you set rhs.arr_ = nullptr? arr_ is a private data member, hence you can't access it from another object.
it would've been better if you replaced the boVector example with a working one.
At 04:08, usually when you allocate memory with "new []" then you should use "delete []" operator, is there any chance of memory leak in this code?
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
Thank you for making the video. I learned a lot from it.
thanks a lot Bo !!!
you could pick another name for variable 'a'. Its kinda confusing when you writing two 'a' in comment section.
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++.
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!
Good explanations on the move semantics ! Thank you !
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.
where can I download the ppt?
8:00 move constructor will not be called. I've tried it myself.
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
13:04 Old Transformers cartoon on in the background.
finally the video i was looking for !!!! u r my hero
How to goal kick lmao
I have always just used const references
but I might be able to find a use for this, I just don't see any reason to force it's use in my code
How would you implement those functions?
defining a function that takes a friggin temporary by temporary.
What are you teaching here? Jesus, beginners, run away from this guy.
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)
Great Video! I understand a lots with you. Thanks so much! (Not important but just mention that I agree with Taras)