Hello and welcome to "C++ Weekly."
I'm your host, Jason Turner.
I'm available for code reviews, contracting,
and on-site training.
In this episode, we're going to discuss a new library feature
of C++ 20 called bind_front.
If you're familiar at all with the C++ functional header,
you may be aware that over the years,
we've had many different ways of binding arguments to function
calls.
There's been bind, which was added in C++ 11,
that comes directly from Boost.
There was unary_function, binary_function.
These things were weird base classes for some things.
Binder1st, binder2nd, bind1st, bind2nd, member function--
you know, it's just all over the place.
And most of them have been replaced by features that came
in C++ 11, 14, 17, 20, with our lambdas.
Really, there's been very little use for them.
And some people even argue that standard bind is itself
outdated-- as soon as it was released, perhaps--
and that it should have never been added to the standard,
because lambdas gave us the same functionality.
And you can see more about that in my "Avoiding Standard Bind"
episode that I recorded a very long time ago.
But there is a new bind function that has been added in C++ 20
called bind_front.
And now, according to the [? CBP ?] reference description
here, this is actually perhaps intended
to replace standard bind in many use cases, as noted here.
But it doesn't have all of the same flexibility
of standard bind.
And to be fair, that's probably not really necessary
anyhow, because standard bind does really a lot of things
that it perhaps doesn't need to do.
But for the sake of this episode,
we're just going to discuss what bind_front is.
And if you saw one of my recent episodes on lambdas about--
in C++ 20, then this will look a little familiar.
But we didn't quite take it all of the way.
I'm just going to forward declare this function
this time.
I'm not going to give it a body.
And in some ways, this is better for us
to be able to really see what the compiler is actually
having to generate.
So in this case, I have bound the first two variables
to 1 and 2, the first two function arguments.
That is, i will be 1, j will be 2.
And then when I call it, I am passing 3 to it.
And we know, if you've been watching
these videos for a while, that edi
is where the first parameter is going to go,
esi is where the second parameter is going to go.
And we rarely talk about the third parameter,
but edx is where it's going.
So we can kind of rearrange these things a little bit
and get a better idea of what the compiler is actually
doing here.
So we can see edi has been replaced with 3, esi
with 2, et cetera.
Now, if I wanted to actually do this and return these values,
we really shouldn't be surprised that the compiler can
do all of this at compile time.
So perhaps we can give it something else
to work with that is an unknown here.
And we should be able to see that yes, it's
going to add the first parameter here--
this is argc to the value 5, which is not surprising,
because 3 plus 2 equals 5.
So that's what bind_front does.
It lets you bind as many of the front arguments as you want to.
Now, if I did this, the program's
ill-formed, because I am calling it with this argument,
and that doesn't make any sense.
It should just add 3 plus 2 plus 1.
And on the other hand, if I bind just the 3, then
I should be able to do something like this,
and pass it two parameters, because again, we
are calling a function that is expecting
three parameters total.
So I have passed in 3, and then argc twice, and 1.
And as we can see, the compiler is able to optimize that here
to the value 4, that's 3 plus 1, plus rdi plus rdi.
rdi and rdi are both the first integer parameter
passed into argc here, which is-- into main, which is argc.
So this argc plus argc plus 4 is what it ends up being.
And the compiler is able to optimize all that.
That's very easy for these compilers to do.
So this bind_front function is one that is coming in C++ 20.
And if you subscribe and look forward to an upcoming episode
where I show some way that bind_front could be implemented
in C++ 20.
So be sure to subscribe and like this video.