WEBVTT

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

00:00.990 --> 00:06.680
In this video, we are going to look at library defined operators. In C++11,

00:06.690 --> 00:10.320
the library defines some generic operator classes.

00:10.950 --> 00:18.540
These are in the <functional> header. And they cover arithmetic operations, relational, logical and bitwise

00:18.540 --> 00:25.470
operations. And it is fairly obvious what they all do, although the naming system is not very consistent.

00:25.980 --> 00:34.560
And also the "not" operator was forgotten. and had to be added in C++14. These will just call the corresponding

00:34.830 --> 00:35.430
operator.

00:35.700 --> 00:41.400
So, for example, "less" will call the less-than operator of the corresponding type.

00:43.440 --> 00:45.420
These are functor classes.

00:45.810 --> 00:48.630
So we need to create an object before we can use them.

00:49.530 --> 00:52.620
And we also need to call the function call operator.

00:54.100 --> 01:00.700
So for example, we can call the constructor to get an object of the "less" operator, and then we have

01:00.700 --> 01:02.350
the function call with some arguments.

01:03.070 --> 01:09.250
And that would be equivalent to calling the less-than operator, with arguments "t1" and "t2".

01:12.440 --> 01:19.010
We can use these operators for writing generic functions which perform default operations.

01:19.790 --> 01:25.070
And these are similar to the way that some of the functions in the algorithm library are implemented.

01:25.490 --> 01:31.100
For example, the sort() function. We can call this with an iterator range, and it will use the less

01:31.100 --> 01:33.290
than operator, to order the elements.

01:33.980 --> 01:40.010
Or, we can provide our own optional predicate, and the sort() algorithm will use that predicate instead

01:40.010 --> 01:42.350
of less-than, for ordering the elements.

01:44.750 --> 01:51.050
So we start by writing our function as a template function, which takes a callable object as one of

01:51.050 --> 01:51.770
the arguments.

01:53.420 --> 02:00.890
So we need two type parameters. One for the things which are being compared, "t1" and "t2", and one for

02:00.890 --> 02:02.180
the callable object.

02:02.870 --> 02:10.400
And then, in the function body, we call the callable object, with these "t1" and "t2" as the arguments.

02:10.970 --> 02:13.700
So that is going to use this argument for doing the comparison.

02:16.510 --> 02:20.350
And then, next, we make this callable object a default argument.

02:21.040 --> 02:27.010
So we turn this template function into a template function with a default parameter. Like the ones that

02:27.010 --> 02:28.420
we looked at in the last video.

02:29.470 --> 02:34.420
So now this function type parameter is going to be the type of the "less" operator.

02:35.350 --> 02:40.720
And then our third argument to the function is going to be an optional argument. A default argument.

02:41.530 --> 02:44.590
And, by default, this will be an object of this type.

02:45.100 --> 02:47.620
So this is going to be the library "less" operator.

02:49.360 --> 02:55.930
And then, by default, our function is going to call the library "less" operator, with "t1" and

02:55.930 --> 02:56.710
"t2" as arguments.

02:59.880 --> 03:00.930
So how does this work?

03:01.320 --> 03:07.050
If we provide a callable object as the third argument, then the compiler will instantiate this.

03:07.410 --> 03:12.390
It is not going to use the default, because we provided an explicit callable object.

03:12.960 --> 03:15.330
So it is going to use this callable object.

03:15.750 --> 03:19.950
So it is going to call the lambda expression, with "t1" and "t2" as arguments.

03:21.150 --> 03:26.670
On the other hand, if we do not have a callable object, then the compiler will instantiate this using

03:26.670 --> 03:27.450
the default.

03:27.900 --> 03:30.210
So this will use the library "less" operator.

03:30.990 --> 03:34.740
So this is going to call less-than, with "t1" and "t2" as arguments.

03:36.810 --> 03:41.010
So here is our template function with the default parameter type.

03:41.340 --> 03:47.820
The default is the type of the library "less" operator. Then the function has a default argument, which

03:47.820 --> 03:49.620
will be an object of this type.

03:49.950 --> 03:52.080
So this will be the library "less" object.

03:52.590 --> 03:57.780
And then by default, our function will call less-than, with "t1" and "t2" as arguments.

04:00.240 --> 04:06.030
If we pass a lambda expression as the third argument, the compiler will instantiate this function.

04:06.420 --> 04:10.050
It will use the lambda expression as this callable object.

04:10.380 --> 04:14.130
So it is going to call the lambda expression, with "x" and "y" as arguments.

04:15.270 --> 04:20.670
If we do not provide the callable object, then the compiler will instantiate this using the less-than

04:20.670 --> 04:21.210
operator.

04:21.690 --> 04:24.990
So this will call less-than, with "x" and "y" as arguments.

04:27.850 --> 04:31.270
And we get true, in both cases, because 1 is less than 2.

04:34.020 --> 04:41.010
If I change the expression, so it now uses greater-than. So the compiler will still instantiate it using the

04:41.010 --> 04:41.760
lambda expression.

04:41.970 --> 04:45.150
But we should now get false, because 1 is not greater than 2.

04:47.620 --> 04:49.570
And yes, we do get false.

04:50.770 --> 04:52.390
So if I just put that back...

04:53.620 --> 05:00.760
If I want to change this comparison, then I need to change the function. So I need to change this type

05:01.000 --> 05:01.450
parameter.

05:01.450 --> 05:05.080
So it is now the "greater" library operator.

05:05.860 --> 05:10.930
And then the callable object will be a functor, an object of this class.

05:11.590 --> 05:15.160
So it is now going to call greater-than with "t1" and "t2" as arguments.

05:18.300 --> 05:21.360
And then this one has the result reversed.

05:21.810 --> 05:22.110
Okay.

05:22.110 --> 05:23.370
So that is it for this video.

05:23.550 --> 05:24.360
I will see you next time.

05:24.600 --> 05:26.460
Until then, keep coding!
