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

  Рет қаралды 2,304

Code for yourself

Code for yourself

Күн бұрын

Пікірлер: 27
@CodeForYourself
@CodeForYourself 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
Awesome! Thanks for the feedback and glad you liked it! 🙏
@taschenorakel
@taschenorakel 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
@@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 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
@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 5 ай бұрын
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 5 ай бұрын
@@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 5 ай бұрын
@@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 5 ай бұрын
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 5 ай бұрын
Did you try the ones at the end of the lecture? Like pass a real temporary value into the forward function directly?
@imnirajan
@imnirajan 5 ай бұрын
@@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 5 ай бұрын
@ilovenirajan no worries at all! This is what learning is all about 👌 I make these mistakes all the time!
@JkaBG
@JkaBG 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
​@@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 5 ай бұрын
Universal Reference gang member Here, Scott Myers most loyal soldier.
@CodeForYourself
@CodeForYourself 5 ай бұрын
Welcome 😅
Re-inventing move semantics in modern C++ in 13 minutes
13:20
Code for yourself
Рет қаралды 7 М.
When u fight over the armrest
00:41
Adam W
Рет қаралды 25 МЛН
Car Bubble vs Lamborghini
00:33
Stokes Twins
Рет қаралды 40 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 116 МЛН
Boost Tool V2
2:52
Reliant
Рет қаралды 2
Why use templates in modern C++
12:32
Code for yourself
Рет қаралды 2,1 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 109 М.
you will never ask about pointers again after watching this video
8:03
C++ Weekly - Ep 404 - How (and Why) To Write Code That Avoids std::move
8:50
C++ Weekly With Jason Turner
Рет қаралды 31 М.
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 426 М.
When u fight over the armrest
00:41
Adam W
Рет қаралды 25 МЛН