C++:部分应用困难

发布于 2024-09-08 12:27:50 字数 1929 浏览 2 评论 0原文

我正在尝试使用函数参数的部分应用,以便可以使用 STL 的 find_if。这是一个示例程序:(为了简洁起见,合并了类头和实现。)

#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

struct Odp
{
    int id;

    Odp(int id)
    {
        this->id = id;
    }

    ~Odp()
    {
        cout << "Destructing Odp " << id << endl;
    }
};

typedef vector<Odp*> OdpVec;

class Foo
{
public:
    void loadUp()
    {
        vec.push_back(new Odp(0));
        vec.push_back(new Odp(1));
        vec.push_back(new Odp(2));
    }
    void printWithID(int id)
    {
        OdpVec::iterator iter = find_if(vec.begin(), vec.end(), bind1st(&hasID, id));
        if (iter != vec.end())
        {
            cout << "Odp at " << *iter << " has id " << id << endl;
            return;
        }
        cout << "No Odp with id " << id << " could be found." << endl; 
    }

private:
    OdpVec vec;
    bool hasID(int id, Odp* odp)
    {
        return odp->id == id;
    }
};

int main()
{
    Foo foo;
    foo.loadUp();
    foo.printWithID(1);
}

但是,这甚至无法编译。错误是:

error C2276: '&' : illegal operation on bound member function expression

我在这里做错了什么?

更新hasID() 设为自由浮动函数会导致此错误:

error C2664: 'std::find_if' : cannot convert parameter 3 from 'std::binder1st<_Fn2>' to 'std::binder1st<_Fn2>'
1>          with
1>          [
1>              _Fn2=bool (__cdecl *)(int,Odp &)
1>          ]
1>          Cannot copy construct class 'std::binder1st<_Fn2>' due to ambiguous copy constructors or no available copy constructor
1>          with
1>          [
1>              _Fn2=bool (__cdecl *)(int,Odp &)
1>          ]

I'm trying to use partial application of function arguments so I can use STL's find_if. Here is a sample program: (Class header and implementation is merged for brevity.)

#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

struct Odp
{
    int id;

    Odp(int id)
    {
        this->id = id;
    }

    ~Odp()
    {
        cout << "Destructing Odp " << id << endl;
    }
};

typedef vector<Odp*> OdpVec;

class Foo
{
public:
    void loadUp()
    {
        vec.push_back(new Odp(0));
        vec.push_back(new Odp(1));
        vec.push_back(new Odp(2));
    }
    void printWithID(int id)
    {
        OdpVec::iterator iter = find_if(vec.begin(), vec.end(), bind1st(&hasID, id));
        if (iter != vec.end())
        {
            cout << "Odp at " << *iter << " has id " << id << endl;
            return;
        }
        cout << "No Odp with id " << id << " could be found." << endl; 
    }

private:
    OdpVec vec;
    bool hasID(int id, Odp* odp)
    {
        return odp->id == id;
    }
};

int main()
{
    Foo foo;
    foo.loadUp();
    foo.printWithID(1);
}

However, this doesn't even compile. The error is:

error C2276: '&' : illegal operation on bound member function expression

What am I doing wrong here?

UPDATE Making hasID() a free floating function results in this error:

error C2664: 'std::find_if' : cannot convert parameter 3 from 'std::binder1st<_Fn2>' to 'std::binder1st<_Fn2>'
1>          with
1>          [
1>              _Fn2=bool (__cdecl *)(int,Odp &)
1>          ]
1>          Cannot copy construct class 'std::binder1st<_Fn2>' due to ambiguous copy constructors or no available copy constructor
1>          with
1>          [
1>              _Fn2=bool (__cdecl *)(int,Odp &)
1>          ]

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

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

发布评论

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

评论(2

双手揣兜 2024-09-15 12:27:50

bind_1st 应该与函子一起使用,而不是与成员函数一起使用。

Functor 是一个重载了operator() 的对象。

您可以使用 mem_fn 围绕您的成员函数构建适配器。

在您的情况下,由于 hasID 没有使用 this 您可以只使用静态方法来完成。 (那么您不必将 this 绑定为第一个参数)

bind_1st should be used with functors not member functions.

Functor is an object with overloaded operator().

You can use mem_fn to construct an adaptor around your member function.

In your case, since hasID makes no use of this you could have done with just using a static method. (Then you don't have to bind this as a first argument)

甜警司 2024-09-15 12:27:50

您必须将 hasID 设为 static 函数(或者从 Foo 中提取它)。您希望绑定器中有一个普通的函数指针,而不是指向成员函数的指针。

You have to make hasID a static function (or extract it from Foo at all). You want to have an ordinary function pointer in the binder, no pointer to a member function.

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