简体   繁体   English

为什么 std::bind1st 可能被认为“几乎无法使用”?

[英]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".在关于boost::bind的对话中,有人指出std::bind1st存在于 C++03 中,但它“几乎无法使用”。

I can't find anything solid to back this up.我找不到任何可靠的东西来支持这一点。

The boost::bind documentation says: boost::bind文档说:

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd . boost::bind是标准函数std::bind1ststd::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. 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; bind对 function object 没有任何要求; in particular, it does not need the result_type , first_argument_type and second_argument_type standard typedefs.特别是,它不需要result_typefirst_argument_typesecond_argument_type标准类型定义。

perhaps suggesting that these restrictions do apply to std::bind1st .也许暗示这些限制确实适用于std::bind1st

Other than the obvious restriction on number of arguments, what are the advantages of boost::bind to std::bind1st / std::bind2nd ?除了对 arguments 数量的明显限制之外, boost::bindstd::bind1st / std::bind2nd优势是什么? Is there any merit to the assertion that std::bind1st is "almost unusable" in C++03? std::bind1st在 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.如果我们查看 C++03 20.3.6.1 和 20.3.6.2,我们会看到对于bind1st的函子参数,我们需要三个typedef (对于结果类型,第一个和第二个参数),并且结果运算符只接受一个参数。

This means that we cannot just use bind1st easily on plain function pointers as they do not have those typedef s.这意味着我们不能简单地在普通的 function 指针上使用bind1st ,因为它们没有那些typedef Also we can only use bind1st on binary functions as we have no support for more parameters.此外,我们只能在二进制函数上使用bind1st ,因为我们不支持更多参数。 Furthermore boost::bind et al have the advantage of being able to reorder the parameters, and of course support more than just the first.此外boost::bind等人具有能够重新排序参数的优势,当然支持的不仅仅是第一个。

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).因此, bind1st的使用非常有限(尽管可以通过使用ptr_fun等其他工具进行扩展,但是这是否使它更有用值得怀疑)。 Of course this is only based on personal experience, but someone might want to do a Google code search statistic for bind1st .当然这只是基于个人经验,但有人可能想对bind1st做一个谷歌代码搜索统计。

The function call operator of the type that bind1st returns is bind1st返回的类型的 function 调用运算符是

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).这不适用于绑定 function object 的参考参数和非常严格的 C++03 编译器(最近的版本更加宽松)。 C++03 forbids references to references. C++03 禁止引用引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM