Advanced C++: Understanding rvalue and lvalue

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

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

  • @alfredli4032
    @alfredli4032 2 года назад +14

    Ten years later and this channel still best channel explaining modern c++ in a concise and informative way.

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

    Probably the best explanation of l/r-values I have seen.

  • @jnana2306
    @jnana2306 10 лет назад +50

    really clear my doubts i have been a c++ programmer for 7+ years of experience but really u cleared my doubt now

  • @tanyixuan1227
    @tanyixuan1227 7 лет назад +55

    best explanation on lvalue / rvalue ever!

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

    By far the best explanation of lvalue and rvalue on RUclips. Thanks Bo!

  • @leemurcha3052
    @leemurcha3052 6 лет назад +7

    This was incredibly helpful and well-explained. It was like you answered every question I was going to have, and clarified anything that could have been confusing.

  • @matteodevellis6266
    @matteodevellis6266 5 лет назад

    This is the simplest way of explaining what r-values and l-values are. Tysm

  • @00gamer2
    @00gamer2 10 лет назад +9

    Wonderfully clearly and simply explained. This video is an asset to programmers _everywhere_. Thank you.

  • @saikirangattu2924
    @saikirangattu2924 7 лет назад +14

    u r genius.. Though the concept is simple it was really confusing to understand when you read other material. This video made life easy...

  • @marjoripomarole
    @marjoripomarole 10 лет назад +16

    Bo, your videos are concise, clear and very useful! Thanks for the hard work.

    • @Wonderspark173
      @Wonderspark173 10 лет назад +2

      This video is quite nice actually. It explains where people might falter, just after reading a few lines, and gives counter examples to explain the point better.
      The video about move semantics is good as well.

  • @bougpan88
    @bougpan88 7 лет назад +128

    lvalue = logical address value , rvalue = register value

    • @jimothyus
      @jimothyus 7 лет назад +16

      this comment helped me more than the vid thanks

    • @TheKoffrig
      @TheKoffrig 7 лет назад +2

      How about lvalues as "Location (memory)" values? Better than "logical address".

    • @leemurcha3052
      @leemurcha3052 6 лет назад +2

      Actually, according to the video that would be wrong, since rvalue is a definition by exclusion. An r value is what an lvalue isn't

    • @stendall
      @stendall 6 лет назад +12

      There should be an option in RUclips to flag incorrect comments like this. What you've written sounds great...and maybe I'm wrong, if so, please post a link to direct me to a source for your comment. But l and r are closer to left and right, than what you wrote. The l and r derive from left and right hand sides of an assignment. Note, I used the word *derive*. It's more involved than just left and right, but that's where it comes from. msdn.microsoft.com/en-us/library/bkbs2cds.aspx

    • @kejianshi9196
      @kejianshi9196 6 лет назад +2

      l and r is for left and right. lvalue means something to be the left side operand of assignment operator =

  • @anamsakalonu4518
    @anamsakalonu4518 17 дней назад

    I never really knew what a pointer is until now. You just explained pointers without talking about it.

  • @prabhneetsingh13
    @prabhneetsingh13 4 года назад

    Hands down the best explanation of L-value and R-value, totally cleared my doubts

  • @Abdullah-mg5zl
    @Abdullah-mg5zl 10 лет назад +15

    Well explained, I was having trouble understanding this in my lectures. Thanks!

  • @xyzzyx9357
    @xyzzyx9357 4 года назад +1

    AWESOME 👍👍👍👍

  • @steve122288
    @steve122288 8 лет назад

    very interesting. rvalues are things i had alot of questions about when learning c++, without knowing there was a term for them

  • @pdd3
    @pdd3 5 лет назад

    I marvel at the intelligence of Bo Qian. He is the most amazing teacher of anything I've found on the Internet.

  • @sheepman6291
    @sheepman6291 6 лет назад +5

    Notes from the video
    Every C++ expression yields either a R-value or an L-value.
    If the expression has an identifiable memory address, it’s L-value otherwise; it’s an R-value
    L-value Examples:
    int i; //i is a L-value
    int* p = &I; //i’s address is identifiable
    i = 2; //memory content is modified
    class dog;
    dog d1; // L-value of user defined type (class)
    // Most variables in c++ code are L-values
    //R-value Examples:
    int x = 2; // 2 is an R-value
    int x = i+2; // i+2 is an R-value;
    int* p = &(i+2);// error
    i+2 = 4; // error
    2=i; Error
    Dog d1;
    d1 = dog(); // dog()is R-value of user defined type (class)
    int sum(int x, int y){return x+y;}
    int i = sum(3,4);// sum(3,4) is R-value
    const is an L-value and is not modifiable
    //R-values: 2,i+2,Dog(),sum(3,4),x+y
    //L-values: x, I, d1, p
    //Reference (or lvalue reference):
    int i;
    int& r=i;
    int& r =5; //error
    //Exception: Constant L-value reference can be assign an R-value;
    const int& r = 5;
    int square (int& x){return x*x;}
    square(i); //ok
    square(40); //error
    //Workaround:
    //////////////////////////////////////////////******** Remember ********////////////////////
    int Square(cons tint& x ){return x*x;} // square(40) and square(I) work no errors this is really cool because it explains the need for the const keyword
    //L-value can be used to create an R-value
    int I =1;
    int x= i+2
    int x =i;
    //R-value used to create a L-value
    int v[3];
    *(v+2)=4;
    //Misconcetion 1 : function or operator always yields r values.
    int x = I +3;
    int y = sum(3,4);
    int myglobal;
    int&foo(){return myglobal;}
    foo() = 50;
    array[3]=50; //operator[] almost always generates l-value;
    //Misconception 2 : L-values are modifiable
    // c language: L-value means “value suitable for left-hand-side of assignment”
    // this definition is no longer true for C++ because L-value is not modifiable
    const int c = 1; // c is a L-value
    c=2; // error, c is not modifiable
    //misconception 3: R-values are not modifiable.
    i+3 = 6;//error
    sum(3,4)=7;//error
    //it is not true for user defined type (class)
    class dog;
    Dog().bark(); // bark() may change the state of the dog object;

  • @GodWentAFK
    @GodWentAFK 5 лет назад

    Glad we're covering this in the 4th week of my C++ class.

  • @dingoDogMan
    @dingoDogMan 5 лет назад

    Thank you for this explanation. Its important to understand these terms because I find they've really helped me out with debugging and understanding errors and warnings. When you understand the errors/warnings it can save you so much time.

  • @VadixeM
    @VadixeM 6 лет назад

    Thank you a lot. Made things clearer. I was always confused about rvalues and lvalues. Now from what I've seen so far: lvalue = any object which location we can get in memory, rvalue = everything else.

  • @oleksijm
    @oleksijm 5 лет назад +4

    YT algorithm in 2019.. thank you for finding this for me.

  • @samuqsith
    @samuqsith 7 лет назад

    Brilliant simplicity in explaining !! Love your precisely manner of presentation.

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

    Mind Blown!! I definitely get what it is. I had such a hard time understanding the move operator. This made it easy.

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

    awesome!

  • @barnarddale
    @barnarddale 4 года назад

    Best/only explanation that I've been able to grasp. Thanks!

  • @SuperYouVIN
    @SuperYouVIN 11 лет назад

    Thank you very much. Thanks to you, I don't have the brain ache over pages of C++ book that doesn't make much sense.

  • @ImperialCraftGaming
    @ImperialCraftGaming 9 лет назад

    Incredible explanation, thank you! Your videos are an incredible asset for anyone willing to dive into the deep with C/C++

  • @charoniv5631
    @charoniv5631 3 года назад +1

    Thats good

  • @JannisAdmek
    @JannisAdmek 5 лет назад

    this is ingenious! superb explanation and I really like your simple style!

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

    best c++ video

  • @TwoLazyTurtles
    @TwoLazyTurtles 11 лет назад

    Finally I understand the concept, I do not know why my c++ book does not define it as you have, lvalues have a identifiable memory address...

  • @matt_7670
    @matt_7670 8 лет назад

    Very clear with great examples. Thank you!

  • @HomoSiliconiens
    @HomoSiliconiens 6 лет назад +1

    The concepts of lvalue and rvalue were first introduced in C language, then C++ adopted the these until C++11 standard. These are now called value category.
    glvalue: lvalue + xvalue
    rvalue: prvalue + xvalue
    The C++ language is evolving rapidly these years. So, new concepts are introduced with new standards, and some features become deprecated.
    glvalue, lvalue, prvalue, xvalue... constexpr, constexpr ... etc. become crystal-clear, once you understand Assembler language. In fact, these terms are not necessary at all if you understand Assembler. All these strange terms are introduced to help those people understand lower-level CPU operations in C++ programmer s perspective.
    All of those advanced concepts of C++ can never be fully understood without some level of Assembler language experience.

  • @jasonbourne3569
    @jasonbourne3569 7 лет назад

    ~38k+-1 million people knows about lvalue and rvalue including yourself, Bo Qian and Bjarne.Thank Qian and Bjarne

  • @rammaheshwari3008
    @rammaheshwari3008 6 лет назад

    Great Lecture ❤

  • @Kris-ne3ik
    @Kris-ne3ik 6 лет назад +1

    Just amazing!

  • @lollopoplogo
    @lollopoplogo 3 года назад

    Thanks Sensei! This helped me understand rvalue and lvalue!

  • @cpubug
    @cpubug 3 года назад +1

    Seriously really really really really thank you 💝

  • @lingarajmarapli9094
    @lingarajmarapli9094 11 лет назад

    Thanks a lot :) .. It's really nice and precise information about the concept...

  • @ManyStandAlone
    @ManyStandAlone 6 лет назад

    hey bo, thanks for the time and effort you put into this. Saved my life.

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

    Awesome work! Thank you very much!

  • @kavithadinesh
    @kavithadinesh 9 лет назад

    Super. Thanks a lot Bo. I love your videos

  • @VadixeM
    @VadixeM 6 лет назад

    Generally, rvalues are values whose address cannot be obtained by dereferencing them, either because they are literals or because they are temporary in nature (such as values returned by functions or explicit constructor calls).
    Taken from www.cplusplus.com/reference/utility/move/

  • @MisterItchy
    @MisterItchy 4 года назад +3

    Just when I thought I understood it, I find out I don't. Still, learning is a process.

  • @sixpetrov
    @sixpetrov 10 лет назад

    Thank you Bo, fantastic video

  • @whiteraimentevangelism
    @whiteraimentevangelism 4 года назад

    They were defined this way because they were looking at the left side of and Equal and the Right side of the equals. Thus lvalue and rvalue. But rvalue are in expressions and other places.

  • @ramprakashjelari7385
    @ramprakashjelari7385 11 лет назад

    Really impressive, and good luck ;).

  • @JordanHolcombe
    @JordanHolcombe 6 лет назад

    very nice. thank you.

  • @OhLawdyLawdy
    @OhLawdyLawdy 7 лет назад

    Awesome video! Thanks Bo!

  • @elyxthelynx
    @elyxthelynx 5 лет назад

    These videos are so helpful! Thank you!

  • @piriyaie
    @piriyaie 6 лет назад

    Thanks a lot. This video helped me a lot.

  • @Dan9CoasterFan
    @Dan9CoasterFan 11 лет назад

    This was very helpful, thanks.

  • @tcbetka
    @tcbetka 11 лет назад

    Very helpful review. Thanks!

  • @DigaoParceiro
    @DigaoParceiro 8 лет назад +3

    Absolute greate!

  • @ViBol-sr7qe
    @ViBol-sr7qe 2 года назад

    Good

  • @wattheshet
    @wattheshet 10 лет назад

    WOW.. Thanks bro. Im really confuse about the lvalue and rvalue thingy. How about explaining the concepts of constant? i believe constants are a major factor in c++

  • @onthegoprint
    @onthegoprint 10 лет назад

    very informative ty

  • @Rupam12345
    @Rupam12345 11 лет назад

    Good one thanks !

  • @eatyourspinachtomperez8700
    @eatyourspinachtomperez8700 7 лет назад

    Thank you for the insight.

  • @leejunbeom9265
    @leejunbeom9265 5 лет назад

    this is lovely

  • @jonkrieger5271
    @jonkrieger5271 9 лет назад

    Thanks Bo that was super helpful, very appreciated!

  • @1schwererziehbar1
    @1schwererziehbar1 5 лет назад

    Thank you, Bo Qian!

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

    Thank you master

  • @Abinashdash17
    @Abinashdash17 4 года назад +1

    9:45 me : "...this is a weird looking code . I never write code like this." Bo Qian: "what abt array[3] = 50?" me: speechless...

    • @zishiwu7757
      @zishiwu7757 4 года назад +1

      If you create a custom class that is similar to a sequence container, one thing you can implement in your class is an overloaded subscript operator that returns a rvalue reference to the sequence at specific subscript index so that you can modify the value at that index of the sequence. For example, suppose you create a class called ExamScores that stores a list of exam scores using a member variable that is an private int pointer called m_ptr. Furthermore, suppose you want to treat an object of class ExamScores as if it were an array instead of having to call a setter function like:
      void setScoreAtIndex(int index, int value) {
      m_ptr[index] = value;
      }
      Then your class would need an overloaded subscript function like this (note this example disregards invalid index):
      int& ExamScores::operator[](int index) {
      return m_ptr[index];
      }
      The overloaded function will allow you to treat an object of class ExamScores as if it were an array:
      int size = 3;
      ExamScores scores{size};
      scores[0] = 92;
      scores[1] = 75;
      scores[2] = 88;

  • @waroracle2692
    @waroracle2692 9 лет назад +1

    what about function? you can get the address of the function. is it rvalue or lvalue?

  • @judgebot7353
    @judgebot7353 3 года назад

    nice

  • @smailgourmi
    @smailgourmi 3 года назад

    Thank you for this explanation, i would like to ask you a question .
    Is it possible to cast a lvalue ?
    Ex :
    I declare a variable i as integer and at certain point in my code i want i to be char . Is it possible to do that casting on lvalues ?
    Thank you

  • @TheHTMLCode
    @TheHTMLCode 8 лет назад

    At 8:10 when you're talking about x = i; and asked if i is an lvalue or an rvalue. I understand the implicit conversion as we're making a copy of i and storing it in x, therefore x is the lvalue and i is an implicit rvalue, but if we did x = &i; would both x and i be an lvalue, as we're setting x to its address? Or would i still be the rvalue? Thanks :)

  • @MrBaltonic
    @MrBaltonic 8 лет назад

    Nice tutorial, ty

  • @RA_I_AM
    @RA_I_AM 6 лет назад

    Thank you Sifu!

  • @mohammadghanatian114
    @mohammadghanatian114 8 лет назад

    Great,Thank you bro!

  • @alojzybabel4153
    @alojzybabel4153 4 года назад

    02:44 I don't think that modifyability matters here at all. `i` can be defined `const`, making it impossible to modify, but it won't change its lvalue status, because it technically still does have an address and it is referred to by a name in our code.
    03:30 I think that you're putting the cart before the horse right here: The fact that you can apply an `&` operator to it, doesnt necessarily mean that it is an L-value. It's the other way around: WHEN something already IS an R-value, THEN you can apply `&` to it, or assign to it. But you have to find out FIRST if a certain expression is an L-value, using the _actual_ language rules about R/L-values.
    03:44 Well, here we have a built-in type (an integer), and a numeric literal. But what if `i` was an object of some user-defined type that has an overloaded `operator+` that returns a non-const temporary? Technically, we can still assign a new value to it, right? Otherwise, things like this wouldn't be possible:
    std::complex a{2,3};
    std::complex b = (a*3) += 5;
    std::cout

  • @sathishsampathirao1278
    @sathishsampathirao1278 7 лет назад +2

    Hello Bo Quian , AT 8:20 ...How (V+2) become R Value...if it is having an Identifiable address...hence i can do int *ptr = V + 2;

    • @vinexgamemaking
      @vinexgamemaking 6 лет назад

      There is a difference between actually taking the address of a variable (with the address-of operator, &) and simply assigning an arbitrary value to a pointer variable. You are doing the latter, which not only is not an rvalue, it's also almost definitely going to give you an access violation if you try to dereference that pointer.

    • @kejianshi9196
      @kejianshi9196 6 лет назад +1

      this is pointer arithmetic *(v+k) is just v[k] which is clearly an identifiable address on the heap

  • @ErichDAtriGuiran1
    @ErichDAtriGuiran1 6 лет назад

    thanks for this.

  • @joeydicastro
    @joeydicastro 6 лет назад

    Thanks a lot.

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

    For people coming from other (OOP) languages, the question remains: so what? Why do we care about rvalue and lvalue? Some illuminating examples can be greatly helpful.

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

      move semantics

  • @29srinath
    @29srinath 8 лет назад

    @11:23 the slide meant to say Misconception 3: rvalues are modifiable

  • @najehmchirgui7968
    @najehmchirgui7968 8 лет назад

    Thanks you so much

  • @adithyakaravadi8170
    @adithyakaravadi8170 7 лет назад

    Thanks, how do you learn all these? Reading books?

  • @fanalgo
    @fanalgo 9 лет назад

    if 50 is an rvalue, you explained that the only way that an lvalue reference can be assigned by an rvalue is if that lvalue reference was declared constant, but your function foo() doesn't have const int& declared as the return type it's just int&. How can I assign a non-constant lvalue reference to an rvalue??? seems contradicting?

  • @rcitaliano
    @rcitaliano 6 лет назад +2

    at 4:56 you forgot to type in "y" which is an rvalue too, correct?

    • @MsJavaWolf
      @MsJavaWolf 5 лет назад

      y is an lvalue I think. You can assign something to y (even if it's a bit pointless because it's passed by value, but you still can) , so by my understanding it must be an lvalue.

  • @yahortsaryk2842
    @yahortsaryk2842 6 лет назад

    thanks a lot

  • @seyalg
    @seyalg 11 лет назад

    Please, if u can explain the c++11 with gdb debugger using unix OS.Furthermore, To be specific i need these things to be explained. If u have time,,,,,Compilation
    • Use g++11 to compile with C++ 2011 standard.
    • Use w++11to in addition include all warnings commonly good.
    • Useg++11filterto in addition filter the errors through gccfilter
    .• Use w++11 filter to use both warnings andgccfilter
    .• Usebcheck ./a.out to check for memory leaks when you run your program.
    A short and simple program will work for me.

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

    You have to return value in your mind

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

    I believe you have forgotten to add "sum" (that is, the function itself) to the lvalue list. Am I wrong? While the function call sum(3,4), that is, the result of its call, is a rvalue, I think the function itself (function pointer) is a lvalue.

  • @EvilRobin1
    @EvilRobin1 11 лет назад

    Thank you!

  • @sarbajitg
    @sarbajitg 5 лет назад

    Is the concept of Lvalue and Rvalue in c and in c++ are same?

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 5 лет назад

    Well, if I know right, you can have a pointer point to a function. Doesn't that make functions lvalues?
    They have a address in the memory, don't they?

    • @konstantinrebrov675
      @konstantinrebrov675 5 лет назад

      A function pointer is lvalue. However, a function call, more specifically what it returns, like sum(3, 4) may be either lvalue or rvalue depending on the return type int& or int. When you call a function you deference the function pointer. The name of the function itself is the function pointer. cout

  • @yotty97
    @yotty97 6 лет назад

    why does dog() not have an identifiable memory address? it surely does ,and if i spat out the assembly i'd see it, no?

  • @anirbansaha7987
    @anirbansaha7987 3 года назад

    In misconception 1: is not foo returning a lvalue reference of lvalue myglobal ? Assigning 50 to lvalue reference should generate error unless we use const? But it compiles fine. Can someone help me understand this?

  • @rafaelmoura8688
    @rafaelmoura8688 9 лет назад

    +Moshe Rabaev . couse function foo() does not return a non initialized int&, Its returning a reference to the global variable "myglobal" , myglobal can be assigned with rvalue 50

  • @enricomariadeangelis2130
    @enricomariadeangelis2130 6 лет назад +1

    Make your Vim more visible and spread the word! :D

  • @tus62
    @tus62 5 лет назад

    can an array be a l-value or not

  • @lancelofjohn6995
    @lancelofjohn6995 3 года назад

    I don't understand in the part int i=4; int x=i why i is rvalue?i has its own address to store 4.

  • @tonyrosam
    @tonyrosam 10 лет назад

    For Lvalue, when you say memory do you mean the RAM or ROM?

    • @LapsedUnity
      @LapsedUnity 10 лет назад +5

      RAM. ROM is only used to help the system boot into an OS. It has nothing to do with this.

    • @GlyGlue
      @GlyGlue 8 лет назад

      +LapsedUnity what about const variables?

  • @vulko
    @vulko 7 лет назад +1

    In the last example "dog" suddenly became an rvalue, while it should be lvalue as it has to be defined like "Dog dog;" where Dog is a class.
    Even though dog is modifiable if some method changes it's state, it's still and lvalue, not an rvalue.

  • @jasoncole7711
    @jasoncole7711 5 лет назад

    A string constant has an identifiable memory address, though, and it is not a lvalue ;)

  • @chrisevers2084
    @chrisevers2084 8 лет назад

    Why is constructor dog() an rvalue, it returns an address right?

    • @chrisevers2084
      @chrisevers2084 8 лет назад

      Thanks Peterolen

    • @roykimor
      @roykimor 8 лет назад +1

      isn't it called xvalue ?

    • @pihsgib
      @pihsgib 6 лет назад +4

      yes, dog() create a temporary object, but this temporary object doesn't have a IDENTIFIABLE memory address, because it doesn't have a name attach to it, right ? So you can't identify it's address, then it is a rvalue

  • @huyvole9724
    @huyvole9724 6 лет назад

    lvalue ^^
    int& back(int& i) { return i; }
    int i = 5;
    bacK(i) = 6;
    printf(i); // will show "6"