WEBVTT

00:00.090 --> 00:00.660
Hello again!

00:01.110 --> 00:03.720
In this video, we are going to look at value categories.

00:04.470 --> 00:10.770
These are something which came in in C++ 17, although they actually describe the situation which goes

00:10.770 --> 00:12.330
back to C++11.

00:13.290 --> 00:19.290
So it is worth spending a few minutes on these, even if you are not ready to update to C++17 just yet.

00:21.580 --> 00:23.440
The characteristics of lvalues.

00:23.800 --> 00:28.840
They represent objects which are persistent, so they survive for more than one statement.

00:29.650 --> 00:32.650
They occupy memory which is accessible to the programmer.

00:33.190 --> 00:39.280
They are either on the program stack or in the heap, and they remain valid until they go out of scope

00:39.610 --> 00:40.570
or are deleted.

00:41.380 --> 00:47.320
So this could be a local variable in a scope; global or static variables; and function arguments.

00:49.690 --> 00:53.920
rvalues, on the other hand, are stored in locations which are not accessible to the programmer,

00:54.310 --> 00:55.900
such as processor registers.

00:56.380 --> 00:57.880
So we cannot take their address.

00:58.150 --> 01:00.370
And there are two different types of rvalue.

01:00.880 --> 01:07.180
There are literals like the number 2 or the letter 'c', and there are temporary objects, which are created

01:07.180 --> 01:09.460
and destroyed in the same statement.

01:12.210 --> 01:17.340
C++17 has three so-called "value categories", to describe these possibilities.

01:18.210 --> 01:22.050
The literals are called "pure" rvalues or "prvalues".

01:22.680 --> 01:26.040
These have no name, and they cannot be referred to again.

01:29.490 --> 01:31.600
Temporary objects are called "xvalues".

01:32.070 --> 01:36.270
These represent an object of some class, and their data can be moved.

01:37.080 --> 01:43.350
The original proposal was that the 'x' stands for "expert" or "exceptional", which are both pretty terrible.

01:44.040 --> 01:46.710
Then someone came up with "expiring", and that is much better.

01:47.160 --> 01:51.510
Because these objects will expire at the end of the statement in which they are created.

01:53.250 --> 01:55.560
And then finally, we have good old-fashioned lvalues.

01:57.030 --> 02:00.210
When we learn C++, we are told that every expression has a type.

02:00.780 --> 02:04.380
Now, we have to say that every expression has a type and a value category.

02:07.840 --> 02:15.400
So let's look at a diagram. Values can be either xvalues which are temporary objects, or prvalues

02:15.400 --> 02:16.390
which are literals.

02:17.020 --> 02:18.640
And then we have lvalues, on their own.

02:21.960 --> 02:23.820
In fact, that is not quite the end of the story.

02:24.150 --> 02:26.970
lvalues and xvalues do have some things in common.

02:27.360 --> 02:32.760
They are objects, and they can have a dynamic type which is different from their static type.

02:33.270 --> 02:37.530
So sometimes, it is useful to talk about lvalues and xvalues together.

02:38.220 --> 02:43.320
And there is a term for this, which is "generalized lvalue", or glvalue for short.

02:44.370 --> 02:49.920
So the final version of our diagram, which looks like a plate of fried eggs! We have rvalues which

02:49.920 --> 02:56.130
can be prvalues or xvalues, and glvalues, which can be lvalues or xvalues.

02:57.060 --> 02:59.340
So, an rvalue can be a literal or a temporary

02:59.340 --> 03:05.670
object, and a generalized lvalue can be either a "proper" lvalue or a temporary object.

03:06.900 --> 03:07.230
Okay.

03:07.230 --> 03:08.340
So that is it for this video.

03:08.820 --> 03:09.660
I will see you next time.

03:09.900 --> 03:11.810
Until then, keep coding!
