关于 Bind1st 和 C++ 中的 men_fun 的老问题; STL

发布于 01-03 03:18 字数 782 浏览 1 评论 0原文

一些简单的代码将演示这个问题:

class Foo {};
struct Bar {
    bool foo(const Foo &f) const { return false; }
};

int main() {
    Bar bar;

    vector<Foo> v;

    std::find_if(v.begin(), v.end(), std::bind1st(
        std::mem_fun_ref(&Bar::foo), bar));

    return 0;
}

现在,对于这段代码,VS2010 c++ 编译器会抱怨: 错误 C2535: bool std::binder1st<_Fn2>::operator()(const Foo&) const: 成员函数已定义或声明

在早期版本的 Visual Studio 上,还会出现两个与引用问题相关的编译错误。虽然这些问题在 VS2010 中已经消失,但 C2535 仍然存在。

这个问题类似于这个< /a>.正如该帖子所建议的,我可以使用 std::bind 或 boost 库作为替代方案。它们工作得很好,但是现在,我想知道在这种情况下是否可以使用旧的bind1st样式,或者这个问题更多的是STL功能框架固有的缺陷? 谢谢!

Some simple code will demonstrate the problem:

class Foo {};
struct Bar {
    bool foo(const Foo &f) const { return false; }
};

int main() {
    Bar bar;

    vector<Foo> v;

    std::find_if(v.begin(), v.end(), std::bind1st(
        std::mem_fun_ref(&Bar::foo), bar));

    return 0;
}

Now, for this code, the VS2010 c++ compiler will complain:
error C2535: bool std::binder1st<_Fn2>::operator()(const Foo&) const:
member function already defined of declared

On earlier versions of Visual Studio, there would be two more compilation errors which are related to reference to reference issues. While these problems have gone in VS2010, the C2535 remains.

This question is similar to this one. As suggested by that post, i can use std::bind, or the boost library as alternatives. They work fine, but for now, i'd like to know is it possible to use the old bind1st style in this case, or is this problem more of a defect inherent in the STL functional framework?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

千笙结2025-01-10 03:18:59

这不是VS或任何编译器的问题。 mem_fun_ref 返回的类型是 mem_fun_ref_t,它继承自一元函数。该函子采用一个参数,该参数必须是成员函数所属的类类型。 mem_fun_ref 不能用于带有参数的成员函数。

活页夹已被弃用是有原因的:它们很糟糕。

This is not an issue of VS or any compiler. The type returned by mem_fun_ref is mem_fun_ref_t which inherits from unary function. This functor takes one argument which must be of the class type the member function belongs to. mem_fun_ref cannot work for member functions that take arguments.

The binders have been deprecated for a reason: They suck.

蓝天白云2025-01-10 03:18:59

请查看将bind1st用于需要以下参数的方法通过引用进行论证
看来你的情况和你的情况很相似。

Please take a look at Using bind1st for a method that takes argument by reference.
It seems that it is the case similar to your case.

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