C++ 11: Rvalue Reference - Perfect Fowarding

  Рет қаралды 63,419

Bo Qian

Bo Qian

Күн бұрын

Second major usage of rvalue reference: parameter perfect forwarding.
Notes can be downloaded from: boqian.weebly.com

Пікірлер: 56
@sr5726
@sr5726 9 жыл бұрын
Thank you for sharing. Very useful videos.Your explanation is very clear and concise
@Jpres
@Jpres 5 жыл бұрын
I'm finding these videos 7 years too late! Thanks for the videos.
@kubic-c3186
@kubic-c3186 4 жыл бұрын
Thanks for the networking!
@yahortsaryk2842
@yahortsaryk2842 7 жыл бұрын
Your tutorials are really perfect, now I've got the point of rvalues, thanks
@siavasharya7111
@siavasharya7111 7 жыл бұрын
Thanks and great job. I appreciate that you clear up common confusions like the difference between move and forward.
@naeemjamali1475
@naeemjamali1475 4 жыл бұрын
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.
@vatnax2680
@vatnax2680 3 жыл бұрын
Even although the video is old, it's still very helpful, thanks for the clear explanation!
@sir890
@sir890 4 жыл бұрын
Thanks a lot Bo for all your videos!! You are the God Of Explanation!
@thestarinthesky_
@thestarinthesky_ 2 жыл бұрын
Great explanation! Thank you so much. C++ is sure a really complicated but fascinating language!
@BloodHaZaRd666
@BloodHaZaRd666 6 жыл бұрын
verry good ecplanation. best resume of scott meyers talk 11 mins for what worth me 6h on reading.
@Jacksilver2
@Jacksilver2 7 жыл бұрын
I only now realized why they're called 'boVectors'. Nice. Great videos!
@dmh20002
@dmh20002 6 жыл бұрын
Thanks. I have had trouble understanding perfect forwarding and universal reference but this video finally explains it clearly.
@nickst2797
@nickst2797 7 ай бұрын
"You can go back to your work and entertain your boss with it"
@pouljin
@pouljin 6 жыл бұрын
Thanks! This was the best explanation I have seen yet on this topic.
@malharjajoo7393
@malharjajoo7393 5 жыл бұрын
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
@deltarambo6230
@deltarambo6230 4 жыл бұрын
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
@willofirony
@willofirony 6 жыл бұрын
A wonderful explanation of perfect forwarding.
@JesseBusman1996
@JesseBusman1996 7 жыл бұрын
Thank you, now I understand the &&, the reference collapsing rules, the std::forward and the std::move :-)
@rakeshmane3617
@rakeshmane3617 3 жыл бұрын
Nice explanation. Thanks for sharing.
@CyrilEjemah
@CyrilEjemah 3 ай бұрын
Thank you for these videos
@gurdevrana5917
@gurdevrana5917 4 жыл бұрын
At 8:58, can anyone please tell me what is the difference between function template type and a class template type???
@thestarinthesky_
@thestarinthesky_ Жыл бұрын
Love C++! Awesome language!
@nagarajnaik9306
@nagarajnaik9306 4 жыл бұрын
Excellent explanation. Thank you!!
@Channel1AlexM
@Channel1AlexM 11 жыл бұрын
really cool!! Really learned the move semantics on this video.
@rajeshkhan808
@rajeshkhan808 10 жыл бұрын
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
@jasontiller
@jasontiller 8 жыл бұрын
Thank you, Bo!
@mdwdyx
@mdwdyx 5 жыл бұрын
Great! I finally understand it! Thanks a lot!
@thachnnguyen
@thachnnguyen 2 жыл бұрын
Perfect!
@jstock2317
@jstock2317 7 жыл бұрын
Nice video! Very clear.
@videofountain
@videofountain 11 жыл бұрын
Thanks. Useful and Concise.
@BoQianTheProgrammer
@BoQianTheProgrammer 12 жыл бұрын
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
@vikram90ful
@vikram90ful 7 жыл бұрын
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
@Dziaji
@Dziaji 7 жыл бұрын
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.
@ghanshamkhobragade3817
@ghanshamkhobragade3817 7 жыл бұрын
Thank you for sharing this.
@dankman7603
@dankman7603 7 жыл бұрын
great tutorial, learned a lot
@edwardkhon4207
@edwardkhon4207 7 жыл бұрын
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).
@edwardkhon4207
@edwardkhon4207 7 жыл бұрын
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.
@miguelfernandosilvacastron3279
@miguelfernandosilvacastron3279 6 жыл бұрын
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
@ccfenix
@ccfenix 5 жыл бұрын
very helpfu!
@nkhullar1
@nkhullar1 11 жыл бұрын
Very Nice .. thumbs up
@necatiergin5690
@necatiergin5690 6 жыл бұрын
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 &&.
@WowPlusWow
@WowPlusWow 3 жыл бұрын
no, it would be int&& && since he is passing an r value reference. I don't think you know how templates work.
@azadalishah2966
@azadalishah2966 5 жыл бұрын
🤔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?
@ADCar
@ADCar 6 жыл бұрын
So does this mean we should declare all template function with T&& ?
@mnfchen
@mnfchen 8 жыл бұрын
Question: so there can't be universal references for class templates, only function templates? How would you achieve universal reference for classes then?
@gordian99
@gordian99 8 жыл бұрын
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.
@vorpal22
@vorpal22 6 жыл бұрын
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.
@mrlarry1975
@mrlarry1975 12 жыл бұрын
Where do i get the new libraries?
@lordadamson
@lordadamson 6 жыл бұрын
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
@malharjajoo7393
@malharjajoo7393 5 жыл бұрын
that was disrespectful. The guy has saved you a lot of time, and you want to comment about saliva =.=
@mryup6100
@mryup6100 4 жыл бұрын
@@malharjajoo7393 right
@harehudi5117
@harehudi5117 7 жыл бұрын
goood
@malharjajoo7393
@malharjajoo7393 5 жыл бұрын
9:17 - You didnt explain the most crucial part :(
@aayushneupane5211
@aayushneupane5211 5 жыл бұрын
and whats that?
@agnesakne4409
@agnesakne4409 Жыл бұрын
lef lef lef lef lef lef
C++ 11: User Defined Literals
9:29
Bo Qian
Рет қаралды 25 М.
C++ 11: Rvalue Reference -- Move Semantics
14:22
Bo Qian
Рет қаралды 151 М.
Blind Boy Saved by Kind Girl ❤️
00:49
Alan Chikin Chow
Рет қаралды 49 МЛН
Don’t try this trick with a Squid Game Soldier 😉 #squidgame
00:15
Andrey Grechka
Рет қаралды 178 МЛН
C++ Perfect Forwarding
9:00
BitsOfQ
Рет қаралды 7 М.
Advanced C++: Understanding rvalue and lvalue
12:06
Bo Qian
Рет қаралды 140 М.
Modern C++ (move semantics, perfect forwarding)
19:49
CopperSpice
Рет қаралды 22 М.
Move Semantics in C++
13:10
The Cherno
Рет қаралды 312 М.
C++11 Perfect Forwarding
17:24
oneproduct
Рет қаралды 19 М.
you will never ask about pointers again after watching this video
8:03
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 198 М.
Blind Boy Saved by Kind Girl ❤️
00:49
Alan Chikin Chow
Рет қаралды 49 МЛН