Second major usage of rvalue reference: parameter perfect forwarding. Notes can be downloaded from: boqian.weebly.com
Пікірлер: 56
@sr57269 жыл бұрын
Thank you for sharing. Very useful videos.Your explanation is very clear and concise
@Jpres5 жыл бұрын
I'm finding these videos 7 years too late! Thanks for the videos.
@kubic-c31864 жыл бұрын
Thanks for the networking!
@yahortsaryk28427 жыл бұрын
Your tutorials are really perfect, now I've got the point of rvalues, thanks
@siavasharya71117 жыл бұрын
Thanks and great job. I appreciate that you clear up common confusions like the difference between move and forward.
@naeemjamali14754 жыл бұрын
Hello Sir, your teaching method is so easy, and thank you for making these videos. I will highly appreciate it if you make a course of c++ 17 or higher. Thank you.
@vatnax26803 жыл бұрын
Even although the video is old, it's still very helpful, thanks for the clear explanation!
@sir8904 жыл бұрын
Thanks a lot Bo for all your videos!! You are the God Of Explanation!
@thestarinthesky_2 жыл бұрын
Great explanation! Thank you so much. C++ is sure a really complicated but fascinating language!
@BloodHaZaRd6666 жыл бұрын
verry good ecplanation. best resume of scott meyers talk 11 mins for what worth me 6h on reading.
@Jacksilver27 жыл бұрын
I only now realized why they're called 'boVectors'. Nice. Great videos!
@dmh200026 жыл бұрын
Thanks. I have had trouble understanding perfect forwarding and universal reference but this video finally explains it clearly.
@nickst27977 ай бұрын
"You can go back to your work and entertain your boss with it"
@pouljin6 жыл бұрын
Thanks! This was the best explanation I have seen yet on this topic.
@malharjajoo73935 жыл бұрын
6:22- This is incorrect. From Scott meyers book and even from Eli Bendersky website (eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c) When an rvalue is given to T&&, let's say it's an integer literal, then T = int, and T&& = int&&. You have written T = int&& at 6:22
@deltarambo62304 жыл бұрын
You are absolutely right malhar jajoo, T is of int type (not int&& type as Bo Qian mentioned in the video). In addition to the sources malhar jajoo mentioned (I did not check them myself to be honest), you can inspect the type of T yourself using a function that exists in the boost library. Pass an rvalue of int type to an argument of T&& type, then inspect the type of T as following: #include std::cout
@willofirony6 жыл бұрын
A wonderful explanation of perfect forwarding.
@JesseBusman19967 жыл бұрын
Thank you, now I understand the &&, the reference collapsing rules, the std::forward and the std::move :-)
@rakeshmane36173 жыл бұрын
Nice explanation. Thanks for sharing.
@CyrilEjemah3 ай бұрын
Thank you for these videos
@gurdevrana59174 жыл бұрын
At 8:58, can anyone please tell me what is the difference between function template type and a class template type???
@thestarinthesky_ Жыл бұрын
Love C++! Awesome language!
@nagarajnaik93064 жыл бұрын
Excellent explanation. Thank you!!
@Channel1AlexM11 жыл бұрын
really cool!! Really learned the move semantics on this video.
@rajeshkhan80810 жыл бұрын
Your videos are awesome. Thanks for taking the time and effort. One quick suggestion regarding perfect forwarding you should also define it "Well, it means that a function template can pass its arguments through to another function whilst retaining the lvalue/rvalue nature of the function arguments by using std::forward. This is called "perfect forwarding", avoids excessive copying, and avoids the template author having to write multiple overloads for lvalue and rvalue references.". I got this from stackoverflow.com/questions/6829241/perfect-forwarding-whats-it-all-about
@jasontiller8 жыл бұрын
Thank you, Bo!
@mdwdyx5 жыл бұрын
Great! I finally understand it! Thanks a lot!
@thachnnguyen2 жыл бұрын
Perfect!
@jstock23177 жыл бұрын
Nice video! Very clear.
@videofountain11 жыл бұрын
Thanks. Useful and Concise.
@BoQianTheProgrammer12 жыл бұрын
If you mean the new STL for C++ 11, you can get them once you get a compiler that supports C++, such as g++ 4.7.x
@vikram90ful7 жыл бұрын
Hey Bo, If I understood clearly, regarding perfect forwarding the -same type should be passed to foo, as passed to relay, so even if we don't use std::forward we can achieve perfect forwarding as what ever is passed to relay lvalue or rvalue, by collapsing rule it will be passed to function foo. Please correct me if I am wrong
@Dziaji7 жыл бұрын
You are wrong. Arg is an lvalue, even though an rvalue was passed into the function. A rule of thumb is "if the variable has a name, it is always an lvalue". The &&arg parameter converts an rvalue into an lvalue by giving it a name.
@ghanshamkhobragade38177 жыл бұрын
Thank you for sharing this.
@dankman76037 жыл бұрын
great tutorial, learned a lot
@edwardkhon42077 жыл бұрын
Thanks for the tutorial Bo, it's very insightful. *thumbs up* I just wanted to clarify some things, apologies if I'm just not a good listener :) (1) The function signature of foo(boVector arg) seems to suggest that arg will be passed by value (normally) -- are you suggesting that if we use std::forward(arg) in relay we won't copy construct? If so could you point me to / explain the rules behind that? (2) Why is std::forward(arg) needed in relay? Isn't arg already of type T&& in relay(T&& arg)? Perhaps this is the same question as (1).
@edwardkhon42077 жыл бұрын
I did a bit more research on my own and I think I've answered both questions myself. For (1): It looks like the compiler can potentially be smart enough to move an rvalue into the value argument (stackoverflow.com/questions/7592630/is-pass-by-value-a-reasonable-default-in-c11). For (2): It looks like std::forward is necessary because 'arg' would have become an lvalue in the body of relay(), so we needed to cast it too T&& (stackoverflow.com/questions/7257144/when-to-use-stdforward-to-forward-arguments). If you have any further insight it'd be nice to hear.
@miguelfernandosilvacastron32796 жыл бұрын
Very nice explanation. If your goal is just to explain "what is" and "How" is perfect. I suggest that you could also work on Why, by telling some pratical example, even if for that you have to had more videos. Thank you
@ccfenix5 жыл бұрын
very helpfu!
@nkhullar111 жыл бұрын
Very Nice .. thumbs up
@necatiergin56906 жыл бұрын
Thanks for sharing. Your videos are very helpful to learners. In function call relay(9), your explanation regarding reference collapsing seems to be wrong. The type T would be declared as int and not int &&. As the type T is deduced as int, the function parameter arg would be int &&, i.e R value reference. If you only declare the function without defining int, and attempt to link the project, the linker typically would mention the deduced type for T: template void relay(T && x); int main() { relay(9); } The message generated by my linker is below: main.obj : error LNK2019: unresolved external symbol "void __cdecl relay(int &&)" (??$relay@H@@YAX$$QAH@Z) referenced in function _main Linker says here that T is int and relay param is int &&.
@WowPlusWow3 жыл бұрын
no, it would be int&& && since he is passing an r value reference. I don't think you know how templates work.
@azadalishah29665 жыл бұрын
🤔Can u pls explain what forward() does when different types of arguments are passed viz int&& and int& types? What it removes when int&& is passed?
@ADCar6 жыл бұрын
So does this mean we should declare all template function with T&& ?
@mnfchen8 жыл бұрын
Question: so there can't be universal references for class templates, only function templates? How would you achieve universal reference for classes then?
@gordian998 жыл бұрын
Yes, you template classes can have methods that use forwarding (universal) references. Make them template methods (of the template class).... class Sample { //...snip // lhs is an rvalue reference Sample(Sample&& lhs); // arg below is a forwarding reference template void relay(T&& arg) { foo( std::forward(arg) ); } }; If your class itself is a template, the sample thing applies... template< class T> class X { T t; public: X(T& lhs) : t{lhs} {} X(const X& lhs) : t{lhs.t} {} X(X&& lhs) : t{std::move(lhs.t)} {} X& operator=(const X& lhs) { if (this != &lhs) t = lhs; return *this;} X& operator=(X&& lhs) { if (this != &lhs) t = std::move(lhs); return *this;} template relay(U&& arg) { t.foo(std::foward(arg)); } };
@佐邱鸣5 жыл бұрын
This is the only topic I found Bo Qian is not that confident about it. Damn, it is hard to understand all of this.
@vorpal226 жыл бұрын
Ooops. Never mind my previous comment (deleted). I didn't realize that this doesn't apply to non-template functions. Now that I made it a template, I get the desired behaviour.
@mrlarry197512 жыл бұрын
Where do i get the new libraries?
@lordadamson6 жыл бұрын
A really good video. But I could hear the saliva in your mouth the whole time so I'm amazed I managed to go through the whole thing. Real good information though thanks :D
@malharjajoo73935 жыл бұрын
that was disrespectful. The guy has saved you a lot of time, and you want to comment about saliva =.=