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?
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 typedef
s (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 typedef
s. 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
.
The function call operator of the type that bind1st
returns is
typename Operation::result_type
operator()(const typename Operation::second_argument_type& x) const;
This won't work with reference parameters of the bound function object and a very strict C++03 compiler (more recent releases are more lax). C++03 forbids references to references.
精彩评论