Hello again!
In this video, we are going to look at library defined operators. In C++11,
the library defines some generic operator classes.
These are in the header. And they cover arithmetic operations, relational, logical and bitwise
operations. And it is fairly obvious what they all do, although the naming system is not very consistent.
And also the "not" operator was forgotten. and had to be added in C++14. These will just call the corresponding
operator.
So, for example, "less" will call the less-than operator of the corresponding type.
These are functor classes.
So we need to create an object before we can use them.
And we also need to call the function call operator.
So for example, we can call the constructor to get an object of the "less" operator, and then we have
the function call with some arguments.
And that would be equivalent to calling the less-than operator, with arguments "t1" and "t2".
We can use these operators for writing generic functions which perform default operations.
And these are similar to the way that some of the functions in the algorithm library are implemented.
For example, the sort() function. We can call this with an iterator range, and it will use the less
than operator, to order the elements.
Or, we can provide our own optional predicate, and the sort() algorithm will use that predicate instead
of less-than, for ordering the elements.
So we start by writing our function as a template function, which takes a callable object as one of
the arguments.
So we need two type parameters. One for the things which are being compared, "t1" and "t2", and one for
the callable object.
And then, in the function body, we call the callable object, with these "t1" and "t2" as the arguments.
So that is going to use this argument for doing the comparison.
And then, next, we make this callable object a default argument.
So we turn this template function into a template function with a default parameter. Like the ones that
we looked at in the last video.
So now this function type parameter is going to be the type of the "less" operator.
And then our third argument to the function is going to be an optional argument. A default argument.
And, by default, this will be an object of this type.
So this is going to be the library "less" operator.
And then, by default, our function is going to call the library "less" operator, with "t1" and
"t2" as arguments.
So how does this work?
If we provide a callable object as the third argument, then the compiler will instantiate this.
It is not going to use the default, because we provided an explicit callable object.
So it is going to use this callable object.
So it is going to call the lambda expression, with "t1" and "t2" as arguments.
On the other hand, if we do not have a callable object, then the compiler will instantiate this using
the default.
So this will use the library "less" operator.
So this is going to call less-than, with "t1" and "t2" as arguments.
So here is our template function with the default parameter type.
The default is the type of the library "less" operator. Then the function has a default argument, which
will be an object of this type.
So this will be the library "less" object.
And then by default, our function will call less-than, with "t1" and "t2" as arguments.
If we pass a lambda expression as the third argument, the compiler will instantiate this function.
It will use the lambda expression as this callable object.
So it is going to call the lambda expression, with "x" and "y" as arguments.
If we do not provide the callable object, then the compiler will instantiate this using the less-than
operator.
So this will call less-than, with "x" and "y" as arguments.
And we get true, in both cases, because 1 is less than 2.
If I change the expression, so it now uses greater-than. So the compiler will still instantiate it using the
lambda expression.
But we should now get false, because 1 is not greater than 2.
And yes, we do get false.
So if I just put that back...
If I want to change this comparison, then I need to change the function. So I need to change this type
parameter.
So it is now the "greater" library operator.
And then the callable object will be a functor, an object of this class.
So it is now going to call greater-than with "t1" and "t2" as arguments.
And then this one has the result reversed.
Okay.
So that is it for this video.
I will see you next time.
Until then, keep coding!