WEBVTT

00:00.060 --> 00:04.740
Hello again! In this video, we are going to look at tuples in C++17.

00:06.480 --> 00:09.610
The first two features are ones that we have seen before,

00:09.630 --> 00:10.170
in fact.

00:11.600 --> 00:13.970
Constructor template argument deduction.

00:14.420 --> 00:20.030
If we have a constructor which is templated, we no longer need to provide the type parameters.

00:20.540 --> 00:24.170
If we have an initializer, then the compiler will work them out for us.

00:25.280 --> 00:31.430
So, for example, we can just create a tuple, from an initializer, and the compiler will deduce that

00:31.430 --> 00:34.070
the types of the elements are int, double and string.

00:34.490 --> 00:37.460
We no longer have to write them out explicitly.

00:39.930 --> 00:40.800
Structured bindings.

00:40.800 --> 00:43.860
We have used these to unpack the members of a pair.

00:44.370 --> 00:50.640
When we were working with map elements, for example. And we can also use structured bindings with tuples.

00:51.930 --> 00:57.720
If we have some function which returns a tuple, then we can just use the structured binding directly

00:58.200 --> 01:03.510
and this will create and initialize these variables, from the elements of the tuple.

01:03.900 --> 01:08.940
We no longer need to declare the variables and call the tie() function.

01:11.320 --> 01:12.360
So let's try this out.

01:12.400 --> 01:15.880
We have our tuple initialization here.

01:16.720 --> 01:18.760
We do not need to do that anymore.

01:19.240 --> 01:25.840
We return this tuple from the function. And then, in the caller, we use a structured binding to unpack

01:25.840 --> 01:28.330
the elements of the tuple, and we print them out.

01:30.580 --> 01:31.150
And there we are.

01:36.190 --> 01:40.030
One of the new functions in C++ is called apply().

01:40.390 --> 01:43.750
This is for using a more functional style of programming.

01:44.830 --> 01:51.730
It takes a callable object as its first argument, and then it will call this function and pass the

01:51.730 --> 01:54.610
rest of the arguments, as arguments to this function.

01:55.270 --> 02:01.060
So if we have some function pointer as the first argument, then this will call the function pointer,

02:01.780 --> 02:03.910
and the rest of the arguments will be forwarded

02:04.030 --> 02:05.710
as arguments to that function call.

02:06.880 --> 02:11.260
Obviously, having to manage several different argument objects could be a bit annoying.

02:11.620 --> 02:17.590
So you can also use a tuple. So you can pack all these objects up into a tuple. And then, when you call

02:17.590 --> 02:23.650
apply(), apply() will unpack the tuple and forward the elements to the function call.

02:26.200 --> 02:28.060
So here is a very simple example.

02:28.420 --> 02:33.670
We have a function which prints out its arguments. And then we have this as the first argument to

02:33.670 --> 02:34.090
apply().

02:34.120 --> 02:36.370
So that is a function pointer to this function.

02:37.180 --> 02:43.360
And then we have a tuple. And this will call the function, with those tuple elements as the arguments

02:43.360 --> 02:44.410
to the function call.

02:47.020 --> 02:47.650
And there we are.

02:49.270 --> 02:54.760
And finally, it is also possible to pass a tuple to constructor arguments.

02:55.330 --> 02:58.810
To do that, we need to call the function make_from_tuple().

02:59.530 --> 03:01.000
This is a template function.

03:01.360 --> 03:03.130
The parameter is the class.

03:03.520 --> 03:06.130
So that is going to be the class whose constructor will be called.

03:06.730 --> 03:11.890
And then the argument is the tuple containing the arguments for the constructor call.

03:14.830 --> 03:17.350
So here is a class, or rather struct.

03:17.950 --> 03:20.800
It has a constructor, which takes int, double and string.

03:21.940 --> 03:23.770
Then we call make_from_tuple().

03:24.490 --> 03:26.410
The parameter is this class.

03:26.830 --> 03:32.080
So this is going to call the class constructor. And it is going to pass the elements of this tuple as

03:32.080 --> 03:32.770
the arguments

03:33.100 --> 03:34.240
to that constructor call.

03:34.780 --> 03:37.990
So this is going to forward the tuple elements into the constructor.

03:40.760 --> 03:46.220
And then we just have a function which will print out the values of the members. So we can see if they have

03:46.220 --> 03:47.480
been correctly initialized.

03:48.770 --> 03:52.730
Visual Studio will not actually compile this code. Or, at least, not the version I have.

03:53.330 --> 04:02.450
So I am going to quickly switch over to MinGW. And, remember, we need to use the C++17 option for all

04:02.450 --> 04:03.230
these examples.

04:06.650 --> 04:09.920
So "i" is one, "d" is two and "s" is "three".

04:10.970 --> 04:16.730
So those are the values we would expect. So that has worked. That has correctly initialized the object

04:16.730 --> 04:19.550
from the tuple. OK, so that is it for this video.

04:19.940 --> 04:20.780
I will see you next time.

04:20.780 --> 04:22.820
But until then, keep coding!
