lvalues and rvalues in C++
Вставка
- Опубліковано 16 тра 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.
*adds another ampersand*
H O L Y C R A P
These videos make me feel like an absolute idiot in C++
Lmao he loved this, SAVAGE xD
It also blew my mind when he assigned a value to the result of the function... seems pretty useless though.
@@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[]
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
condescend me daddy!
The C++ series is definitely my favorite.
This is the MOST clear explanation of r- and l- values I've ever heard or read
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.
@@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.
Everybody: blown mind
Cherno: bored so he has to fiddle with paprika
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 :)
congratulations 🥰🥰
@@richardlyman2961 yes, you asked on April 27th 2019 at around 12:37 UTC. Why?
*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
For me lvalues == "Location", rvalues == "tempoRary".
@@ikemkrueger what about a global on the right side.
@@ikemkrueger That was the first thing I was thinking when I heard about it aswel !
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.
rvalues of class type DO have and address and CAN appear on the left hand side of assignment: std::string("hello") = "bye";
@@fredoverflow ever heard of copy constructor!?
@@fredoverflow or operator overloading for that matter?
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++.
@@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.
The core explanation starts at 3:55.
Just wanted to write the same, so much of "the thing I'm going to talk about", so much time wasted.
just wanted to type the same
Thnx😂
THANK YOU
@@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
I've been waiting for this! Thank You! :)
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
Mandarin is harder than C++, haha
@@AdiPrimandaGinting true that..
@@AdiPrimandaGinting Nah mother language is always the easiest. Also chinese is really easy but they just have a nightmare writing system.
很多中文翻譯的專有名詞,看得很一頭霧水。還不如直接去看原文的code說明教學,比較快理解
Woah, what a coincidence, I was just looking for a cohesive explanation of these yesterday! Great video, keep it up!
Cherno, thank you so much! Your videos are awesome, and I can't wait for move semantics.
Your videos are amazing ! Thank you for taking the time and effort to share your knowledge!
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.
Just checking in to see whether you've made a career switch haha
You are a doctor AND a lawyer?! AND now you are also learning programming?! Your parents must be damn proud of you.
Thank you for explaining this concept so well! Looking forward to your video on move semantics!
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 😉 )
Great explanation as always! Thanks for making these videos!
Thanks Cherno, top quality content as always
Really enjoy your work @The Cherno
Explained beautifully! appreciate the clarity it provides
Thanks for sharing wonderful stuff Yan
Every tutorial you make is so informative and so well made. Thank you for making those videos free for all people.
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!
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
Thank you for clarifying this topic. Well done.
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.
Thank you Cherno for all this Amazing content. I've learned so much with your C++ series 👍😎❤️.
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.
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.
@@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
@@oracleoftroy well...
how about this:
#include
void f(const int& i) {
const int* p = &i;
std::cout
@@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.
@@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.
This video answered so many questions that had been shelved in the back of my mind. Thank you.
Awesome video Cherno! Been coding with modern C++ for a few months now, but can FINALLY say I understand rvalues and lvalues! :D
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.
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
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!
Clear to-the-point explanation. Your C++ series saves time googling a specific topic. Keep up the good work.
Thank you so much for your C++ videos! Love them!
Great explanation Mr. Черников
Rly like your way of explaining things
Thank you very much for the info and knowledge you provide : )
This was the clearest and most concise explanation of this topic that I have seen. Cheers!
I love your demonstrations, it clarifies everything
Really appreciate the good work you're doing!
Great Explanation. Thanks for making it clear & stronger understanding
Always clear, always good. Thanks
Thanks for the video
We are really looking forward to see your videos on design patterns 😇
thanks cherno, i’ve always found this topic to be confusing, but you thankfully made it clearer
Thank you so much! It was a really clean explanation and got all my questions answered :)
Extremely well explained, calling lvalues 'location values' entirely cleared up the cloudiness about this subject for me! Amazing job!
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!
I was so daunted by this going into it but your explanation was so good, thanks!
Thank you. Expecting to see a video on move semantics soon
Can't find the right words of praise for you. My deepest gratitude and appreciation!
Man, the video was great, it made lots of stuff more clear for me. Thank you for it!
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!
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.
Definitely one of the best detailed explanation on l and r values in c++
Thanks!
I watched the ad of this videoon purpose bc you really deserve it :) thanks a lot for making this clear in my head!
This was a very clear explanation, thank you. I'm about to watch the move semantics video.
Way more clear than the articles I've tried to read.. thanks a lot!!
Thank you it has been SUPER helpful!
Genius at work. Thanks a lot!!!
You are seriously the only legit free video series for modern C++
Thanks a lot for partially sharing your knowledge for free!
Really insightful, thanks!
As always, very useful!
Thanks a lot!
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.
This has plagued me for long, and I've been programming in C++ (03) for years. Thank you!
Thanks for the super clear explanation!
loving your C++ videos
hope to see more of it
Thanks a lot Yan. This C++ series is really the best on UA-cam :)
Helps a lot!!. This is very clear explanation, especially the provided sample code example.
Thanks so much for your work!
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.
THE BEST.
Amazing explanation.
Thanks for your helpful explanation!
Very nice explanation about this topic. Really cleared it up.
Wow this was very helpful & straight to the point ! thank you !
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!
I love to listen when you talk about c++ don't stop pleas. This is gold.
Crisp and clear. Thanks Cherno
never heard this been explained so clear
I had forgotten about this topic, but you made it clear and quick.
I am a big fan Just love the videos I love that you give a reason for everything
Quick and direct to the point, really great.
The explanation can't be simpler than what Cherno mentioned. I am totally impressed.
I really hope you won't stop there as this whole topic seems to be quite hard to understand and you make such things appear really simple.
@The Cherno, so then if lets say void setValue(const int& value) accepts both lvalue and rvalue, how come simply saying void setValue(int value) accepts both too?
@@battosaijenkins946 by passing by value you make another copy of variable which is lvalue type.
The difference is present only when you are using references in order to make your code more effective by avoiding unnecessary copying especially when it comes to large objects.
@@battosaijenkins946 The parameter (the variable "value" inside setValue()) is initialized from the argument expression. In case of void setValue(int value) that means it will construct a new int, regardless of what type of expression the argument is. If you use some class instead of int then you'll see a constructor of that class will get invoked (which one will depend on what type of expression the argument is). In case of void setValue(int const &value) the caller needs to provide an object (of type int) to which the parameter will bind. This means that if your expression is an "lvalue" (technically a glvalue, any expression that designates an existing object), then no new object will be constructed and "value" will refer to that existing object. If your expression is an "rvalue" (technically a prvalue) then as he explained in the video, a new temporary object will be constructed from this expression and the parameter will bind to that.
@@alextiga8166 So if I do setValue(const int& value) then im not copying at all as oppposed to setValue(int value), is that correct?
@@battosaijenkins946 yes, the "value" variable inside the function will be a reference to the variable you passed in this case.
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.
Thank you, I'll need to revisit this but it does feel clearer!
Great explanation. You are a good mentor. Thanks a lot.
Thank you so much, i've only started learning C++ recently and this is just what i need
Finally! Thank you for the video.
very good explanation man! Good work.
Dude you really have a gift for explaining stuff. C++ and opengl programming has become very easy for me thank you
This C++ series is excellent. Thank you TheCherno!
Great explanation!! Thanks.
- int& a; //lvalue reference
- const int& a; //support both lvalue and rvalue(by temporary storage) reference
- int&& a; //rvalue reference
const reference in function for "temporary" rvalue, at 10:05. void Print(const std::string& name)
Awesome explanation as always!
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!
you nailed it man, please make more videos like this.
Extremely useful and well explaned, thank you!
Informative and easy explanation ) Thx!
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.
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 ^^
I went from 0 to intermediate in C++ thanks to your C++ series. Keep it up! Thank you.
Best sum up about glvalues and prvalues I saw, nicely described what is going on and what consequences and benefits it brings. ♥