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.
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 :)
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!
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 :)
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.
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.
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
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++
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. :)
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
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 :) ?
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.
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
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 :)
@@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.
@@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!
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?
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++.
@@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"
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.). :)
this is the best c++ series this video was amazing
Cheers, thank you for the kind words!
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.
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 :)
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!
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 :)
@@MikeShah Got it! Thank you for the reminder :)
I appreciate the pop quizzes. Really improves the value of the lesson!
Thank you for the kind words! On lessons like this I like to throw those in to test folks understanding :)
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.
Cheers, glad to hear this helped!
hobbyist*
@@stupidteous male-donkey*
all videos are great and excellent. Thanks
Cheers!
You are great! Thanks
Thank you for the kind words 🙏
I really enjoyed this explanation, even more than reading about it in the C++ Crash Course by No Starch Press. Good job.
Thank you for the kind words!
I saw this video a while ago, and know I come back to watch that again! thank for your great video!
Cheers! You are most welcome!
damn! just like that I understood l-values and r-values. I don't know how to thank you enough Mike :)
Cheers, happy to help!
Thanks mike for teaching this super important topic for extreme simplicity. really love your teaching style 👍
Cheers thank you for the kind words!
42. There is something to that number. Perhaps the piscine?
From Douglas Adam's book -- Hitchhiker's Guide to the Galaxy :)
Il a pas la ref dans la ref 😅
👍Thanks again!
Very well explained as always. Thank you.
Cheers thank you for the kind words!
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.
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
Thank you so much I enjoyed a lot
Cheers!
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++
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. :)
Very nice video.
Cheers!
@28:44 How do you use declspec to check value category of expressions?
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
&& was great
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 :) ?
Thank you for the kind words. Can you provide a timestamp for the example?
24:25
needed this one bro
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.
Cheers
Thanks so much
Cheers!
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
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 :)
@@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.
@@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!
@@MikeShah Thank you .
I get the error: error C2440: 'initializing': cannot convert from 'std::basic_string' to 'std::string &&'
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?
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++.
@@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"
Concerning the std::string&& s3 = s1+s2, it doesn't work in C++20. Is it deprecated in the modern language?
Not sure--what message are you getting with what compiler?
What does the name Shah mean? Does it mean king in Parsi?
Depends on the language, but yes, King is one meaning 🙂
It finally started to click for me at this place ruclips.net/video/pMCHWTwIWns/видео.html
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.). :)