Is there a way to partially bind the first/last n arguments of a callable object (e.g. function) without explicitly specifying the rest of the arguments?
std::bind()
seems to require that all the arguments are be bound, those that are to be left should be bound to std::placeholders::_1
,_2
,_3
etc.
Is it possible to write a bind_first()
/bind_last()
for partial binding starting from the first/last argument and that automagically inserts the placeholders for any remaining unbound arguments in their original order in their the original position?
Inspired by the question, I wrote my own prebind from scratch. It ended up looking pretty similar to everyone else though, but I promise it's original :) - call it convergent evolution.
It has a slightly different flavour though. For one thing, it forwards to it's constructor, but you may prefer to use
std::decay
(it makes more sense in some ways, but I don't like writingstd::ref
everywhere). For another I added support for nested prebinds soprebind(foo, prebind(GetRandomNumber))()
is the same asprebind(foo)(GetRandomNumber())
.It can be easily changed to postbind as well.
Usage looks like:
Neither Boost nor the standard library
bind
fill in the blanks automatically. You could write such a gadget yourself if you have a rainy evening to fill; here's an example for trailing arguments of a plain function only:Usage: