为什么 std::bind1st 被认为“几乎无法使用”?
在关于 boost::bind
的对话中,有人指出 std::bind1st
存在于 C++03 中,但它“几乎无法使用”。
我找不到任何可靠的东西来支持这一点。
boost::bind 是 标准函数 std::bind1st 和 std::bind2nd。支持任意 函数对象、函数、函数 指针和成员函数 指针,并且能够绑定任何 特定值或路由的参数 输入参数到任意 职位。 bind 不放置任何 对功能对象的要求; 特别是,它不需要 result_type、first_argument_type 和 second_argument_type 标准 类型定义。
也许表明这些限制确实适用于std::bind1st
。
除了对参数数量的明显限制之外,boost::bind
相对于 std::bind1st
/std 的优势还有哪些? ::bind2nd
? std::bind1st
在 C++03 中“几乎无法使用”的断言有什么优点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们看一下 C++03 20.3.6.1 和 20.3.6.2,那么我们会发现,对于
bind1st
的函子参数,我们需要三个typedef
(对于结果类型、第一个和第二个参数),并且结果运算符仅采用一个参数。这意味着我们不能轻松地在普通函数指针上使用
bind1st
,因为它们没有那些typedef
。此外,我们只能在二进制函数上使用bind1st
,因为我们不支持更多参数。此外,boost::bind
等人具有能够对参数重新排序的优点,当然支持的不仅仅是第一个。在我看来,绑定器的大多数用例是自由函数和成员函数,而不是函子对象;因此,
bind1st
的使用非常有限(尽管可以通过使用ptr_fun
等其他工具进行扩展,但这是否会使其更可用是值得怀疑的)。当然这只是基于个人经验,但有人可能想对bind1st
进行 Google 代码搜索统计。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 threetypedef
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 thosetypedef
s. Also we can only usebind1st
on binary functions as we have no support for more parameters. Furthermoreboost::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 likeptr_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 forbind1st
.bind1st
返回的类型的函数调用运算符是这不适用于绑定函数对象的引用参数和非常严格的 C++03 编译器(最近的版本更加宽松)。 C++03 禁止引用引用。
The function call operator of the type that
bind1st
returns isThis 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.