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

  Рет қаралды 12,866

Mike Shah

Mike Shah

2 жыл бұрын

►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Lesson Description: In this lesson I am going to talk about something that is not so much a 'language feature' but rather something that is part of the semantics of modern C++. At the least, you should leave this lesson understanding what an 'lvalue' and 'rvalue' error means from your compiler. We'll also talk a bit about lvalue references (which you already know as int&), and then briefly introduce rvalue references. rvalue references will be further motivated, and we'll start to look at why they may be useful for performance in a later video.
►KZfaq Channel: / mikeshah
►Please like and subscribe to help the channel!

Пікірлер: 57
@Yuvaraj-pd6ng
@Yuvaraj-pd6ng 4 ай бұрын
this is the best c++ series this video was amazing
@MikeShah
@MikeShah 4 ай бұрын
Cheers, thank you for the kind words!
@mohsenzare2511
@mohsenzare2511 3 ай бұрын
I saw this video a while ago, and know I come back to watch that again! thank for your great video!
@MikeShah
@MikeShah 3 ай бұрын
Cheers! You are most welcome!
@dwolrdcojp
@dwolrdcojp 2 жыл бұрын
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 :)
@Quancept
@Quancept 9 ай бұрын
damn! just like that I understood l-values and r-values. I don't know how to thank you enough Mike :)
@MikeShah
@MikeShah 9 ай бұрын
Cheers, happy to help!
@k0185123
@k0185123 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
@@MikeShah Got it! Thank you for the reminder :)
@muhammedusman7269
@muhammedusman7269 5 ай бұрын
all videos are great and excellent. Thanks
@MikeShah
@MikeShah 5 ай бұрын
Cheers!
@mytech6779
@mytech6779 8 ай бұрын
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 8 ай бұрын
Cheers, glad to hear this helped!
@robertstrickland9722
@robertstrickland9722 2 жыл бұрын
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!
@OzanCetinaslan
@OzanCetinaslan Ай бұрын
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 Ай бұрын
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
@muhammadharris4470
@muhammadharris4470 Жыл бұрын
Thanks mike for teaching this super important topic for extreme simplicity. really love your teaching style 👍
@MikeShah
@MikeShah Жыл бұрын
Cheers thank you for the kind words!
@piotrlezanski5156
@piotrlezanski5156 5 ай бұрын
You are great! Thanks
@MikeShah
@MikeShah 5 ай бұрын
Thank you for the kind words 🙏
@dhanushs1802
@dhanushs1802 2 жыл бұрын
Very well explained as always. Thank you.
@MikeShah
@MikeShah 2 жыл бұрын
Cheers thank you for the kind words!
@kamilziemian995
@kamilziemian995 5 ай бұрын
Very nice video.
@MikeShah
@MikeShah 5 ай бұрын
Cheers!
@user-ql7pw7ld1n
@user-ql7pw7ld1n 6 ай бұрын
Thank you so much I enjoyed a lot
@MikeShah
@MikeShah 6 ай бұрын
Cheers!
@VoidloniXaarii
@VoidloniXaarii Жыл бұрын
Thanks so much
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@arjunkandaswamy1532
@arjunkandaswamy1532 Жыл бұрын
&& was great
@TheIntervurt
@TheIntervurt 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 жыл бұрын
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. :)
@joymakerRC
@joymakerRC 9 ай бұрын
needed this one bro
@joymakerRC
@joymakerRC 9 ай бұрын
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 9 ай бұрын
Cheers
@muhammedhasankayapnar9495
@muhammedhasankayapnar9495 Жыл бұрын
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?
@user-yk2dc6yr7q
@user-yk2dc6yr7q 6 ай бұрын
24:25
@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 жыл бұрын
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 .
@IsaacC20
@IsaacC20 8 күн бұрын
@28:44 How do you use declspec to check value category of expressions?
@MikeShah
@MikeShah 7 күн бұрын
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
@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 Жыл бұрын
I get the error: error C2440: 'initializing': cannot convert from 'std::basic_string' to 'std::string &&'
@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 kzfaq.info/get/bejne/prNze7qF3K-7nqc.html
@MikeShah
@MikeShah Жыл бұрын
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.). :)
Introduction to std::move in C++ | Modern Cpp Series Ep. 32
10:03
НЫСАНА КОНЦЕРТ 2024
2:26:34
Нысана театры
Рет қаралды 1,4 МЛН
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 17 МЛН
Inside Out 2: Who is the strongest? Joy vs Envy vs Anger #shorts #animation
00:22
lvalues and rvalues in C++
14:13
The Cherno
Рет қаралды 308 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 101 М.
C++  Tutorial: Rule of 5  [move constructor, move assignment operator]
29:46
Professor Hank Stalica
Рет қаралды 16 М.
Introduction to constexpr | Modern Cpp Series Ep. 86
10:56
Mike Shah
Рет қаралды 11 М.
Новые iPhone 16 и 16 Pro Max
0:42
Romancev768
Рет қаралды 2,3 МЛН
$1 vs $100,000 Slow Motion Camera!
0:44
Hafu Go
Рет қаралды 28 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 63 МЛН