lvalue, rvalue, lvalue references and an introd to rvalue references | Modern Cpp Series Ep. 31

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

Комментарии • 65

  • @Yuvaraj-pd6ng
    @Yuvaraj-pd6ng 8 месяцев назад +9

    this is the best c++ series this video was amazing

    • @MikeShah
      @MikeShah  8 месяцев назад +1

      Cheers, thank you for the kind words!

  • @MindHaunter
    @MindHaunter Месяц назад +1

    Between `std::string s3 = s1+s2;` and `std::string&& s3 = s1+s2;`, the latter is indeed slower (for s2 around 4000 characters), especially with -O3 or -O2 optimization. I am sure there are use cases for rvalue ref (and you will teach us in the next videos), but this simple example is probably not one of them. As always, thank you for detailed explanations. Coming from python and R, I cannot imagine how terribly it'd have been if I just wanted to follow cppref or learncpp.

    • @MikeShah
      @MikeShah  Месяц назад +1

      Cheers! I'll need to think about what exactly is going on when we do std::string&& s3 = s1+s2. To me we're doing all of the construction, and then we move s1 and s2 (or otherwise extend their lifetime). We'll have to be careful otherwise as we can pass s3 into a function only once. I'll talk about this in a future video when I also cover things like perfect forwarding :)

  • @k0185123
    @k0185123 9 месяцев назад +2

    Wow. I really enjoy how you break down these subtle concepts step by step. I came here because sometimes I am not sure about if some of my functions can return reference. I was wondering if that is related to this lvalue issue. So I didn't expect I'd learn this interesting "rvalue_reference"! This is so useful! I think sometimes it can indeed save computational time. Thank you very much for your sharing. I really love how to put everything together in one screen and smoothly control those windows. Hopefully one day I can be as professional as you! Thank you again!

    • @MikeShah
      @MikeShah  9 месяцев назад +1

      Cheers, thanks for the kind words! As for returning a reference -- just be careful when returning references to stack allocated data (same as with a pointer) -- as it's not clear what value will be in memory :)

    • @k0185123
      @k0185123 9 месяцев назад +1

      @@MikeShah Got it! Thank you for the reminder :)

  • @dwolrdcojp
    @dwolrdcojp 2 года назад +5

    I appreciate the pop quizzes. Really improves the value of the lesson!

    • @MikeShah
      @MikeShah  2 года назад

      Thank you for the kind words! On lessons like this I like to throw those in to test folks understanding :)

  • @mytech6779
    @mytech6779 Год назад +2

    Very clean, as a hobbiest I've been struggling with this for years.
    At first I was thinking Local and Reference because the contexts where I fiest heard the term was talking about passing function arguments and scope.
    Then Left and Right [but no, I thought, it couldn't really be that simple, sometimes an L seems to be on the right or they are standing alone], and the formal technical definitions are rather cumbersome parse.
    But this video clears up a lot, and your example also made me realize they could be thought of as Location/Locateable [ "Loadable" from memory?] and Register because the rvalues live in the cpu registers (Ignoring some details like instruction immediates and i/o reads which live elsewhere, though the registers still generally act as their gate keeper.),
    eg `(A+B)`, A nd B may be loadable from memory but the sum only exists in a register prior to assignment.

    • @MikeShah
      @MikeShah  Год назад

      Cheers, glad to hear this helped!

    • @stupidteous
      @stupidteous Месяц назад

      hobbyist*

    • @mytech6779
      @mytech6779 Месяц назад

      @@stupidteous male-donkey*

  • @muhammedusman7269
    @muhammedusman7269 9 месяцев назад +1

    all videos are great and excellent. Thanks

  • @piotrlezanski5156
    @piotrlezanski5156 8 месяцев назад +1

    You are great! Thanks

    • @MikeShah
      @MikeShah  8 месяцев назад

      Thank you for the kind words 🙏

  • @robertstrickland9722
    @robertstrickland9722 2 года назад +3

    I really enjoyed this explanation, even more than reading about it in the C++ Crash Course by No Starch Press. Good job.

    • @MikeShah
      @MikeShah  2 года назад

      Thank you for the kind words!

  • @mohsenzare2511
    @mohsenzare2511 6 месяцев назад +1

    I saw this video a while ago, and know I come back to watch that again! thank for your great video!

    • @MikeShah
      @MikeShah  6 месяцев назад

      Cheers! You are most welcome!

  • @Quancept
    @Quancept Год назад +1

    damn! just like that I understood l-values and r-values. I don't know how to thank you enough Mike :)

    • @MikeShah
      @MikeShah  Год назад +1

      Cheers, happy to help!

  • @muhammadharris4470
    @muhammadharris4470 2 года назад +2

    Thanks mike for teaching this super important topic for extreme simplicity. really love your teaching style 👍

    • @MikeShah
      @MikeShah  2 года назад

      Cheers thank you for the kind words!

  • @FadiEID-s3g
    @FadiEID-s3g 2 месяца назад +2

    42. There is something to that number. Perhaps the piscine?

    • @MikeShah
      @MikeShah  2 месяца назад +2

      From Douglas Adam's book -- Hitchhiker's Guide to the Galaxy :)

    • @lucasnicosia3536
      @lucasnicosia3536 Месяц назад +2

      Il a pas la ref dans la ref 😅

  • @guilherme5094
    @guilherme5094 Месяц назад +1

    👍Thanks again!

  • @dhanushs1802
    @dhanushs1802 2 года назад +1

    Very well explained as always. Thank you.

    • @MikeShah
      @MikeShah  2 года назад

      Cheers thank you for the kind words!

  • @OzanCetinaslan
    @OzanCetinaslan 4 месяца назад +1

    Hello Mike, you are a great teacher and lately I really enjoy your videos. Why I am watching I do not know maybe you explain really clean :) In this video I noticed a small issue at the beginning (4:30 ish) when you define Lval and Rval. You say Lval is on memory and Rval does not point to anything. I am not sure if this is really the case. If you think about how Bjarne define the object you understand what I mean. Object: a piece of memory, Variable: a named object. So, if you map this info to Lval and Rval I believe that you understand what I mean. For example, string str = string{"Mike"}. So, in here, str = variable, which is Lval, string{"Mike"} = object which is Rval and certainly points to some address, but no one really needs to know. I hope I am clear. Anyways Mike, you are great please keep up.

    • @MikeShah
      @MikeShah  4 месяца назад +1

      Hmm, indeed there might be a more clear way to define rvalue -- the definitions in the link below provide a bit more strict definition. I think rvalues indeed have some location, but you cannot realistically take the address of (with &) of these values so that's the main idea :)
      learn.microsoft.com/en-us/cpp/cpp/lvalues-and-rvalues-visual-cpp?view=msvc-170

  • @visitor_t-w3p
    @visitor_t-w3p 10 месяцев назад +1

    Thank you so much I enjoyed a lot

  • @TheIntervurt
    @TheIntervurt 2 года назад +2

    Did you mean to say copy assignment operator at 24:22 ?
    Thank you for these videos, you take the time to present several examples and explain what is going on behind the scenes which I think is often overlooked in many resources teaching C++

    • @MikeShah
      @MikeShah  2 года назад +2

      I meant to say copy constructor. Copy constructor is called when the object is first being initialized from a previously created object. Looks like I mixed my words a bit. :)

  • @kamilziemian995
    @kamilziemian995 9 месяцев назад +1

    Very nice video.

  • @IsaacC20
    @IsaacC20 4 месяца назад

    @28:44 How do you use declspec to check value category of expressions?

    • @MikeShah
      @MikeShah  4 месяца назад

      There's a trick you can use with templates to get the decltype of a type. See stackoverflow.com/questions/16637945/empirically-determine-value-category-of-c11-expression

  • @arjunkandaswamy1532
    @arjunkandaswamy1532 Год назад +1

    && was great

  • @muhammedhasankayapnar9495
    @muhammedhasankayapnar9495 Год назад +1

    Hi Mike, thank you for this wonderful lecture. As far as I understand, rightvalue references can be used for large data processing for the purpose of preventing stackoverflow. How do we see the s3? since rvalue_references do not have any addresses/memory location ? Is that magic :) ?

    • @MikeShah
      @MikeShah  Год назад

      Thank you for the kind words. Can you provide a timestamp for the example?

  • @ARANDOMOPENAIUSER
    @ARANDOMOPENAIUSER 10 месяцев назад

    24:25

  • @joymakerRC
    @joymakerRC Год назад

    needed this one bro

    • @joymakerRC
      @joymakerRC Год назад

      i had come across the exact issue earlier this summer when attempting my mode manager class, couldnt figure out why my funtions wouldnt compile to test correctly when i just entered a hard number. I didnt know all this at the time and just assumed i had to create variables all the time for parameters. I had just recent read In "deciphering OOP in C++" when writing a test driver for a class that this can occur.

    • @MikeShah
      @MikeShah  Год назад

      Cheers

  • @VoidloniXaarii
    @VoidloniXaarii Год назад

    Thanks so much

  • @blaisofotso3439
    @blaisofotso3439 2 года назад

    I forgot to thank you for your hard work and sharing your knowledge.
    Do you have a roadmap to advise for one who wnat to be c++ developper in the embedded system and game developpment.
    i have been scoring the internet looking for books which teach c++ with under the hood concepts and explanations.
    Please advise for books.
    Yours sincerely

    • @MikeShah
      @MikeShah  2 года назад +1

      You are most welcome! I'd recommend "C++ for game programmers" second edition, "C++ API Design" , then any "Building games in C++" style book. Then follow up with Jason Gregory's "Game Engine Architecture" after you have built a few small games. "Computer Systems a programmers perspective" also is a book of high value. That's a good starting list, perhaps I'll do a video on some book recommendations :)

    • @blaisofotso3439
      @blaisofotso3439 2 года назад

      @@MikeShah Thank you Mike for your advise.
      I was asking about books which can teach solid background of c++ in general. I loved your tutorials becuase the pictorial explaiations make it relatively easy to follow, so i have been willing to get a book with such details of explanation. Your recommended list mostly focus on game programming which i would like to do as a hobby to apply and learn c++. My study field is mechatronics. Which is why i asked for books in embedded system domain(Robotics, automation).
      As you said a video would be greatly appreciated.

    • @MikeShah
      @MikeShah  2 года назад

      @@blaisofotso3439 You are most welcome! I will add the video idea to the wish list, as I have a series of education content I would like to make advising some books I have enjoyed :) Your field of study is a good one!

    • @blaisofotso3439
      @blaisofotso3439 2 года назад

      @@MikeShah Thank you .

  • @MrTNVolsFan
    @MrTNVolsFan Год назад

    I get the error: error C2440: 'initializing': cannot convert from 'std::basic_string' to 'std::string &&'

  • @blaisofotso3439
    @blaisofotso3439 2 года назад

    Hallo Mike
    I have tried adding two integers and assigned the result to an Rvalue reference and it has worked. my question to you is :
    we never wrote a move assignment operator, is the compiler doing it behind our back?

    • @MikeShah
      @MikeShah  2 года назад

      For the primitive types, we need not have these implemented for rvalue references (i..e int&& rv_ref = 42;). It' sort of how we never have constructors for 'int' in C++ for example. We'd call 'int' 'long' 'char' etc. trivial types in C++.

    • @blaisofotso3439
      @blaisofotso3439 2 года назад

      @@MikeShah I had that feeling. I was surprised that we perform the move assignement without implementing it. Then i thought that the compiler might have done it as it would when making copy without pointer as data member.
      In all thank you for your comment about the reason why, it helps with understanding the basics. "There is no rules without exception" . "l exception qui confirme la règle"

  • @MrTNVolsFan
    @MrTNVolsFan Год назад

    Concerning the std::string&& s3 = s1+s2, it doesn't work in C++20. Is it deprecated in the modern language?

    • @MikeShah
      @MikeShah  Год назад

      Not sure--what message are you getting with what compiler?

  • @bobweiram6321
    @bobweiram6321 Год назад

    What does the name Shah mean? Does it mean king in Parsi?

    • @MikeShah
      @MikeShah  Год назад

      Depends on the language, but yes, King is one meaning 🙂

  • @damondouglas
    @damondouglas Год назад

    It finally started to click for me at this place ruclips.net/video/pMCHWTwIWns/видео.html

    • @MikeShah
      @MikeShah  Год назад +1

      It's a bit obscure to think of variables as something that stores information and also something with a 'value category' or 'identity' (being lvalue, rvalue, etc.). :)