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

Поделиться
HTML-код
  • Опубликовано: 21 ноя 2024

Комментарии • 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 месяцев назад +1

      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 месяцев назад +1

      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 месяцев назад +1

      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 месяцев назад +1

      @@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 месяцев назад +2

    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 месяцев назад +1

      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 месяцев назад +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  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 месяцев назад +1

    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 😉

  • @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!

  • @segfault4568
    @segfault4568 5 месяцев назад

    Universal Reference gang member Here, Scott Myers most loyal soldier.

  • @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 месяцев назад +1

      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 месяцев назад +1

      @@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 месяцев назад +1

      @@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 😅

  • @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.