Why use forwarding references and how they are different from rvalue references in C++

  Рет қаралды 1,956

Code for yourself

Code for yourself

Күн бұрын

Пікірлер: 27
@CodeForYourself
@CodeForYourself 3 ай бұрын
Hey everyone. How did you find this video? Too long? Or short? Too complicated? Or rather too dumbed down? I need your feedback to make sure I’m getting better. So… what do you think? 🙏
@Wizarth
@Wizarth 3 ай бұрын
I don't know any way you could cover this topic with sufficient detail and be any shorter or less complicated. For me, it was just right.
@CodeForYourself
@CodeForYourself 3 ай бұрын
Awesome! Thanks for the feedback and glad you liked it! 🙏
@taschenorakel
@taschenorakel 3 ай бұрын
You should give more time to read and understand the examples. It's really nice to see you talking, but often you return from code to yourself way too quickly.
@syyyr
@syyyr 3 ай бұрын
I don't have much use for std::forward (and had no idea how it works, and when to use it), so for me, it had the correct amount of detail.
@CodeForYourself
@CodeForYourself 3 ай бұрын
@@taschenorakel totally makes sense. In this format I made a deliberate decision of keeping up with the pace to keep people a bit more engaged. My reasoning was that if people would really want to return and follow all the details they can always pause the video. I might convert all of this into a proper course on a different platform geared for just learning where I could maybe reduce rhetoric pace somewhat 🤔 Thoughts?
@Wizarth
@Wizarth 3 ай бұрын
Thank you for walking through how this forwarding works, particularly the ampersand collapsing. I hadn't fully understood how forwarding works until you put it all together like that.
@CodeForYourself
@CodeForYourself 3 ай бұрын
Thanks for the kind words! I have to admit that before recording the video I didn’t understand it fully either 😅 Well, I had a pretty good idea but then my intuition was wrong with respect to which forward overload would be called. 😅
@AzerAnimations
@AzerAnimations 3 ай бұрын
You have some of the best videos on C++. I would love if after your "C++ basics" lessons that are happening, you could move to a more specialized subject. I see you have a PhD in computer vision, so that would be a cool subject to tackle in C++ (as a lot of tutorials use Python)
@CodeForYourself
@CodeForYourself 3 ай бұрын
Thanks 🙏 I keep playing with this idea. I come from a robotics background and we’ll see if I manage to make something out of it once I finish the C++ course here 😅
@zamf
@zamf 3 ай бұрын
An interesting fact is that forwarding (or universal) references were discovered and not designed in the language. They just happened to spring into existence following the rules of template deduction and reference collapsing. It's one of the many miracles of the language showing that when you design something right it gives more value than designer has originally anticipated. It's an example of 1+1 giving more than 2.
@CodeForYourself
@CodeForYourself 3 ай бұрын
Yes, thanks for this comment! I believe the reason the name “universal reference” exists is just for that reason too! It came before the “forwarding reference” name was used in the standard. That being said, I don’t have the hard facts for this, so if somebody can point me in the right direction I would be immensely grateful 😉
@francescobenacci6611
@francescobenacci6611 3 ай бұрын
@10:57 when we call DoSmth(number) i didn't expect the parameter type to be int&. Why is that, and not simply int? After all, it will be copied...
@CodeForYourself
@CodeForYourself 3 ай бұрын
Unfortunately the answer to this comment is more complicated than I would like it to be. It has to do with how overload resolution is taken care of in C++, which is not trivial: en.cppreference.com/w/cpp/language/overload_resolution But the rule of thumb, at least the way I think about it, is that if we have a local object the compiler will prefer passing it by reference if an appropriate function overload exists. During this some implicit conversions can take place as well as some copies performed. My recommendation would be to write a simple example with some Foo function that has various overloads and see what the compiler does in each case to build more intuition. That being said if somebody who can explain it better stumbles upon this comment, I would like to find a nice and simple explanation for the future. 😅
@francescobenacci6611
@francescobenacci6611 3 ай бұрын
@@CodeForYourself thank you, i really appreciate the answer, it surely provides a good starting point for me to try and understand this :) Keep up the great work, i discovered your channel with this video, and decided to start the whole course from the first lesson, even if I'm familiar with some of the basics of C++
@CodeForYourself
@CodeForYourself 3 ай бұрын
@@francescobenacci6611 thanks for the kind words! Please mind that the first videos have a slightly different style from this one. I guess I converged to a style like this some time around the cmake video 😅
@imnirajan
@imnirajan 3 ай бұрын
still can't figure out on what paramterts to put to make it choose this function T&& forward(std::remove_reference_t&& t) instead
@CodeForYourself
@CodeForYourself 3 ай бұрын
Did you try the ones at the end of the lecture? Like pass a real temporary value into the forward function directly?
@imnirajan
@imnirajan 3 ай бұрын
@@CodeForYourself oh lol, my bad. I mistakenly put real temporary value into doSmth instead of forward. lol, i'm so stupid. sorry for bothering u.
@CodeForYourself
@CodeForYourself 3 ай бұрын
@ilovenirajan no worries at all! This is what learning is all about 👌 I make these mistakes all the time!
@JkaBG
@JkaBG 3 ай бұрын
I find it better to understand if I read `&&` as `owned` or `owned_value`. #define owned && So the difference between `int` and `int owned` is the ownership.
@CodeForYourself
@CodeForYourself 3 ай бұрын
That’s an interesting idea, although I don’t like the use of macros here (what if you have a variable called owned in the code or smth) But apart from that it sounds fun. I’m not sure how I feel about it just yet as in my head a cope of a variable owns its memory too and can transfer it if we pass it through an std::move and some move assignment operator 🤔
@JkaBG
@JkaBG 3 ай бұрын
​@@CodeForYourself Yes, I do not use in code but for explaining and understanding it is good. You can show a `int &&` and then show `int owned` to compare it in different contexed. For example: Saying `int &&` / `int owned` has more in common with `int`, than `int &`. Comparing && and & can be very confusing.
@segfault4568
@segfault4568 3 ай бұрын
Universal Reference gang member Here, Scott Myers most loyal soldier.
@CodeForYourself
@CodeForYourself 3 ай бұрын
Welcome 😅
Re-inventing move semantics in modern C++ in 13 minutes
13:20
Code for yourself
Рет қаралды 6 М.
POV: Your kids ask to play the claw machine
00:20
Hungry FAM
Рет қаралды 17 МЛН
大家都拉出了什么#小丑 #shorts
00:35
好人小丑
Рет қаралды 96 МЛН
Apple peeling hack @scottsreality
00:37
_vector_
Рет қаралды 127 МЛН
Why use templates in modern C++
12:32
Code for yourself
Рет қаралды 1,9 М.
Stop Using FirstOrDefault in .NET! | Code Cop #021
12:54
Nick Chapsas
Рет қаралды 58 М.
Perfect Forwarding In C++ | Why is it Perfect ? 🤔
6:00
CatchyBytes By Techy
Рет қаралды 200
Don't use static (outside of classes) in modern C++
15:56
Code for yourself
Рет қаралды 5 М.
How to use FUNCTION TEMPLATES - a comprehensive guide for modern C++
23:33
Code for yourself
Рет қаралды 2,3 М.
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
POV: Your kids ask to play the claw machine
00:20
Hungry FAM
Рет қаралды 17 МЛН