lvalues and rvalues in C++

The Cherno · 405K views · 2020 · 14:13

Open on YouTube

C, C++, Rust, Go & systems languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about && and move semantics, reference return types, and 'not a modifiable lvalue' errors that come down to value categories.

Recommended by 3 people across 3 different months, 2020 to 2022.

What it is

A roughly 14-minute C++ tutorial from The Cherno that explains what lvalues and rvalues are, how they differ, and how they show up in assignments, function arguments and references. It is part of his C++ series and uses short code examples.

What people said

Recommends Quadra Project · 2020-05 · Stack Overflow · on “What does & after a return type mean in C++ OOP”

Your Are Returning a Refrence To An Object just like next to any variable , Now that is good in some cases, but in others you might be returning a refrence to temporary variables and then by any change these temporary objects get freed from memory then you are referencing a freed block of memory The Cherno explains it very well in his rvalue and lvalues video you can go and see it here: [this video]

Recommends Jonh Smith Benjamin · 2021-11 · Stack Overflow · on “C++ Double Address Operator? (&&)”

I believe && is for move semantics . It allows something to give up its memory to something else that needs it, avoiding the need for copying. Move constructor E.g. String(String&& other) noexcept { //... } 20/02/24: I felt that the && question was relevant to move constructors and is missing explainations on lvalues, rvalues, move semantics and std::move. I've included a decent video from the Cherno that explains the && better than StackOverflow comments. [this video]

Recommends tedtanner · 2022-02 · Stack Overflow · on “How do I dynamically allocate memory to a struct with data?”

Here is a link to a resource [this video] to learn a little more about lvalues and rvalues. If you dereference a `char**`, you get a `char*`, which can be assigned so it is an lvalue. Dereferencing a `char*`, however, gives a `char` literal, which is an rvalue. Hence the compiler was complaining about the `char*` not being a modifiable lvalue.