WEBVTT

00:00.120 --> 00:00.720
Hello again!

00:01.020 --> 00:04.140
In this video, we are going to have a practical on perfect forwarding.

00:04.530 --> 00:07.500
We are going to write a slightly more realistic example.

00:09.450 --> 00:11.220
We are going to use this class.

00:11.220 --> 00:13.920
It has one member, which is a library string.

00:14.730 --> 00:20.790
It has two overloaded constructors which take a string, one for an lvalue and one for an rvalue.

00:21.690 --> 00:25.290
We're going to make a function which will create objects of this class.

00:25.710 --> 00:33.390
So this is a very simple version of make_pair(), if you like, or perhaps it should be make_single! In

00:33.390 --> 00:34.710
this make_test() function,

00:34.710 --> 00:39.180
we want to make sure that the move constructor is called whenever possible.

00:39.660 --> 00:42.600
So we are going to use perfect forwarding to achieve that.

00:43.920 --> 00:49.260
Our make_test() function is going to have a forwarding reference argument, and then it is going to call

00:49.260 --> 00:54.930
the constructor for the class. And it will use the library forward() function to make sure that the appropriate

00:55.500 --> 01:00.750
constructor is called for the argument type. And then we return this object by value.

01:01.260 --> 01:06.630
It may look as though we are introducing an extra copy operation here, but modern compilers should be

01:06.630 --> 01:07.890
able to optimize this away.

01:08.190 --> 01:11.700
And in fact, in C++ 17, they are required to elide

01:12.000 --> 01:13.380
that copy constructor call.

01:14.100 --> 01:15.990
So this is going to be very efficient, we hope.

01:18.750 --> 01:19.870
Let's look at the code.

01:19.890 --> 01:25.230
We have our class here with the string member, and the two overloaded constructors.

01:27.090 --> 01:31.050
Then we have our make_test() function which takes a forwarding reference.

01:32.010 --> 01:39.570
It calls the constructor for the class, and it uses the library forward() function to call the lvalue

01:39.570 --> 01:44.700
or the rvalue constructor as appropriate, and then we return it by value.

01:45.900 --> 01:51.950
In the main() function we call this make_test() function with an lvalue argument, and then with an

01:51.960 --> 01:52.830
rvalue argument.

01:53.730 --> 01:55.440
So let's see if this works.

01:58.320 --> 02:04.260
So, calling with an lvalue argument calls the constructor which takes lvalue, and calling with an rvalue

02:04.260 --> 02:06.450
argument calls the move constructor.

02:07.050 --> 02:07.830
So that works.

02:10.750 --> 02:16.000
I've also put in these functions similar to the ones we had before, which will tell us what type

02:16.000 --> 02:17.860
the arguments to make_test() is.

02:19.000 --> 02:20.080
So let's call those.

02:22.070 --> 02:25.250
So with the lvalue arguments, we get the modifiable version.

02:25.260 --> 02:29.690
So that is an lvalue. And with the rvalue argument, we get the move version.

02:30.050 --> 02:32.240
So x is actually an rvalue in here.

02:34.810 --> 02:36.580
So this may seem a bit pointless.

02:36.580 --> 02:41.260
Why not just call the constructors directly? And with this class you can do that.

02:41.530 --> 02:42.730
It just has one member.

02:43.420 --> 02:47.620
So there's only two possible constructors. With the pair class,

02:48.190 --> 02:53.530
there is two members, which means there is four combinations of constructors. And also the members

02:53.530 --> 02:54.940
are template types.

02:55.480 --> 02:58.900
So you'd be using templates anyway, and this approach would make more sense.

03:00.860 --> 03:05.750
C++ now has a few more functions which will make objects of library classes.

03:06.180 --> 03:09.570
We are going to meet a couple of them in the next section, with "smart" pointers.

03:10.100 --> 03:11.570
So that is why I'm going into this now.

03:12.380 --> 03:13.900
Okay, so that is it for this feature.

03:14.330 --> 03:15.170
I will see you next time.

03:15.380 --> 03:17.480
Until then, keep coding!
