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

发布于 2024-11-19 01:02:31 字数 864 浏览 1 评论 0原文

在关于 boost::bind 的对话中,有人指出 std::bind1st 存在于 C++03 中,但它“几乎无法使用”。

我找不到任何可靠的东西来支持这一点。

boost::bind 文档 说:

boost::bind 是 标准函数 std::bind1ststd::bind2nd。支持任意 函数对象、函数、函数 指针和成员函数 指针,并且能够绑定任何 特定值或路由的参数 输入参数到任意 职位。 bind 不放置任何 对功能对象的要求; 特别是,它不需要 result_typefirst_argument_typesecond_argument_type 标准 类型定义。

也许表明这些限制确实适用于std::bind1st

除了对参数数量的明显限制之外,boost::bind 相对于 std::bind1st/std 的优势还有哪些? ::bind2ndstd::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街九尾狐 2024-11-26 01:02:31

如果我们看一下 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 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.

携余温的黄昏 2024-11-26 01:02:31

bind1st 返回的类型的函数调用运算符是

typename Operation::result_type
operator()(const typename Operation::second_argument_type& x) const;

这不适用于绑定函数对象的引用参数和非常严格的 C++03 编译器(最近的版本更加宽松)。 C++03 禁止引用引用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文