Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why might std::bind1st be considered "almost unusable"?

During a conversation on boost::bind, it was noted that std::bind1st exists in C++03, but that it is "almost unusable".

I can't find anything solid to back this up.

The boost::bind documentation says:

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions. bind does not place any requirements on the function object; in particular, it does not need the result_type, first_argument_type and second_argument_type standard typedefs.

perhaps suggesting that these restrictions do apply to std::bind1st.

Other than the obvious restriction on number of arguments, what are the advantages of boost::bind to std::bind1st/std::bind2nd? Is there any merit to the assertion that std::bind1st is "almost unusable" in C++03?

like image 272
Lightness Races in Orbit Avatar asked Jan 19 '23 07:01

Lightness Races in Orbit


1 Answers

If we look at C++03 20.3.6.1 and 20.3.6.2, then we see that for the functor argument to bind1st we have a requirement of three typedefs (for the result type, first and second argument), and that the resulting operator only takes one argument.

This means that we cannot just use bind1st easily on plain function pointers as they do not have those typedefs. Also we can only use bind1st on binary functions as we have no support for more parameters. Furthermore boost::bind et al have the advantage of being able to reorder the parameters, and of course support more than just the first.

It seems to me that the most use cases for a binder are for free functions and member functions, and not for functor objects; as such the use of bind1st is quite limited (though extensible through the use of other tools like ptr_fun, but it's questionable as to whether this makes it more usable). Of course this is only based on personal experience, but someone might want to do a Google code search statistic for bind1st.

like image 182
PlasmaHH Avatar answered Feb 03 '23 07:02

PlasmaHH