Hello again!
In this video, we are going to look at value categories.
These are something which came in in C++ 17, although they actually describe the situation which goes
back to C++11.
So it is worth spending a few minutes on these, even if you are not ready to update to C++17 just yet.
The characteristics of lvalues.
They represent objects which are persistent, so they survive for more than one statement.
They occupy memory which is accessible to the programmer.
They are either on the program stack or in the heap, and they remain valid until they go out of scope
or are deleted.
So this could be a local variable in a scope; global or static variables; and function arguments.
rvalues, on the other hand, are stored in locations which are not accessible to the programmer,
such as processor registers.
So we cannot take their address.
And there are two different types of rvalue.
There are literals like the number 2 or the letter 'c', and there are temporary objects, which are created
and destroyed in the same statement.
C++17 has three so-called "value categories", to describe these possibilities.
The literals are called "pure" rvalues or "prvalues".
These have no name, and they cannot be referred to again.
Temporary objects are called "xvalues".
These represent an object of some class, and their data can be moved.
The original proposal was that the 'x' stands for "expert" or "exceptional", which are both pretty terrible.
Then someone came up with "expiring", and that is much better.
Because these objects will expire at the end of the statement in which they are created.
And then finally, we have good old-fashioned lvalues.
When we learn C++, we are told that every expression has a type.
Now, we have to say that every expression has a type and a value category.
So let's look at a diagram. Values can be either xvalues which are temporary objects, or prvalues
which are literals.
And then we have lvalues, on their own.
In fact, that is not quite the end of the story.
lvalues and xvalues do have some things in common.
They are objects, and they can have a dynamic type which is different from their static type.
So sometimes, it is useful to talk about lvalues and xvalues together.
And there is a term for this, which is "generalized lvalue", or glvalue for short.
So the final version of our diagram, which looks like a plate of fried eggs! We have rvalues which
can be prvalues or xvalues, and glvalues, which can be lvalues or xvalues.
So, an rvalue can be a literal or a temporary
object, and a generalized lvalue can be either a "proper" lvalue or a temporary object.
Okay.
So that is it for this video.
I will see you next time.
Until then, keep coding!