Hello again! In this video, we are going to look at tuples in C++17.
The first two features are ones that we have seen before,
in fact.
Constructor template argument deduction.
If we have a constructor which is templated, we no longer need to provide the type parameters.
If we have an initializer, then the compiler will work them out for us.
So, for example, we can just create a tuple, from an initializer, and the compiler will deduce that
the types of the elements are int, double and string.
We no longer have to write them out explicitly.
Structured bindings.
We have used these to unpack the members of a pair.
When we were working with map elements, for example. And we can also use structured bindings with tuples.
If we have some function which returns a tuple, then we can just use the structured binding directly
and this will create and initialize these variables, from the elements of the tuple.
We no longer need to declare the variables and call the tie() function.
So let's try this out.
We have our tuple initialization here.
We do not need to do that anymore.
We return this tuple from the function. And then, in the caller, we use a structured binding to unpack
the elements of the tuple, and we print them out.
And there we are.
One of the new functions in C++ is called apply().
This is for using a more functional style of programming.
It takes a callable object as its first argument, and then it will call this function and pass the
rest of the arguments, as arguments to this function.
So if we have some function pointer as the first argument, then this will call the function pointer,
and the rest of the arguments will be forwarded
as arguments to that function call.
Obviously, having to manage several different argument objects could be a bit annoying.
So you can also use a tuple. So you can pack all these objects up into a tuple. And then, when you call
apply(), apply() will unpack the tuple and forward the elements to the function call.
So here is a very simple example.
We have a function which prints out its arguments. And then we have this as the first argument to
apply().
So that is a function pointer to this function.
And then we have a tuple. And this will call the function, with those tuple elements as the arguments
to the function call.
And there we are.
And finally, it is also possible to pass a tuple to constructor arguments.
To do that, we need to call the function make_from_tuple().
This is a template function.
The parameter is the class.
So that is going to be the class whose constructor will be called.
And then the argument is the tuple containing the arguments for the constructor call.
So here is a class, or rather struct.
It has a constructor, which takes int, double and string.
Then we call make_from_tuple().
The parameter is this class.
So this is going to call the class constructor. And it is going to pass the elements of this tuple as
the arguments
to that constructor call.
So this is going to forward the tuple elements into the constructor.
And then we just have a function which will print out the values of the members. So we can see if they have
been correctly initialized.
Visual Studio will not actually compile this code. Or, at least, not the version I have.
So I am going to quickly switch over to MinGW. And, remember, we need to use the C++17 option for all
these examples.
So "i" is one, "d" is two and "s" is "three".
So those are the values we would expect. So that has worked. That has correctly initialized the object
from the tuple. OK, so that is it for this video.
I will see you next time.
But until then, keep coding!