C++:部分应用困难
我正在尝试使用函数参数的部分应用,以便可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 bindthis
as a first argument)您必须将
hasID
设为static
函数(或者从Foo
中提取它)。您希望绑定器中有一个普通的函数指针,而不是指向成员函数的指针。You have to make
hasID
astatic
function (or extract it fromFoo
at all). You want to have an ordinary function pointer in the binder, no pointer to a member function.