lvalues and rvalues in C++

Sdílet
Vložit
  • čas přidán 14. 05. 2024
  • The first 1000 people who click the link in the description will get 2 free months of Skillshare Premium: skl.sh/thechernoproject6
    Patreon ► / thecherno
    Instagram ► / thecherno
    Twitter ► / thecherno
    Discord ► thecherno.com/discord
    Series Playlist ► thecherno.com/cpp
    This video is sponsored by Skillshare.

Komentáře • 673

  • @B4RN154N
    @B4RN154N Před 4 lety +441

    The C++ series is definitely my favorite.

  • @besusbb
    @besusbb Před 4 lety +686

    *adds another ampersand*
    H O L Y C R A P
    These videos make me feel like an absolute idiot in C++

    • @adityaverma6120
      @adityaverma6120 Před 4 lety +35

      Lmao he loved this, SAVAGE xD

    • @jd_bruce
      @jd_bruce Před 4 lety +15

      It also blew my mind when he assigned a value to the result of the function... seems pretty useless though.

    • @led23zeppelin
      @led23zeppelin Před 4 lety +88

      @@jd_bruce It's actually really useful. every time you do "vector[2] = 4" you're essentially assigning a value to the result of std::vector::operator[]

    • @TeamSurvivalLP
      @TeamSurvivalLP Před 4 lety +10

      My first thought was why should somebody need that, than he said it's useful with std::move etc... mindblown. Would have saved me some trouble with moving smart pointers

    • @INeedAttentionEXE
      @INeedAttentionEXE Před 4 lety +7

      condescend me daddy!

  • @user-rr5bz4zs5u
    @user-rr5bz4zs5u Před 4 lety +407

    This is the MOST clear explanation of r- and l- values I've ever heard or read

    • @ABaumstumpf
      @ABaumstumpf Před 9 měsíci +2

      And sadly it is wrong on many points cause he does exactly what he said we should not do: Try to come up with his own definitions for l/r-values.
      For example assigning to r-values is allowed and normal. So 6:20 and on are by the standard wrong.
      C++11 defined that L-values have identity, rvalues can be moved from. And an object can fall into both categories - so called xvalue.

    • @pacocarrion7869
      @pacocarrion7869 Před měsícem

      @@ABaumstumpf but I don't understand L-Values o R-Values. If I have for example:
      -> double&& rref = std::sqrt(36.0);
      is the same than:
      -> double rref=6;
      So the variable called "&& rref" I can use it as a normal variable. So whats the point with L-Values o R-Values??? at the end both variables are the same.

  • @TheSenorTuco
    @TheSenorTuco Před 4 lety +79

    Everybody: blown mind
    Cherno: bored so he has to fiddle with paprika

  • @Tom4ick
    @Tom4ick Před 4 lety +149

    Hey Yan, just wanted to say that thanks to you I got a final 95 grade in Systems Programming course in my university (Computer Science BSc). Thanks a lot! I'm 100% sure that you have the best programming series. period.
    As a side note, no one EVER got me this excited to code and study :)

  • @maximorlov8208
    @maximorlov8208 Před 4 lety +130

    The core explanation starts at 3:55.

    • @TWiStErRob
      @TWiStErRob Před 4 lety +17

      Just wanted to write the same, so much of "the thing I'm going to talk about", so much time wasted.

    • @II_xD_II
      @II_xD_II Před 3 lety

      just wanted to type the same

    • @aishwarya1895
      @aishwarya1895 Před 3 lety +1

      Thnx😂

    • @bcuz8998
      @bcuz8998 Před 3 lety

      THANK YOU

    • @littiliom
      @littiliom Před 3 lety +6

      @@TWiStErRob it's only 4 minutes of video lmao. rest of 10 minutes took me like half an hour, i was playing around with code and googled additional stuff. not much time wasted. pretty sure it was painful to read, but yo get the point

  • @leixun
    @leixun Před 4 lety +136

    *My takeaways:*
    1. lvalues vs rvalues 4:25
    2. Main advantages 11:35
    3. lvalues are variables with some kinds of storage backing them, rvalues are temporary values 12:25

    • @ikemkrueger
      @ikemkrueger Před 2 lety +7

      For me lvalues == "Location", rvalues == "tempoRary".

    • @duality4y
      @duality4y Před rokem

      @@ikemkrueger what about a global on the right side.

    • @Timo-Epis
      @Timo-Epis Před 9 měsíci +1

      @@ikemkrueger That was the first thing I was thinking when I heard about it aswel !

  • @cipherxen2
    @cipherxen2 Před 4 lety +302

    lvalue has two components 1) value 2) address
    rvalue has only 1) value
    -----
    rvalue is allowed only on right hand side on assignment
    only lvalue is allowed on left hand side of assignment
    lvalue is allowed on right hand side of assignment
    -----
    On right hand side of assignment
    1. As rvalue only has value, it's value is used
    2. As lvalue has both value and address, what gets used depends on left hand side expression
    On left hand side of assignment
    1. Only address part of lvalue is used
    2. As rvalue doesn't have address, it cannot be used
    -----
    rvalue reference is just an optimization hack.

    • @fredoverflow
      @fredoverflow Před 4 lety +5

      rvalues of class type DO have and address and CAN appear on the left hand side of assignment: std::string("hello") = "bye";

    • @cipherxen2
      @cipherxen2 Před 4 lety +12

      @@fredoverflow ever heard of copy constructor!?

    • @cipherxen2
      @cipherxen2 Před 4 lety +13

      @@fredoverflow or operator overloading for that matter?

    • @fredoverflow
      @fredoverflow Před 4 lety +3

      Speaking of operator overloading: If you have two string variables x and y, then the rvalue x+y can also appear on the left hand side of an assignment, i.e. x+y="wat" is perfectly valid (albeit nonsensical) C++.

    • @cipherxen2
      @cipherxen2 Před 4 lety +15

      @@fredoverflow it has nothing to do with lvalue or rvalue. It's just syntactic sugar for calling function named "operator=". lvalue/rvalue distinction is applicable for assignments only, and not for overloaded = operator.

  • @hanscagayan7260
    @hanscagayan7260 Před 4 lety +12

    I am not a programmer/software developer/coder. I'm not even sure I am referring to the right profession. I got here because I watched the reaction to unreal engine 5 and the ps5 (part 1). The way you explained things in those two videos got me hooked and now I'm watching a video on C++. I am a doctor and a lawyer and C++ is totally alien to me. But I found myself watching this video and "understanding" the main concepts you wanted to get across, which is a testament to how well you can grasp the important lessons and convey them to your audience. Made me want to study programming now (my wife would kill me).
    I went to a high school in the Philippines and back in 1982, it was one of the first high schools in my country which taught programming. We were taught how to program in "basic". A lot of my classmates became programmers and are now working in the US or have their own software companies. I went down a different path but if I had you as a teacher, who knows, I might now be working on dynamic global illumination and triangles, etc.
    You should try to fit in a teaching job in between your project development schedules. A lot of programmers/software developers/coders would benefit from your teaching and hopefully, someday come up with something special themselves. You would have the satisfaction of having a hand in shaping the minds of these future superstar developers.You can inspire them.

    • @leonardodavinci4259
      @leonardodavinci4259 Před rokem

      Just checking in to see whether you've made a career switch haha

    • @siddharthchillale6698
      @siddharthchillale6698 Před rokem

      You are a doctor AND a lawyer?! AND now you are also learning programming?! Your parents must be damn proud of you.

  • @williamtetlie4274
    @williamtetlie4274 Před 4 lety +1

    I've been waiting for this! Thank You! :)

  • @DaveMackinschleavy88
    @DaveMackinschleavy88 Před 4 lety +1

    Cherno, thank you so much! Your videos are awesome, and I can't wait for move semantics.

  • @dingalong14
    @dingalong14 Před 4 lety +3

    Woah, what a coincidence, I was just looking for a cohesive explanation of these yesterday! Great video, keep it up!

  • @FlareGunDebate
    @FlareGunDebate Před 2 lety

    This video answered so many questions that had been shelved in the back of my mind. Thank you.

  • @shashanksharma21
    @shashanksharma21 Před 2 lety

    Your videos are amazing ! Thank you for taking the time and effort to share your knowledge!

  • @chrisstone-streetlightinte5629

    Thanks Cherno! I always wondered what the compiler was referring to in terms of l and r values. Had no idea it was actually a pretty simple concept. As always, thank you!

  • @bo-dg3bh
    @bo-dg3bh Před 2 lety +37

    Chinese is my native language, I read an article in Chinese talking about the same topic and didn't understand. But now it's almost crystal clear to me thanks to this video!
    How do you remember these things if you're not using them every day? This is one of my biggest headaches about learning cpp

    • @AdiPrimandaGinting
      @AdiPrimandaGinting Před 2 lety +11

      Mandarin is harder than C++, haha

    • @bo-dg3bh
      @bo-dg3bh Před 2 lety +2

      @@AdiPrimandaGinting true that..

    • @kilinw
      @kilinw Před 2 lety +3

      ​@@AdiPrimandaGinting Nah mother language is always the easiest. Also chinese is really easy but they just have a nightmare writing system.

    • @yusinwu
      @yusinwu Před rokem +1

      很多中文翻譯的專有名詞,看得很一頭霧水。還不如直接去看原文的code說明教學,比較快理解

  • @007LvB
    @007LvB Před 4 lety +1

    Thank you for explaining this concept so well! Looking forward to your video on move semantics!

  • @mulayvaibhav
    @mulayvaibhav Před 2 lety

    Explained beautifully! appreciate the clarity it provides

  • @GreenFlame16
    @GreenFlame16 Před 3 lety

    Can't find the right words of praise for you. My deepest gratitude and appreciation!

  • @ZiadxKabakibi
    @ZiadxKabakibi Před 4 lety

    I read a lot of articles and watched many videos about it but no one explained it clear and short like this
    Thank you very much :D

  • @nicolascorgier4500
    @nicolascorgier4500 Před rokem

    Thank you Cherno for all this Amazing content. I've learned so much with your C++ series 👍😎❤️.

  • @apenasmeucanal5984
    @apenasmeucanal5984 Před 4 lety

    thanks cherno, i’ve always found this topic to be confusing, but you thankfully made it clearer

  • @CacheTaFace2
    @CacheTaFace2 Před 4 lety

    This was the clearest and most concise explanation of this topic that I have seen. Cheers!

  • @j.r.waheed4610
    @j.r.waheed4610 Před rokem

    Thank you for clarifying this topic. Well done.

  • @mohammadreza.beygifard

    Man, the video was great, it made lots of stuff more clear for me. Thank you for it!

  • @armaghanasghar2911
    @armaghanasghar2911 Před 3 lety

    Really enjoy your work @The Cherno

  • @LunaticaPirate93
    @LunaticaPirate93 Před 2 lety

    Thank you so much! It was a really clean explanation and got all my questions answered :)

  • @levondarbinyan3934
    @levondarbinyan3934 Před 4 lety

    Thank you Cherno, your explanation why we use move semantics is so much better than what I have read from books. You have created a great playlist to understand C++ more.

  • @Max-dj4kd
    @Max-dj4kd Před 2 lety

    I was so daunted by this going into it but your explanation was so good, thanks!

  • @Qualk9000
    @Qualk9000 Před 4 lety

    Thank you so much for your C++ videos! Love them!

  • @timetotango9453
    @timetotango9453 Před 3 lety

    I watched the ad of this videoon purpose bc you really deserve it :) thanks a lot for making this clear in my head!

  • @mikefochtman7164
    @mikefochtman7164 Před rokem +1

    Been watching C++ series for a while. Great stuff. Just an aside, my compliments to you and your partner (if you have one with you) for the very nice home you have. Seeing various furniture, and now the kitchen, you obviously have good taste and done some wonderful things with your home. (see? Some folks DO notice more than just your skills as a C++ tutor 😉 )

  • @saeedmahmoodi7211
    @saeedmahmoodi7211 Před 4 lety +4

    great job, I was looking for it a long time ago and after 1 month of research I finally got all of my answers, this is really good, I want you to explain it fully so other people can save their time :D
    Thanks a lot.

  • @SkyrimCZtutorials
    @SkyrimCZtutorials Před 4 lety +1

    Great explanation as always! Thanks for making these videos!

  • @shreemoyee_art
    @shreemoyee_art Před 3 lety +1

    Awesome video Cherno! Been coding with modern C++ for a few months now, but can FINALLY say I understand rvalues and lvalues! :D

  • @jeremymesloh1981
    @jeremymesloh1981 Před 3 lety

    Yan, you are a rock star! I just subscribed to your Patreon btw.. Look forward to all your knowledge about C++ and many other things too. Take care

  • @dlofiasco
    @dlofiasco Před 2 lety

    Man just following along with this makes things so much easier to understand in class. Thanks again Chrono, I also learned that he is an expert grind presser.

  • @santoshjanakiraman8907

    Really appreciate the good work you're doing!

  • @durumbasa
    @durumbasa Před 4 lety +1

    Thanks Cherno, top quality content as always

  • @KarimOsama89
    @KarimOsama89 Před 2 lety

    Clear to-the-point explanation. Your C++ series saves time googling a specific topic. Keep up the good work.

  • @sidharthsharma8588
    @sidharthsharma8588 Před 2 lety

    Thanks for sharing wonderful stuff Yan

  • @hsaidinsan6345
    @hsaidinsan6345 Před 4 lety +2

    Thanks for the video
    We are really looking forward to see your videos on design patterns 😇

  • @carterfang95
    @carterfang95 Před 3 lety

    Way more clear than the articles I've tried to read.. thanks a lot!!

  • @fr3ddyfr3sh
    @fr3ddyfr3sh Před 2 lety

    You are seriously the only legit free video series for modern C++
    Thanks a lot for partially sharing your knowledge for free!

  • @Sturmtreiben
    @Sturmtreiben Před 4 lety

    I know about that topic and advanced C++ concepts like move semantics already, but have to admit you do a pretty good job at explaining stuff like that to beginners. Great work. Hope you explain one day about why passing parameters by value and letting the compiler chose what to do might be good for performance. There’s so much to learn about C++, that’s why I like it.

  • @gongchen7308
    @gongchen7308 Před rokem

    I love your demonstrations, it clarifies everything

  • @pharos640
    @pharos640 Před 9 měsíci +1

    Every tutorial you make is so informative and so well made. Thank you for making those videos free for all people.

  • @ryanb159
    @ryanb159 Před 4 lety +6

    This has plagued me for long, and I've been programming in C++ (03) for years. Thank you!

  • @mykalesalad
    @mykalesalad Před rokem

    This was a very clear explanation, thank you. I'm about to watch the move semantics video.

  • @bharanidharans5857
    @bharanidharans5857 Před 2 lety

    Great Explanation. Thanks for making it clear & stronger understanding

  • @magessm5228
    @magessm5228 Před 4 lety +1

    Great explanation Mr. Черников
    Rly like your way of explaining things
    Thank you very much for the info and knowledge you provide : )

  • @h.hristov
    @h.hristov Před 4 lety +1

    Thank you. Expecting to see a video on move semantics soon

  • @deutschWallah
    @deutschWallah Před rokem

    Thanks a lot Yan. This C++ series is really the best on CZcams :)

  • @coen44444
    @coen44444 Před 3 lety +1

    Extremely well explained, calling lvalues 'location values' entirely cleared up the cloudiness about this subject for me! Amazing job!

  • @macewindont9922
    @macewindont9922 Před 3 lety

    Thanks a lot for these C++ videos. They helped me out with a project at work that turned out great. I ported code from Python to C++ and saw a 10x speed improvement!

  • @chrismakesgames9075
    @chrismakesgames9075 Před 3 lety +6

    I'm learning C++ and immediately came across this syntax. The two books I have on C++ didn't explain rvalues/lvalues as well as you did with your examples. Thanks!

  • @marbangens
    @marbangens Před 4 lety +1

    I love to listen when you talk about c++ don't stop pleas. This is gold.

  • @juststudy3703
    @juststudy3703 Před 2 lety +1

    Definitely one of the best detailed explanation on l and r values in c++
    Thanks!

  • @jackwu
    @jackwu Před 4 lety

    Helps a lot!!. This is very clear explanation, especially the provided sample code example.

  • @jessprogany4345
    @jessprogany4345 Před 3 lety +1

    loving your C++ videos
    hope to see more of it

  • @tehLyrex
    @tehLyrex Před 4 lety +119

    If I remember correctly, then Titus Winter (or was it Herb Sutter?) gave
    a pretty good talk at CppCon some time in the past where he talked
    about all kind of value types. Very simply spoken, he just said:
    Everything that has a name is a lvalue, everything that doesn't is a
    rvalue. In detail it sure gets more complicated than that, but I found
    that explanation quite intuituve and maybe it helps somebody.

    • @oracleoftroy
      @oracleoftroy Před 4 lety +16

      I like STL's (Stephan T. Lavavej) explanation, if you can get its address, its an lvalue, otherwise its an rvalue. This covers cases where an lvalue might not have a name, like as the result of a getter function call.

    • @MatthijsvanDuin
      @MatthijsvanDuin Před 4 lety +1

      @@oracleoftroy I was going to object "you can take the address of an xvalues (which are rvalues but not prvalues)" but I wisely tested that before posting and.... you actually can't! even though an xvalue is an already constructed object that _has_ an address, turns out the compiler won't let you use the address-of operator on it.... TIL

    • @rvoros
      @rvoros Před 4 lety +1

      @@oracleoftroy well...
      how about this:
      #include
      void f(const int& i) {
      const int* p = &i;
      std::cout

    • @oracleoftroy
      @oracleoftroy Před 4 lety

      @@MatthijsvanDuin I wouldn't be surprised if there are exceptions, the goal after all is to have a reasonable approximation that works in many situations. STL's version works remarkably well in practice.

    • @oracleoftroy
      @oracleoftroy Před 4 lety

      @@rvoros No, *i* has an address on the stack that you are printing out. An rvalue like *10* does not have an address. Even change the parameter to *const int &&i* and *i* will itself be an lvalue that takes an rvalue.

  • @jayys7745
    @jayys7745 Před 3 lety

    Wow this was very helpful & straight to the point ! thank you !

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd Před 4 lety +1

    Welcome Back Champ!! Thanks for the informative video on this topic,most of the online sites are quite confusing but you made it quite clear.Please post more videos on advanced C++ topics such as lambda function , constexpr , and new feature of C++ 17 and C++ 20....Also could you please suggest online materials for c++ that you refer usually?
    Thanks.STAY SAFE HOME!

  • @HungryEagle2610
    @HungryEagle2610 Před měsícem

    Yan! You rock! Even though this video was posted 4 years ago it is still relevant and to the point. There is still no resource on the whole internet who can explain it smoothly. Love the videos! You rock!

  • @ramitgupta9919
    @ramitgupta9919 Před 3 lety

    I am a big fan Just love the videos I love that you give a reason for everything

  • @neerajbute2094
    @neerajbute2094 Před 4 lety

    For the love of god, please don't stop making videos.
    Btw , would like to see more on move semantics in c++. Thanks for the content.

  • @LokeshKumar-os6ux
    @LokeshKumar-os6ux Před 4 lety

    you nailed it man, please make more videos like this.

  • @kennithsalado7212
    @kennithsalado7212 Před rokem

    Quick and direct to the point, really great.

  • @befikerbiresaw9788
    @befikerbiresaw9788 Před 3 lety

    Dude you really have a gift for explaining stuff. C++ and opengl programming has become very easy for me thank you

  • @khangnguyenhuu2814
    @khangnguyenhuu2814 Před 3 lety

    Thank you so much, i've only started learning C++ recently and this is just what i need

  • @TusharHaral
    @TusharHaral Před 4 lety

    *You just make it so simple. Thanks. BTW the large font used in IDE makes reading code easy to viewers. Nice.*

  • @mahdishirazi5785
    @mahdishirazi5785 Před 2 lety

    one of the videos worth watching over and over again (and thanks so much for that double ampersand, for a whole year i thought it meant reference of a reference and that made me confused af xD)

  • @jaimebaron3299
    @jaimebaron3299 Před 4 lety

    Wow, I´ve had problems in my code, and I had no idea why it didn´t work, and noticed that it only worked on saved variables.
    Thanks to this, you´ve basically solved my problem, and can now edit it to make my some work.
    Thank you very much ^^

  • @Vermilicious
    @Vermilicious Před 4 lety +1

    I had forgotten about this topic, but you made it clear and quick.

  • @diegosolis9681
    @diegosolis9681 Před 4 lety +1

    C/C++ are my favorite programming languages. I love to learn new things about them and the way this clarifies lvalues and rvalues is absolutely fantastic, thanks a lot.

  • @kacperozieblowski3809
    @kacperozieblowski3809 Před 4 lety

    that just made me understand everything that I couldn't with c++ reference. you're a wizard. thanks

  • @jonathanmoore5619
    @jonathanmoore5619 Před 4 lety +2

    Always clear, always good. Thanks

  • @jammeri
    @jammeri Před 3 lety

    Very nice explanation about this topic. Really cleared it up.

  • @commercial3750
    @commercial3750 Před rokem

    This was extremely helpful. I wish I found this video series earlier in my quarter, it would have helped me not bomb my first midterm lmao. Thanks to you though, there's still hope for my final. Thank you so much

  • @iluvyunie
    @iluvyunie Před 4 lety +31

    Digging the covid beard
    Also probably the most helpful channel I've found on youtube in regard to c++.
    If anyone ever asks me anything about this sort of programming this is straight where they're getting sent.

  • @silverqx
    @silverqx Před 3 lety +1

    Best sum up about glvalues and prvalues I saw, nicely described what is going on and what consequences and benefits it brings. ♥

  • @thestarinthesky_
    @thestarinthesky_ Před 4 lety

    Thank you it has been SUPER helpful!

  • @farzadfaradjizadeh3916

    As always, very useful!
    Thanks a lot!

  • @lilgohan
    @lilgohan Před 2 měsíci

    this is one thing I thought I'd never understand, but, as with most topics, you made it easy. thanks

  • @miyazakizachary5108
    @miyazakizachary5108 Před 2 lety

    Genius at work. Thanks a lot!!!

  • @sasamilenkovic6053
    @sasamilenkovic6053 Před 4 lety

    This C++ series is excellent. Thank you TheCherno!

  • @barjeshbarjesh8215
    @barjeshbarjesh8215 Před 2 lety +1

    The explanation can't be simpler than what Cherno mentioned. I am totally impressed.

  • @xuxusito
    @xuxusito Před 4 lety +9

    I'm actually learning c++ since 2 months now and this kind of Videos about c++ makes me happy that I decided to pick up c++ . I started with c and python because it was recommended everywhere but with python it was like owning a iPhone or working with windows. It's very simple to use and intuitive but you just don't feel satisfied. C++ is like switching to android or linux. You are overwhelmed by the freedom and possibilities and it just feels amazing to learn all the things and feel the power of that systems. I hope that makes sense.

  • @adamshield5029
    @adamshield5029 Před 4 lety +14

    I use L and R values all the time, have dealt with move semantics, etc. Clicked the link because I knew I always learn something new from you anyway. Did not disappoint. Excellent presentation of this material.

  • @averageguy985
    @averageguy985 Před 4 lety +1

    Thanks for making C++ so easy for me. Really appreciate all your hard work in it ^_^

  • @georgioskatsanakis1711

    very good explanation man! Good work.

  • @khanhnguyeninh1481
    @khanhnguyeninh1481 Před 6 měsíci

    Thanks so much for your work!

  • @NimrodGoldberger
    @NimrodGoldberger Před 2 lety

    Thank you, I'll need to revisit this but it does feel clearer!

  • @sasa23290
    @sasa23290 Před 3 lety

    Perfect. Fantastic topic. This topic is very tied to move semantics as you already said. C++ is a great language because a programmer can manually do whatever he wants to reduce copying, manipulate with memory, do some optimization but if there is no awareness of that, using c++ has no much sense. It is like you want to clean your bathroom but with no special reason to do that.
    Good job!

  • @petrkassadinovich2705
    @petrkassadinovich2705 Před 3 lety

    Extremely useful and well explaned, thank you!

  • @akshaynaik4888
    @akshaynaik4888 Před 4 lety

    I was trying to understand this a year ago but left it there because too much information, this 10 minutes video made it crystal clear, thanks

  • @randomWork111
    @randomWork111 Před rokem

    Crisp and clear. Thanks Cherno

  • @jaspreetgrewal1931
    @jaspreetgrewal1931 Před 4 lety

    you are amazing...keep uploading such good topic videos.

  • @saurabhsingh-gd6qr
    @saurabhsingh-gd6qr Před 3 lety

    Never understood lvalue and rvalue so clearly before this video .Thank you ...very informative video

  • @danbhakta
    @danbhakta Před 4 lety

    My God man. Watching C++ again brings back nightmares. Subbed!